Skip to content

Commit 3304c49

Browse files
authored
Merge pull request #2936 from silvin-lubecki/format-json
Add --format=json to inspect and list commands
2 parents 6745f62 + 9c0234b commit 3304c49

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+480
-95
lines changed

cli/command/config/inspect.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/docker/cli/cli"
99
"github.com/docker/cli/cli/command"
1010
"github.com/docker/cli/cli/command/formatter"
11+
flagsHelper "github.com/docker/cli/cli/flags"
1112
"github.com/spf13/cobra"
1213
)
1314

@@ -30,7 +31,7 @@ func newConfigInspectCommand(dockerCli command.Cli) *cobra.Command {
3031
},
3132
}
3233

33-
cmd.Flags().StringVarP(&opts.Format, "format", "f", "", "Format the output using the given Go template")
34+
cmd.Flags().StringVarP(&opts.Format, "format", "f", "", flagsHelper.InspectFormatHelp)
3435
cmd.Flags().BoolVar(&opts.Pretty, "pretty", false, "Print the information in a human friendly format")
3536
return cmd
3637
}

cli/command/config/ls.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/docker/cli/cli"
88
"github.com/docker/cli/cli/command"
99
"github.com/docker/cli/cli/command/formatter"
10+
flagsHelper "github.com/docker/cli/cli/flags"
1011
"github.com/docker/cli/opts"
1112
"github.com/docker/docker/api/types"
1213
"github.com/fvbommel/sortorder"
@@ -35,7 +36,7 @@ func newConfigListCommand(dockerCli command.Cli) *cobra.Command {
3536

3637
flags := cmd.Flags()
3738
flags.BoolVarP(&listOpts.Quiet, "quiet", "q", false, "Only display IDs")
38-
flags.StringVarP(&listOpts.Format, "format", "", "", "Pretty-print configs using a Go template")
39+
flags.StringVarP(&listOpts.Format, "format", "", "", flagsHelper.FormatHelp)
3940
flags.VarP(&listOpts.Filter, "filter", "f", "Filter output based on conditions provided")
4041

4142
return cmd

cli/command/container/inspect.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/docker/cli/cli"
77
"github.com/docker/cli/cli/command"
88
"github.com/docker/cli/cli/command/inspect"
9+
flagsHelper "github.com/docker/cli/cli/flags"
910
"github.com/spf13/cobra"
1011
)
1112

@@ -30,7 +31,7 @@ func newInspectCommand(dockerCli command.Cli) *cobra.Command {
3031
}
3132

3233
flags := cmd.Flags()
33-
flags.StringVarP(&opts.format, "format", "f", "", "Format the output using the given Go template")
34+
flags.StringVarP(&opts.format, "format", "f", "", flagsHelper.InspectFormatHelp)
3435
flags.BoolVarP(&opts.size, "size", "s", false, "Display total file sizes")
3536

3637
return cmd

cli/command/container/list.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/docker/cli/cli"
88
"github.com/docker/cli/cli/command"
99
"github.com/docker/cli/cli/command/formatter"
10+
flagsHelper "github.com/docker/cli/cli/flags"
1011
"github.com/docker/cli/opts"
1112
"github.com/docker/cli/templates"
1213
"github.com/docker/docker/api/types"
@@ -46,7 +47,7 @@ func NewPsCommand(dockerCli command.Cli) *cobra.Command {
4647
flags.BoolVar(&options.noTrunc, "no-trunc", false, "Don't truncate output")
4748
flags.BoolVarP(&options.nLatest, "latest", "l", false, "Show the latest created container (includes all states)")
4849
flags.IntVarP(&options.last, "last", "n", -1, "Show n last created containers (includes all states)")
49-
flags.StringVarP(&options.format, "format", "", "", "Pretty-print containers using a Go template")
50+
flags.StringVarP(&options.format, "format", "", "", flagsHelper.FormatHelp)
5051
flags.VarP(&options.filter, "filter", "f", "Filter output based on conditions provided")
5152

5253
return cmd
@@ -80,7 +81,6 @@ func buildContainerListOptions(opts *psOptions) (*types.ContainerListOptions, er
8081
// Only requesting container size information when needed is an optimization,
8182
// because calculating the size is a costly operation.
8283
tmpl, err := templates.NewParse("", opts.format)
83-
8484
if err != nil {
8585
return nil, errors.Wrap(err, "failed to parse template")
8686
}

cli/command/container/stats.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/docker/cli/cli"
1212
"github.com/docker/cli/cli/command"
1313
"github.com/docker/cli/cli/command/formatter"
14+
flagsHelper "github.com/docker/cli/cli/flags"
1415
"github.com/docker/docker/api/types"
1516
"github.com/docker/docker/api/types/events"
1617
"github.com/docker/docker/api/types/filters"
@@ -44,7 +45,7 @@ func NewStatsCommand(dockerCli command.Cli) *cobra.Command {
4445
flags.BoolVarP(&opts.all, "all", "a", false, "Show all containers (default shows just running)")
4546
flags.BoolVar(&opts.noStream, "no-stream", false, "Disable streaming stats and only pull the first result")
4647
flags.BoolVar(&opts.noTrunc, "no-trunc", false, "Do not truncate output")
47-
flags.StringVar(&opts.format, "format", "", "Pretty-print images using a Go template")
48+
flags.StringVar(&opts.format, "format", "", flagsHelper.FormatHelp)
4849
return cmd
4950
}
5051

cli/command/context/inspect.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/docker/cli/cli/command"
77
"github.com/docker/cli/cli/command/inspect"
88
"github.com/docker/cli/cli/context/store"
9+
flagsHelper "github.com/docker/cli/cli/flags"
910
"github.com/spf13/cobra"
1011
)
1112

@@ -34,7 +35,7 @@ func newInspectCommand(dockerCli command.Cli) *cobra.Command {
3435
}
3536

3637
flags := cmd.Flags()
37-
flags.StringVarP(&opts.format, "format", "f", "", "Format the output using the given Go template")
38+
flags.StringVarP(&opts.format, "format", "f", "", flagsHelper.InspectFormatHelp)
3839
return cmd
3940
}
4041

cli/command/context/list.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/docker/cli/cli/command"
1010
"github.com/docker/cli/cli/command/formatter"
1111
"github.com/docker/cli/cli/context/docker"
12+
flagsHelper "github.com/docker/cli/cli/flags"
1213
"github.com/fvbommel/sortorder"
1314
"github.com/spf13/cobra"
1415
)
@@ -31,7 +32,7 @@ func newListCommand(dockerCli command.Cli) *cobra.Command {
3132
}
3233

3334
flags := cmd.Flags()
34-
flags.StringVar(&opts.format, "format", "", "Pretty-print contexts using a Go template")
35+
flags.StringVar(&opts.format, "format", "", flagsHelper.FormatHelp)
3536
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Only show context names")
3637
return cmd
3738
}

cli/command/formatter/formatter.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ const (
1616
TableFormatKey = "table"
1717
RawFormatKey = "raw"
1818
PrettyFormatKey = "pretty"
19+
JSONFormatKey = "json"
1920

2021
DefaultQuietFormat = "{{.ID}}"
22+
jsonFormat = "{{json .}}"
2123
)
2224

2325
// Format is the format string rendered using the Context
@@ -28,6 +30,11 @@ func (f Format) IsTable() bool {
2830
return strings.HasPrefix(string(f), TableFormatKey)
2931
}
3032

33+
// IsJSON returns true if the format is the json format
34+
func (f Format) IsJSON() bool {
35+
return string(f) == JSONFormatKey
36+
}
37+
3138
// Contains returns true if the format contains the substring
3239
func (f Format) Contains(sub string) bool {
3340
return strings.Contains(string(f), sub)
@@ -50,10 +57,12 @@ type Context struct {
5057

5158
func (c *Context) preFormat() {
5259
c.finalFormat = string(c.Format)
53-
5460
// TODO: handle this in the Format type
55-
if c.Format.IsTable() {
61+
switch {
62+
case c.Format.IsTable():
5663
c.finalFormat = c.finalFormat[len(TableFormatKey):]
64+
case c.Format.IsJSON():
65+
c.finalFormat = jsonFormat
5766
}
5867

5968
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+
}

cli/command/image/history.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/docker/cli/cli"
77
"github.com/docker/cli/cli/command"
88
"github.com/docker/cli/cli/command/formatter"
9+
flagsHelper "github.com/docker/cli/cli/flags"
910
"github.com/spf13/cobra"
1011
)
1112

@@ -37,7 +38,7 @@ func NewHistoryCommand(dockerCli command.Cli) *cobra.Command {
3738
flags.BoolVarP(&opts.human, "human", "H", true, "Print sizes and dates in human readable format")
3839
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Only show image IDs")
3940
flags.BoolVar(&opts.noTrunc, "no-trunc", false, "Don't truncate output")
40-
flags.StringVar(&opts.format, "format", "", "Pretty-print images using a Go template")
41+
flags.StringVar(&opts.format, "format", "", flagsHelper.FormatHelp)
4142

4243
return cmd
4344
}

0 commit comments

Comments
 (0)