Skip to content

Commit

Permalink
Add jen list actions.
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 f1c5547 commit a0660ae
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 6 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,17 @@ $ jen do create

## Invoking actions

You can now call different project actions with `jen do ACTION`. This `hello-world` example includes actions `create`, `prompt`, `install` and `uninstall`. The last two are meant to register your project with your CI/CD pipeline and infra, but here they just call dummy bash scripts that simulate the real thing. For example:
You can now call different project actions with `jen do ACTION`, but first let's see what actions this `hello-world` example defines:

```bash
$ jen list actions
create
install
prompt
uninstall
```

We have already discussed about `create` and `prompt`. Now, `install` and `uninstall` are meant to register/unregister your project with your CI/CD pipeline and infra, but here they just call dummy bash scripts that simulate the real thing. For example:

```bash
$ jen do install
Expand Down Expand Up @@ -395,13 +405,13 @@ To associate a template with an existing project that was not initially generate
- Add `jen list scripts` to list available scripts (both shared and template-specific).
- 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).
- Add `jen shell` to start a sub-shell with all project variables in environment (same as `jen exec $SHELL`).
- Allow `do` step to define multiple actions to call.
- Invoking `jen do` without specifying an action should prompt user to select it from available list of actions.
- Add reusable modules (including both templates and scripts).
- Add `set` step to set multiple variables.
- Add `--dry-run` flag (automatically turns on `--verbose`?).
- Add regex validation for `input` prompt.
- Add more example templates, for go, node...
- Fix `choice` step to pre-select current value, if any.
- Allow special `.tmpl` and `.notmpl` extensions to be placed before actual extension (ie: `file.tmpl.txt`), to allow file editor to recognize them better during template editing.
- Allow to customize placeholders in spec file:
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/do/do.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/spf13/cobra"
)

// New creates the "jen do" cobra sub-command
// New creates a cobra command
func New(options *internal.Options) *cobra.Command {
return &cobra.Command{
Use: "do",
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/spf13/cobra"
)

// New creates the "jen exec" cobra sub-command
// New creates a cobra command
func New(options *internal.Options) *cobra.Command {
return &cobra.Command{
Use: "exec",
Expand Down
11 changes: 11 additions & 0 deletions src/cmd/internal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"

"github.com/Samasource/jen/src/internal/exec"
Expand Down Expand Up @@ -173,6 +174,16 @@ func (c context) GetAction(name string) exec.Executable {
return action
}

// GetActionNames returns the names of all actions available in template.
func (c context) GetActionNames() []string {
names := make([]string, 0, len(c.spec.Actions))
for name := range c.spec.Actions {
names = append(names, name)
}
sort.Strings(names)
return names
}

// GetProjectDir returns the current project's dir
func (c context) GetProjectDir() string {
return c.project.Dir
Expand Down
33 changes: 33 additions & 0 deletions src/cmd/list/actions/actions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package actions

import (
"fmt"

"github.com/Samasource/jen/src/cmd/internal"
"github.com/spf13/cobra"
)

// New creates a cobra command
func New(options *internal.Options) *cobra.Command {
return &cobra.Command{
Use: "actions",
Aliases: []string{"action"},
Short: "Lists actions available in current template",
Args: cobra.NoArgs,
RunE: func(_ *cobra.Command, args []string) error {
return run(options, args)
},
}
}

func run(options *internal.Options, args []string) error {
execContext, err := options.NewContext()
if err != nil {
return err
}

for _, action := range execContext.GetActionNames() {
fmt.Println(action)
}
return nil
}
33 changes: 33 additions & 0 deletions src/cmd/list/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package list

import (
"strings"

"github.com/Samasource/jen/src/cmd/internal"
"github.com/Samasource/jen/src/cmd/list/actions"
"github.com/Samasource/jen/src/internal/shell"
"github.com/spf13/cobra"
)

// New creates a cobra command
func New(options *internal.Options) *cobra.Command {
c := &cobra.Command{
Use: "list",
Aliases: []string{"ls"},
Short: "Lists available templates, actions, variables or scripts",
RunE: func(_ *cobra.Command, args []string) error {
return run(options, args)
},
}
c.AddCommand(actions.New(options))
return c
}

func run(options *internal.Options, args []string) error {
execContext, err := options.NewContext()
if err != nil {
return err
}

return shell.Execute(execContext.GetShellVars(), "", strings.Join(args, " "))
}
2 changes: 1 addition & 1 deletion src/cmd/pull/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/spf13/cobra"
)

// New creates the "jen pull" cobra sub-command
// New creates a cobra command
func New() *cobra.Command {
return &cobra.Command{
Use: "pull",
Expand Down
2 changes: 2 additions & 0 deletions src/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/Samasource/jen/src/cmd/do"
"github.com/Samasource/jen/src/cmd/exec"
"github.com/Samasource/jen/src/cmd/internal"
"github.com/Samasource/jen/src/cmd/list"
"github.com/Samasource/jen/src/cmd/pull"
"github.com/Samasource/jen/src/cmd/shell"
"github.com/Samasource/jen/src/internal/logging"
Expand All @@ -30,5 +31,6 @@ continues to support you throughout development in executing project-related com
c.AddCommand(do.New(&options))
c.AddCommand(exec.New(&options))
c.AddCommand(shell.New(&options))
c.AddCommand(list.New(&options))
return c
}
2 changes: 1 addition & 1 deletion src/cmd/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/spf13/cobra"
)

// New creates the "jen exec" cobra sub-command
// New creates a cobra command
func New(options *internal.Options) *cobra.Command {
return &cobra.Command{
Use: "shell",
Expand Down
3 changes: 3 additions & 0 deletions src/internal/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ type Context interface {
// found.
GetAction(name string) Executable

// GetActionNames returns the names of all actions available in template.
GetActionNames() []string

// GetProjectDir returns the current project's dir
GetProjectDir() string
}
Expand Down

0 comments on commit a0660ae

Please sign in to comment.