Skip to content

Commit

Permalink
Merge pull request #42 from gruntwork-io/skip-hooks
Browse files Browse the repository at this point in the history
Add ability to skip hooks
  • Loading branch information
brikis98 committed Oct 20, 2017
2 parents 66e7653 + 332bea8 commit ca49865
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 3 deletions.
5 changes: 5 additions & 0 deletions _docs/README.md
Expand Up @@ -206,12 +206,14 @@ hooks:
- <ARG>
env:
<KEY>: <VALUE>
skip: <CONDITION>
after:
- command: <CMD>
args:
- <ARG>
env:
<KEY>: <VALUE>
skip: <CONDITION>
```

Here's an example:
Expand Down Expand Up @@ -399,6 +401,9 @@ Note the following:
- {{"{{"}} .AnotherVariable {{"}}"}}
```
* Boilerplate runs your `command` with the working directory set to the `--template-folder` option.
* `skip` (Optional): Skip this hook if this condition, which can use Go templating syntax and
boilerplate variables, evaluates to the string `true`. This is useful to conditionally enable or disable
dependencies.
* For an alternative way to execute commands, see the `shell` helper in [template helpers](#template-helpers).

#### Templates
Expand Down
6 changes: 4 additions & 2 deletions config/config_test.go
Expand Up @@ -593,6 +593,7 @@ const CONFIG_MULTIPLE_HOOKS =
env:
foo: bar
baz: blah
skip: "true"
- command: run-some-script.sh
args:
Expand All @@ -601,6 +602,7 @@ const CONFIG_MULTIPLE_HOOKS =
after:
- command: foo
skip: "{{ .baz }}"
- command: bar
`

Expand All @@ -613,11 +615,11 @@ func TestParseBoilerplateConfigMultipleHooks(t *testing.T) {
Dependencies: []variables.Dependency{},
Hooks: variables.Hooks{
BeforeHooks: []variables.Hook{
{Command: "echo", Args: []string{"Hello World"}, Env: map[string]string{"foo": "bar", "baz": "blah"}},
{Command: "echo", Args: []string{"Hello World"}, Env: map[string]string{"foo": "bar", "baz": "blah"}, Skip: "true"},
{Command: "run-some-script.sh", Args: []string{"{{ .foo }}", "{{ .bar }}"}},
},
AfterHooks: []variables.Hook{
{Command: "foo"},
{Command: "foo", Skip: "{{ .baz }}"},
{Command: "bar"},
},
},
Expand Down
7 changes: 7 additions & 0 deletions examples/shell/boilerplate.yml
Expand Up @@ -14,6 +14,13 @@ hooks:
env:
TEXT: "{{ .Text }} - executed via a before hook"

- command: ./example-script-2.sh
args:
- "{{ outputFolder }}/before-hook-should-be-skipped.txt"
env:
TEXT: "this hook should be skipped"
skip: "true"

after:
- command: echo
args:
Expand Down
24 changes: 24 additions & 0 deletions templates/template_processor.go
Expand Up @@ -76,6 +76,15 @@ func processHooks(hooks []variables.Hook, opts *options.BoilerplateOptions, vars

// Process the given hook, which is a script that should be execute at the command-line
func processHook(hook variables.Hook, opts *options.BoilerplateOptions, vars map[string]interface{}) error {
skip, err := shouldSkipHook(hook, opts, vars)
if err != nil {
return err
}
if skip {
util.Logger.Printf("Skipping hook with command '%s'", hook.Command)
return nil
}

cmd, err := render.RenderTemplate(config.BoilerplateConfigPath(opts.TemplateFolder), hook.Command, vars, opts)
if err != nil {
return err
Expand Down Expand Up @@ -108,6 +117,21 @@ func processHook(hook variables.Hook, opts *options.BoilerplateOptions, vars map
return util.RunShellCommand(opts.TemplateFolder, envVars, cmd, args...)
}

// Return true if the "skip" condition of this hook evaluates to true
func shouldSkipHook(hook variables.Hook, opts *options.BoilerplateOptions, vars map[string]interface{}) (bool, error) {
if hook.Skip == "" {
return false, nil
}

rendered, err := render.RenderTemplateRecursively(opts.TemplateFolder, hook.Skip, vars, opts)
if err != nil {
return false, err
}

util.Logger.Printf("Skip attribute for hook with command '%s' evaluated to '%s'", hook.Command, rendered)
return rendered == "true", nil
}

// Execute the boilerplate templates in the given list of dependencies
func processDependencies(dependencies []variables.Dependency, opts *options.BoilerplateOptions, variables map[string]interface{}) error {
for _, dependency := range dependencies {
Expand Down
14 changes: 13 additions & 1 deletion variables/hooks.go
Expand Up @@ -5,6 +5,7 @@ type Hook struct {
Command string
Args []string
Env map[string]string
Skip string
}

// All the scripts to execute as boilerplate hooks
Expand All @@ -22,13 +23,15 @@ type Hooks struct {
// - <ARG>
// env:
// <KEY>: <VALUE>
// skip: <CONDITION>
//
// after:
// - command: <CMD>
// args:
// - <ARG>
// env:
// <KEY>: <VALUE>
// skip: <CONDITION>
//
// This method takes the data above and unmarshals it into a Hooks struct
func UnmarshalHooksFromBoilerplateConfigYaml(fields map[string]interface{}) (Hooks, error) {
Expand Down Expand Up @@ -111,5 +114,14 @@ func unmarshalHookFromBoilerplateConfigYaml(fields map[string]interface{}, hookN
return nil, err
}

return &Hook{Command: *command, Args: args, Env: env}, nil
skipPtr, err := unmarshalStringField(fields, "skip", false, hookName)
if err != nil {
return nil, err
}
var skip string
if skipPtr != nil {
skip = *skipPtr
}

return &Hook{Command: *command, Args: args, Env: env, Skip: skip}, nil
}

0 comments on commit ca49865

Please sign in to comment.