2020-11-17 05:41:34 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2020-11-22 05:25:24 +00:00
|
|
|
"path/filepath"
|
2020-11-17 05:41:34 +00:00
|
|
|
|
2021-11-02 03:08:41 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2020-11-17 05:41:34 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
var Test = &cli.Command{
|
|
|
|
Name: "test",
|
|
|
|
Usage: "Test if a directory is a valid template",
|
2020-11-22 05:25:24 +00:00
|
|
|
Description: "Test whether a directory is valid for use with tmpl",
|
|
|
|
ArgsUsage: "[path (default: \".\")]",
|
2020-11-17 05:41:34 +00:00
|
|
|
Action: runTest,
|
|
|
|
}
|
|
|
|
|
2020-11-22 05:25:24 +00:00
|
|
|
func runTest(ctx *cli.Context) error {
|
|
|
|
testPath := "."
|
|
|
|
if ctx.NArg() > 0 {
|
|
|
|
testPath = ctx.Args().First()
|
|
|
|
}
|
|
|
|
|
2020-11-17 05:41:34 +00:00
|
|
|
var errs []string
|
2020-11-22 05:25:24 +00:00
|
|
|
if _, err := os.Lstat(filepath.Join(testPath, "template.toml")); err != nil {
|
2020-11-17 05:41:34 +00:00
|
|
|
errs = append(errs, "could not find template.toml")
|
|
|
|
}
|
|
|
|
|
2020-11-22 05:25:24 +00:00
|
|
|
fi, err := os.Lstat(filepath.Join(testPath, "template"))
|
2020-11-17 05:41:34 +00:00
|
|
|
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 {
|
2021-11-02 03:08:41 +00:00
|
|
|
log.Error().Msg(err)
|
2020-11-17 05:41:34 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-11-02 03:08:41 +00:00
|
|
|
log.Info().Msg("this is a valid tmpl template")
|
2020-11-17 05:41:34 +00:00
|
|
|
return nil
|
|
|
|
}
|