Skip to content

Commit

Permalink
Add jen list templates
Browse files Browse the repository at this point in the history
Signed-off-by: Mathieu Frenette <[email protected]>
  • Loading branch information
silphid committed Mar 6, 2021
1 parent 4563c0b commit 173e1f8
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions src/cmd/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
}

Expand Down
59 changes: 59 additions & 0 deletions src/cmd/list/templates/templates.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 173e1f8

Please sign in to comment.