From 173e1f84ddae9df6755409ecc9227b4ad4a73886 Mon Sep 17 00:00:00 2001 From: Mathieu Frenette Date: Sat, 6 Mar 2021 11:53:40 -0500 Subject: [PATCH] Add `jen list templates` Signed-off-by: Mathieu Frenette --- README.md | 1 - src/cmd/list/list.go | 2 + src/cmd/list/templates/templates.go | 59 +++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 src/cmd/list/templates/templates.go diff --git a/README.md b/README.md index 93e3e6d..0a2d9d0 100644 --- a/README.md +++ b/README.md @@ -436,7 +436,6 @@ To associate a template with an existing project that was not initially generate - Add `confirm` step (similar to `if`, but `confirm` property contains message to display and `then` the steps to execute). - Add `jen export` command to output env variables in a format that can be sourced directly. -- Add `jen list templates` to list available templates. - Add `jen list vars` to list project variables and their values (same as `jen export` but more human-readable). - Add `jen chk vars VAR1 VAR2 ...` to ensure that all given variables are set in environment (to document and make scripts more robust). - Allow `do` step to define multiple actions to call. diff --git a/src/cmd/list/list.go b/src/cmd/list/list.go index de792f7..f2b4f53 100644 --- a/src/cmd/list/list.go +++ b/src/cmd/list/list.go @@ -6,6 +6,7 @@ import ( "github.com/Samasource/jen/src/cmd/internal" "github.com/Samasource/jen/src/cmd/list/actions" "github.com/Samasource/jen/src/cmd/list/scripts" + "github.com/Samasource/jen/src/cmd/list/templates" "github.com/Samasource/jen/src/internal/shell" "github.com/spf13/cobra" ) @@ -22,6 +23,7 @@ func New(options *internal.Options) *cobra.Command { } c.AddCommand(actions.New(options)) c.AddCommand(scripts.New(options)) + c.AddCommand(templates.New(options)) return c } diff --git a/src/cmd/list/templates/templates.go b/src/cmd/list/templates/templates.go new file mode 100644 index 0000000..6e790fd --- /dev/null +++ b/src/cmd/list/templates/templates.go @@ -0,0 +1,59 @@ +package templates + +import ( + "fmt" + "io/ioutil" + "path/filepath" + "strings" + + "github.com/Samasource/jen/src/cmd/internal" + "github.com/Samasource/jen/src/internal/home" + "github.com/Samasource/jen/src/internal/spec" + "github.com/spf13/cobra" +) + +// New creates a cobra command +func New(options *internal.Options) *cobra.Command { + return &cobra.Command{ + Use: "templates", + Aliases: []string{"template"}, + Short: "Lists templates available in git clone", + Args: cobra.NoArgs, + RunE: func(_ *cobra.Command, args []string) error { + return run(options, args) + }, + } +} + +func run(options *internal.Options, args []string) error { + _, err := home.GetOrCloneRepo() + if err != nil { + return err + } + + templatesDir, err := home.GetTemplatesDir() + if err != nil { + return err + } + + // Read templates dir + infos, err := ioutil.ReadDir(templatesDir) + if err != nil { + return fmt.Errorf("reading templates directory %q: %w", templatesDir, err) + } + + // Print templates with descriptions + for _, info := range infos { + template := info.Name() + if strings.HasPrefix(template, ".") { + continue + } + templateDir := filepath.Join(templatesDir, template) + spec, err := spec.Load(templateDir) + if err != nil { + return err + } + fmt.Printf("%s - %s\n", template, spec.Description) + } + return nil +}