-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Mathieu Frenette <[email protected]>
- Loading branch information
Showing
3 changed files
with
61 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |