Skip to content

Commit fc11c85

Browse files
Added "json" as specific value for --format flag in list commands, as an alias to {{json .}}
Signed-off-by: Silvin Lubecki <silvin.lubecki@docker.com>
1 parent 71824d3 commit fc11c85

2 files changed

Lines changed: 82 additions & 3 deletions

File tree

cli/command/formatter/formatter.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,21 @@ import (
77
"text/tabwriter"
88
"text/template"
99

10-
"github.com/docker/cli/templates"
1110
"github.com/pkg/errors"
11+
12+
"github.com/docker/cli/templates"
1213
)
1314

1415
// Format keys used to specify certain kinds of output formats
1516
const (
1617
TableFormatKey = "table"
1718
RawFormatKey = "raw"
1819
PrettyFormatKey = "pretty"
20+
JSONFormatKey = "json"
1921

2022
DefaultQuietFormat = "{{.ID}}"
23+
24+
jsonFormat = "{{json .}}"
2125
)
2226

2327
// Format is the format string rendered using the Context
@@ -28,6 +32,11 @@ func (f Format) IsTable() bool {
2832
return strings.HasPrefix(string(f), TableFormatKey)
2933
}
3034

35+
// IsJSON returns true if the format is the json format
36+
func (f Format) IsJSON() bool {
37+
return string(f) == JSONFormatKey
38+
}
39+
3140
// Contains returns true if the format contains the substring
3241
func (f Format) Contains(sub string) bool {
3342
return strings.Contains(string(f), sub)
@@ -50,10 +59,12 @@ type Context struct {
5059

5160
func (c *Context) preFormat() {
5261
c.finalFormat = string(c.Format)
53-
5462
// TODO: handle this in the Format type
55-
if c.Format.IsTable() {
63+
switch {
64+
case c.Format.IsTable():
5665
c.finalFormat = c.finalFormat[len(TableFormatKey):]
66+
case c.Format.IsJSON():
67+
c.finalFormat = jsonFormat
5768
}
5869

5970
c.finalFormat = strings.Trim(c.finalFormat, " ")
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package formatter
2+
3+
import (
4+
"bytes"
5+
"testing"
6+
7+
"gotest.tools/v3/assert"
8+
)
9+
10+
func TestFormat(t *testing.T) {
11+
f := Format("json")
12+
assert.Assert(t, f.IsJSON())
13+
assert.Assert(t, !f.IsTable())
14+
15+
f = Format("table")
16+
assert.Assert(t, !f.IsJSON())
17+
assert.Assert(t, f.IsTable())
18+
19+
f = Format("other")
20+
assert.Assert(t, !f.IsJSON())
21+
assert.Assert(t, !f.IsTable())
22+
}
23+
24+
type fakeSubContext struct {
25+
Name string
26+
}
27+
28+
func (f fakeSubContext) FullHeader() interface{} {
29+
return map[string]string{"Name": "NAME"}
30+
}
31+
32+
func TestContext(t *testing.T) {
33+
testCases := []struct {
34+
name string
35+
format string
36+
expected string
37+
}{
38+
{
39+
name: "json format",
40+
format: JSONFormatKey,
41+
expected: `{"Name":"test"}
42+
`,
43+
},
44+
{
45+
name: "table format",
46+
format: `table {{.Name}}`,
47+
expected: `NAME
48+
test
49+
`,
50+
},
51+
}
52+
for _, tc := range testCases {
53+
t.Run(tc.name, func(t *testing.T) {
54+
buf := bytes.NewBuffer(nil)
55+
ctx := Context{
56+
Format: Format(tc.format),
57+
Output: buf,
58+
}
59+
subContext := fakeSubContext{Name: "test"}
60+
subFormat := func(f func(sub SubContext) error) error {
61+
return f(subContext)
62+
}
63+
err := ctx.Write(&subContext, subFormat)
64+
assert.NilError(t, err)
65+
assert.Equal(t, buf.String(), tc.expected)
66+
})
67+
}
68+
}

0 commit comments

Comments
 (0)