Merge pull request #5019 from laurazard/multiple-plugin-hooks

plugins/templates: break on newlines when printing hooks
This commit is contained in:
Laura Brehm 2024-04-15 13:55:33 +01:00 committed by GitHub
commit c0cc22db58
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 18 additions and 13 deletions

View File

@ -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")

View File

@ -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)
}
}

View File

@ -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
}