Skip to content

Commit

Permalink
Add support for multiple child actions under do step
Browse files Browse the repository at this point in the history
Signed-off-by: Mathieu Frenette <[email protected]>
  • Loading branch information
silphid committed Mar 7, 2021
1 parent c89c0d5 commit 4b28c02
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 15 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -360,9 +360,13 @@ actions:
# By convention, the "uninstall" action is in charge of removing the project from infra
uninstall:
# Here the "exec" step is invoked multiple times, each executing a single command
- exec: remove-docker-repo
- exec: remove-cicd-triggers
# The "confirm" step is similar to "if", however it prompts user with given message and
# only upon confirmation executes steps in the "then" clause.
- confirm: Are you sure you want to completely uninstall project {{.PROJECT}} from infrastructure?
then:
# Here the "exec" step is invoked multiple times, each executing a single command
- exec: remove-docker-repo
- exec: remove-cicd-triggers
```

# Templates
Expand Down Expand Up @@ -474,7 +478,6 @@ To associate a template with an existing project that was not initially generate

# Wishlist

- Allow `do` step to define multiple actions to call.
- Add reusable modules (including both templates and scripts).
- Add support for injecting snippets in specific sections of files in a second time (ie: adding multiple endpoints to an existing service).
- Add `set` step to set multiple variables.
Expand Down
6 changes: 3 additions & 3 deletions src/internal/spec/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func loadExecutable(node yaml.Node, templateDir string) (exec.Executable, error)
},
{
name: "do",
defaultSubKey: "action",
defaultSubKey: "actions",
fct: loadDoStep,
},
}
Expand Down Expand Up @@ -361,12 +361,12 @@ func loadExecStep(_map yaml.Map, templateDir string) (exec.Executable, error) {
}

func loadDoStep(_map yaml.Map, templateDir string) (exec.Executable, error) {
action, err := getRequiredStringFromMap(_map, "action")
actions, err := getRequiredStringsOrStringFromMap(_map, "actions")
if err != nil {
return nil, err
}

return do.Do{
Action: action,
Actions: actions,
}, nil
}
31 changes: 28 additions & 3 deletions src/internal/spec/spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,19 @@ exec:
},
},
},
{
Name: "exec step multiple child strings",
Buffer: `
exec:
- Command 1
- Command 2`,
Expected: execstep.Exec{
Commands: []string{
"Command 1",
"Command 2",
},
},
},
{
Name: "exec step short-hand",
Buffer: `
Expand All @@ -264,17 +277,29 @@ exec: Command 1`,
Name: "do step long-hand",
Buffer: `
do:
action: Action`,
actions:
- Action 1
- Action 2`,
Expected: do.Do{
Actions: []string{"Action 1", "Action 2"},
},
},
{
Name: "do step multiple child strings",
Buffer: `
do:
- Action 1
- Action 2`,
Expected: do.Do{
Action: "Action",
Actions: []string{"Action 1", "Action 2"},
},
},
{
Name: "do step short-hand",
Buffer: `
do: Action`,
Expected: do.Do{
Action: "Action",
Actions: []string{"Action"},
},
},
}
Expand Down
16 changes: 11 additions & 5 deletions src/internal/steps/do/do.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
// Do represents a reference to another action within same spec file to which
// execution will be delegated
type Do struct {
Action string
Actions []string
}

func (d Do) String() string {
Expand All @@ -18,9 +18,15 @@ func (d Do) String() string {

// Execute executes another action with given name within same spec file
func (d Do) Execute(context exec.Context) error {
action := context.GetAction(d.Action)
if action == nil {
return fmt.Errorf("action %q not found for do step", d.Action)
for _, action := range d.Actions {
action := context.GetAction(action)
if action == nil {
return fmt.Errorf("action %q not found for do step", action)
}
err := action.Execute(context)
if err != nil {
return err
}
}
return action.Execute(context)
return nil
}

0 comments on commit 4b28c02

Please sign in to comment.