2020-11-17 05:41:34 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2022-06-14 19:59:53 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-11-17 05:41:34 +00:00
|
|
|
"os"
|
2020-11-22 05:25:24 +00:00
|
|
|
"path/filepath"
|
2020-11-17 05:41:34 +00:00
|
|
|
|
2022-06-14 19:59:53 +00:00
|
|
|
"go.jolheiser.com/tmpl/schema"
|
|
|
|
|
2021-11-02 03:22:27 +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
|
2022-06-14 19:59:53 +00:00
|
|
|
|
|
|
|
fi, err := os.Open(filepath.Join(testPath, "tmpl.yaml"))
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, fmt.Sprintf("could not open tmpl.yaml: %v", err))
|
|
|
|
}
|
|
|
|
defer fi.Close()
|
|
|
|
if err := schema.Lint(fi); err != nil {
|
|
|
|
var rerr schema.ResultErrors
|
|
|
|
if errors.As(err, &rerr) {
|
|
|
|
for _, re := range rerr {
|
|
|
|
errs = append(errs, fmt.Sprintf("%s: %s", re.Field(), re.Description()))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
errs = append(errs, fmt.Sprintf("could not lint tmpl.yaml: %v", err))
|
|
|
|
}
|
2020-11-17 05:41:34 +00:00
|
|
|
}
|
|
|
|
|
2022-06-14 19:59:53 +00:00
|
|
|
fstat, 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")
|
|
|
|
}
|
2022-06-14 19:59:53 +00:00
|
|
|
if err == nil && !fstat.IsDir() {
|
2020-11-17 05:41:34 +00:00
|
|
|
errs = append(errs, "template path is a file, not a directory")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errs) > 0 {
|
|
|
|
for _, err := range errs {
|
2021-11-02 03:22:27 +00:00
|
|
|
log.Error().Msg(err)
|
2020-11-17 05:41:34 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-06-14 19:59:53 +00:00
|
|
|
|
|
|
|
log.Info().Msg("This is a valid tmpl template!")
|
2020-11-17 05:41:34 +00:00
|
|
|
return nil
|
|
|
|
}
|