Skip to content

Commit

Permalink
plugins/templates: break on newlines when printing hooks
Browse files Browse the repository at this point in the history
Signed-off-by: Laura Brehm <laurabrehm@hey.com>
  • Loading branch information
laurazard committed Apr 15, 2024
1 parent c23a404 commit 867061b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
9 changes: 5 additions & 4 deletions cli-plugins/hooks/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"strconv"
"strings"
"text/template"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -71,18 +72,18 @@ func TemplateReplaceArg(i int) string {
return fmt.Sprintf(hookTemplateArg, strconv.Itoa(i))
}

func ParseTemplate(hookTemplate string, cmd *cobra.Command) (string, error) {
func ParseTemplate(hookTemplate string, cmd *cobra.Command) ([]string, error) {
tmpl := template.New("").Funcs(commandFunctions)
tmpl, err := tmpl.Parse(hookTemplate)
if err != nil {
return "", err
return nil, err
}
b := bytes.Buffer{}
err = tmpl.Execute(&b, cmd)
if err != nil {
return "", err
return nil, err
}
return b.String(), nil
return strings.Split(b.String(), "\n"), nil
}

var ErrHookTemplateParse = errors.New("failed to parse hook template")
Expand Down
20 changes: 12 additions & 8 deletions cli-plugins/hooks/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ func TestParseTemplate(t *testing.T) {
template string
flags []testFlag
args []string
expectedOutput string
expectedOutput []string
}{
{
template: "",
expectedOutput: "",
expectedOutput: []string{""},
},
{
template: "a plain template message",
expectedOutput: "a plain template message",
expectedOutput: []string{"a plain template message"},
},
{
template: TemplateReplaceFlagValue("tag"),
Expand All @@ -34,7 +34,7 @@ func TestParseTemplate(t *testing.T) {
value: "my-tag",
},
},
expectedOutput: "my-tag",
expectedOutput: []string{"my-tag"},
},
{
template: TemplateReplaceFlagValue("test-one") + " " + TemplateReplaceFlagValue("test2"),
Expand All @@ -48,17 +48,21 @@ func TestParseTemplate(t *testing.T) {
value: "value2",
},
},
expectedOutput: "value value2",
expectedOutput: []string{"value value2"},
},
{
template: TemplateReplaceArg(0) + " " + TemplateReplaceArg(1),
args: []string{"zero", "one"},
expectedOutput: "zero one",
expectedOutput: []string{"zero one"},
},
{
template: "You just pulled " + TemplateReplaceArg(0),
args: []string{"alpine"},
expectedOutput: "You just pulled alpine",
expectedOutput: []string{"You just pulled alpine"},
},
{
template: "one line\nanother line!",
expectedOutput: []string{"one line", "another line!"},
},
}

Expand All @@ -77,6 +81,6 @@ func TestParseTemplate(t *testing.T) {

out, err := ParseTemplate(tc.template, testCmd)
assert.NilError(t, err)
assert.Equal(t, out, tc.expectedOutput)
assert.DeepEqual(t, out, tc.expectedOutput)
}
}
2 changes: 1 addition & 1 deletion cli-plugins/manager/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func invokeAndCollectHooks(dockerCli command.Cli, rootCmd, subCmd *cobra.Command
if err != nil {
continue
}
nextSteps = append(nextSteps, processedHook)
nextSteps = append(nextSteps, processedHook...)
}
return nextSteps
}
Expand Down

0 comments on commit 867061b

Please sign in to comment.