Home

tmpl @71213332903e2651e8aaefae6b8c34909cc79519 - refs - log -
-
https://git.jolheiser.com/tmpl.git
Template automation
tmpl / cmd / test.go
- raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package cmd

import (
	"os"
	"path/filepath"

	"github.com/urfave/cli/v2"
	"go.jolheiser.com/beaver"
)

var Test = &cli.Command{
	Name:        "test",
	Usage:       "Test if a directory is a valid template",
	Description: "Test whether a directory is valid for use with tmpl",
	ArgsUsage:   "[path (default: \".\")]",
	Action:      runTest,
}

func runTest(ctx *cli.Context) error {
	testPath := "."
	if ctx.NArg() > 0 {
		testPath = ctx.Args().First()
	}

	var errs []string
	if _, err := os.Lstat(filepath.Join(testPath, "template.toml")); err != nil {
		errs = append(errs, "could not find template.toml")
	}

	fi, err := os.Lstat(filepath.Join(testPath, "template"))
	if err != nil {
		errs = append(errs, "no template directory found")
	}
	if err == nil && !fi.IsDir() {
		errs = append(errs, "template path is a file, not a directory")
	}

	if len(errs) > 0 {
		for _, err := range errs {
			beaver.Error(err)
		}
		return nil
	}
	beaver.Info("this is a valid tmpl template")
	return nil
}