Skip to content

Commit a28654e

Browse files
committed
Add template functions for list/inspect message
Makes it possible to show all messages using a format string like: {{.Name}}{{"\n"}}{{.Message | missing "<no message>" | indent 2 | trim "\n"}} In case you missed the message the first time, and want to show it. This is instead of having a dedicated lima command like "message". Signed-off-by: Anders F Björklund <[email protected]>
1 parent d8e5fe8 commit a28654e

File tree

4 files changed

+78
-6
lines changed

4 files changed

+78
-6
lines changed

cmd/limactl/list.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ func newListCommand() *cobra.Command {
4141
--format yaml - output in yaml format
4242
--format table - output in table format
4343
--format '{{ <go template> }}' - if the format begins and ends with '{{ }}', then it is used as a go template.
44-
44+
` + store.FormatHelp +
45+
`
4546
The following legacy flags continue to function:
4647
--json - equal to '--format json'
4748
`,

pkg/store/instance.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,11 @@ type FormatData struct {
211211
IdentityFile string
212212
}
213213

214+
var FormatHelp = "\n" + textutil.PrefixString("\t\t",
215+
"These functions are available to go templates:\n\n"+
216+
textutil.IndentString(2,
217+
strings.Join(textutil.FuncHelp, "\n")+"\n"))
218+
214219
func AddGlobalFields(inst *Instance) (FormatData, error) {
215220
var data FormatData
216221
data.Instance = *inst

pkg/textutil/textutil.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,37 @@ func ExecuteTemplate(tmpl string, args interface{}) ([]byte, error) {
2323
return b.Bytes(), nil
2424
}
2525

26+
// PrefixString adds prefix to beginning of each line
27+
func PrefixString(prefix string, text string) string {
28+
result := ""
29+
for _, line := range strings.Split(text, "\n") {
30+
if line == "" {
31+
continue
32+
}
33+
result += prefix + line + "\n"
34+
}
35+
return result
36+
}
37+
38+
// IndentString add spaces to beginning of each line
39+
func IndentString(size int, text string) string {
40+
prefix := strings.Repeat(" ", size)
41+
return PrefixString(prefix, text)
42+
}
43+
44+
// TrimString removes characters from beginning and end
45+
func TrimString(cutset string, text string) string {
46+
return strings.Trim(text, cutset)
47+
}
48+
49+
// MissingString returns message if the text is empty
50+
func MissingString(message string, text string) string {
51+
if text == "" {
52+
return message
53+
}
54+
return text
55+
}
56+
2657
// TemplateFuncMap is a text/template FuncMap.
2758
var TemplateFuncMap = template.FuncMap{
2859
"json": func(v interface{}) string {
@@ -42,4 +73,14 @@ var TemplateFuncMap = template.FuncMap{
4273
}
4374
return "---\n" + strings.TrimSuffix(b.String(), "\n")
4475
},
76+
"indent": IndentString,
77+
"trim": TrimString,
78+
"missing": MissingString,
79+
}
80+
81+
// TemplateFuncHelp is help for TemplateFuncMap.
82+
var FuncHelp = []string{
83+
"indent <size>: add spaces to beginning of each line",
84+
"trim <cutset>: remove characters from beginning and end",
85+
"missing <message>: return message if the text is empty",
4586
}

pkg/textutil/textutil_test.go

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,43 @@ import (
88
"gotest.tools/v3/assert"
99
)
1010

11+
func TestPrefixString(t *testing.T) {
12+
assert.Equal(t, "- foo\n", PrefixString("- ", "foo"))
13+
assert.Equal(t, "- foo\n- bar\n", PrefixString("- ", "foo\nbar\n"))
14+
}
15+
func TestIndentString(t *testing.T) {
16+
assert.Equal(t, " foo\n", IndentString(2, "foo"))
17+
assert.Equal(t, " foo\n bar\n", IndentString(2, "foo\nbar\n"))
18+
}
19+
20+
func TestTrimString(t *testing.T) {
21+
assert.Equal(t, "foo", TrimString("\n", "foo"))
22+
assert.Equal(t, "bar", TrimString("\n", "bar\n"))
23+
}
24+
25+
func TestMissingString(t *testing.T) {
26+
assert.Equal(t, "no", MissingString("no", ""))
27+
assert.Equal(t, "msg", MissingString("no", "msg"))
28+
}
29+
1130
func TestTemplateFuncs(t *testing.T) {
1231
type X struct {
13-
Foo int `json:"foo" yaml:"foo"`
14-
Bar string `json:"bar" yaml:"bar"`
32+
Foo int `json:"foo" yaml:"foo"`
33+
Bar string `json:"bar" yaml:"bar"`
34+
Message string `json:"message,omitempty" yaml:"message,omitempty"`
1535
}
16-
x := X{Foo: 42, Bar: "hello"}
36+
x := X{Foo: 42, Bar: "hello", Message: "One\nTwo\nThree\n"}
1737

1838
testCases := map[string]string{
19-
"{{json .}}": `{"foo":42,"bar":"hello"}`,
39+
"{{json .}}": `{"foo":42,"bar":"hello","message":"One\nTwo\nThree\n"}`,
2040
"{{yaml .}}": `---
2141
foo: 42
22-
bar: hello`,
42+
bar: hello
43+
message: |
44+
One
45+
Two
46+
Three`,
47+
`{{.Bar}}{{"\n"}}{{.Message | missing "<no message>" | indent 2 | trim "\n"}}`: "hello\n One\n Two\n Three",
2348
}
2449

2550
for format, expected := range testCases {

0 commit comments

Comments
 (0)