diff --git a/cli-plugins/hooks/template.go b/cli-plugins/hooks/template.go index d7e114e1cd..e6bd69f387 100644 --- a/cli-plugins/hooks/template.go +++ b/cli-plugins/hooks/template.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "strconv" + "strings" "text/template" "github.com/spf13/cobra" @@ -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") diff --git a/cli-plugins/hooks/template_test.go b/cli-plugins/hooks/template_test.go index fbf86ae97f..3154eaf36d 100644 --- a/cli-plugins/hooks/template_test.go +++ b/cli-plugins/hooks/template_test.go @@ -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"), @@ -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"), @@ -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!"}, }, } @@ -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) } } diff --git a/cli-plugins/manager/hooks.go b/cli-plugins/manager/hooks.go index b4f8d16ddf..09c49397f6 100644 --- a/cli-plugins/manager/hooks.go +++ b/cli-plugins/manager/hooks.go @@ -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 }