Skip to content

Commit fc1384b

Browse files
committed
Remove trim and trailing message newline in data
Now there is no need to trim it, when the data is changed. {{.Name}}{{"\n"}}{{.Message | missing "<no message>" | indent 2}} Make the prefix function not append a newline, if not there. Signed-off-by: Anders F Björklund <[email protected]>
1 parent a28654e commit fc1384b

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

pkg/store/instance.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ func PrintInstances(w io.Writer, instances []*Instance, format string) error {
285285
if err != nil {
286286
return err
287287
}
288+
data.Message = strings.TrimSuffix(instance.Message, "\n")
288289
err = tmpl.Execute(w, data)
289290
if err != nil {
290291
return err

pkg/textutil/textutil.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@ func ExecuteTemplate(tmpl string, args interface{}) ([]byte, error) {
2525

2626
// PrefixString adds prefix to beginning of each line
2727
func PrefixString(prefix string, text string) string {
28-
result := ""
28+
result := []string{}
2929
for _, line := range strings.Split(text, "\n") {
3030
if line == "" {
31+
result = append(result, "")
3132
continue
3233
}
33-
result += prefix + line + "\n"
34+
result = append(result, prefix+line)
3435
}
35-
return result
36+
return strings.Join(result, "\n")
3637
}
3738

3839
// IndentString add spaces to beginning of each line
@@ -74,13 +75,11 @@ var TemplateFuncMap = template.FuncMap{
7475
return "---\n" + strings.TrimSuffix(b.String(), "\n")
7576
},
7677
"indent": IndentString,
77-
"trim": TrimString,
7878
"missing": MissingString,
7979
}
8080

8181
// TemplateFuncHelp is help for TemplateFuncMap.
8282
var FuncHelp = []string{
8383
"indent <size>: add spaces to beginning of each line",
84-
"trim <cutset>: remove characters from beginning and end",
8584
"missing <message>: return message if the text is empty",
8685
}

pkg/textutil/textutil_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import (
99
)
1010

1111
func TestPrefixString(t *testing.T) {
12-
assert.Equal(t, "- foo\n", PrefixString("- ", "foo"))
12+
assert.Equal(t, "- foo", PrefixString("- ", "foo"))
1313
assert.Equal(t, "- foo\n- bar\n", PrefixString("- ", "foo\nbar\n"))
1414
}
1515
func TestIndentString(t *testing.T) {
16-
assert.Equal(t, " foo\n", IndentString(2, "foo"))
16+
assert.Equal(t, " foo", IndentString(2, "foo"))
1717
assert.Equal(t, " foo\n bar\n", IndentString(2, "foo\nbar\n"))
1818
}
1919

@@ -33,18 +33,18 @@ func TestTemplateFuncs(t *testing.T) {
3333
Bar string `json:"bar" yaml:"bar"`
3434
Message string `json:"message,omitempty" yaml:"message,omitempty"`
3535
}
36-
x := X{Foo: 42, Bar: "hello", Message: "One\nTwo\nThree\n"}
36+
x := X{Foo: 42, Bar: "hello", Message: "One\nTwo\nThree"}
3737

3838
testCases := map[string]string{
39-
"{{json .}}": `{"foo":42,"bar":"hello","message":"One\nTwo\nThree\n"}`,
39+
"{{json .}}": `{"foo":42,"bar":"hello","message":"One\nTwo\nThree"}`,
4040
"{{yaml .}}": `---
4141
foo: 42
4242
bar: hello
43-
message: |
43+
message: |-
4444
One
4545
Two
4646
Three`,
47-
`{{.Bar}}{{"\n"}}{{.Message | missing "<no message>" | indent 2 | trim "\n"}}`: "hello\n One\n Two\n Three",
47+
`{{.Bar}}{{"\n"}}{{.Message | missing "<no message>" | indent 2}}`: "hello\n One\n Two\n Three",
4848
}
4949

5050
for format, expected := range testCases {

0 commit comments

Comments
 (0)