Skip to content

Commit 6e286a4

Browse files
authored
feat(printer): add a json function in the template printer (#4667)
1 parent 556a310 commit 6e286a4

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

cmd/scw/testdata/test-all-usage-help-output-usage.golden

+4
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ Visit https://golang.org/pkg/text/template/ to learn more about Go template form
7676
foo||11111111-1111-1111-1111-111111111111
7777
bar||22222222-2222-2222-2222-222222222222
7878

79+
In case the command returns data that is not correctly converted, for instance a []byte, you can use the json function to marshal the data as json and print it as is.
80+
81+
scw instance server list -o template="{{ json . }}"
82+
7983
USAGE:
8084
scw help output
8185

core/printer.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,17 @@ func setupJSONPrinter(printer *Printer, opts string) error {
106106
}
107107

108108
func setupTemplatePrinter(printer *Printer, opts string) error {
109+
funcMap := template.FuncMap{
110+
"json": func(v interface{}) string {
111+
b, err := json.Marshal(v)
112+
if err != nil {
113+
return fmt.Sprintf("Error: %s", err)
114+
}
115+
116+
return string(b)
117+
},
118+
}
119+
109120
printer.printerType = PrinterTypeTemplate
110121
if opts == "" {
111122
return &CliError{
@@ -115,7 +126,7 @@ func setupTemplatePrinter(printer *Printer, opts string) error {
115126
}
116127
}
117128

118-
t, err := template.New("OutputFormat").Parse(opts)
129+
t, err := template.New("OutputFormat").Funcs(funcMap).Parse(opts)
119130
if err != nil {
120131
return err
121132
}

docs/commands/help.md

+8
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ Visit https://golang.org/pkg/text/template/ to learn more about Go template form
160160
foo||11111111-1111-1111-1111-111111111111
161161
bar||22222222-2222-2222-2222-222222222222
162162

163+
In case the command returns data that is not correctly converted, for instance a []byte, you can use the json function to marshal the data as json and print it as is.
164+
165+
scw instance server list -o template="{{ json . }}"
166+
163167

164168
Output formatting in the CLI
165169

@@ -237,6 +241,10 @@ Visit https://golang.org/pkg/text/template/ to learn more about Go template form
237241
foo||11111111-1111-1111-1111-111111111111
238242
bar||22222222-2222-2222-2222-222222222222
239243

244+
In case the command returns data that is not correctly converted, for instance a []byte, you can use the json function to marshal the data as json and print it as is.
245+
246+
scw instance server list -o template="{{ json . }}"
247+
240248

241249
**Usage:**
242250

internal/namespaces/help/output.go

+4
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,9 @@ Visit https://golang.org/pkg/text/template/ to learn more about Go template form
7777
7878
foo||11111111-1111-1111-1111-111111111111
7979
bar||22222222-2222-2222-2222-222222222222
80+
81+
In case the command returns data that is not correctly converted, for instance a []byte, you can use the json function to marshal the data as json and print it as is.
82+
83+
scw instance server list -o template="{{ json . }}"
8084
`
8185
)

0 commit comments

Comments
 (0)