Skip to content

Commit 24b40e1

Browse files
authored
Merge pull request docker-archive#854 from gtardif/retro-compat-context-json-list
Revert breaking change on `docker context ls —format “{{ json . }}`
2 parents 8104611 + c3cc543 commit 24b40e1

File tree

10 files changed

+46
-11
lines changed

10 files changed

+46
-11
lines changed

cli/cmd/context/ls.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func runList(cmd *cobra.Command, opts lsOpts) error {
7070
return err
7171
}
7272
format := strings.ToLower(strings.ReplaceAll(opts.format, " ", ""))
73-
if format != "" && format != formatter.JSON && format != formatter.PRETTY && format != formatter.TemplateJSON {
73+
if format != "" && format != formatter.JSON && format != formatter.PRETTY && format != formatter.TemplateLegacyJSON {
7474
mobycli.Exec(cmd.Root())
7575
return nil
7676
}
@@ -94,9 +94,12 @@ func runList(cmd *cobra.Command, opts lsOpts) error {
9494
return nil
9595
}
9696

97-
if opts.json || format == formatter.JSON || format == formatter.TemplateJSON {
97+
if opts.json || format == formatter.JSON {
9898
opts.format = formatter.JSON
9999
}
100+
if format == formatter.TemplateLegacyJSON {
101+
opts.format = formatter.TemplateLegacyJSON
102+
}
100103

101104
view := viewFromContextList(contexts, currentContext)
102105
return formatter.Print(view, opts.format, os.Stdout,
@@ -108,7 +111,7 @@ func runList(cmd *cobra.Command, opts lsOpts) error {
108111
}
109112
_, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\n",
110113
contextName,
111-
c.Type,
114+
c.ContextType,
112115
c.Description,
113116
c.DockerEndpoint,
114117
c.KubernetesEndpoint,
@@ -141,7 +144,7 @@ type contextView struct {
141144
Description string
142145
DockerEndpoint string
143146
KubernetesEndpoint string
144-
Type string
147+
ContextType string
145148
Name string
146149
StackOrchestrator string
147150
}
@@ -155,7 +158,7 @@ func viewFromContextList(contextList []*store.DockerContext, currentContext stri
155158
DockerEndpoint: getEndpoint("docker", c.Endpoints),
156159
KubernetesEndpoint: getEndpoint("kubernetes", c.Endpoints),
157160
Name: c.Name,
158-
Type: c.Type(),
161+
ContextType: c.Type(),
159162
StackOrchestrator: c.Metadata.StackOrchestrator,
160163
}
161164
}

cli/cmd/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func runVersion(cmd *cobra.Command) {
5959
case formatter.PRETTY, "":
6060
versionString = strings.Replace(getOutFromMoby(cmd, fixedPrettyArgs(os.Args[1:])...),
6161
"\n Version:", "\n Cloud integration: "+displayedVersion+"\n Version:", 1)
62-
case formatter.JSON, formatter.TemplateJSON: // Try to catch full JSON formats
62+
case formatter.JSON, formatter.TemplateLegacyJSON: // Try to catch full JSON formats
6363
versionString = strings.Replace(getOutFromMoby(cmd, fixedJSONArgs(os.Args[1:])...),
6464
`"Version":`, fmt.Sprintf(`"CloudIntegration":%q,"Version":`, displayedVersion), 1)
6565
default:

formatter/consts.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ package formatter
1919
const (
2020
// JSON is the constant for Json formats on list commands
2121
JSON = "json"
22-
// TemplateJSON the legacy json formatting value using go template
23-
TemplateJSON = "{{json.}}"
22+
// TemplateLegacyJSON the legacy json formatting value using go template
23+
TemplateLegacyJSON = "{{json.}}"
2424
// PRETTY is the constant for default formats on list commands
2525
PRETTY = "pretty"
2626
)

formatter/formatter.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,25 @@ func Print(toJSON interface{}, format string, outWriter io.Writer, writerFn func
3232
switch strings.ToLower(format) {
3333
case PRETTY, "":
3434
return PrintPrettySection(outWriter, writerFn, headers...)
35+
case TemplateLegacyJSON:
36+
switch reflect.TypeOf(toJSON).Kind() {
37+
case reflect.Slice:
38+
s := reflect.ValueOf(toJSON)
39+
for i := 0; i < s.Len(); i++ {
40+
obj := s.Index(i).Interface()
41+
outJSON, err := ToJSON(obj, "", "")
42+
if err != nil {
43+
return err
44+
}
45+
_, _ = fmt.Fprint(outWriter, outJSON)
46+
}
47+
default:
48+
outJSON, err := ToStandardJSON(toJSON)
49+
if err != nil {
50+
return err
51+
}
52+
_, _ = fmt.Fprintln(outWriter, outJSON)
53+
}
3554
case JSON:
3655
switch reflect.TypeOf(toJSON).Kind() {
3756
case reflect.Slice:

formatter/formatter_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,16 @@ func TestPrint(t *testing.T) {
5858
}
5959
}, "NAME", "STATUS"))
6060
assert.Equal(t, b.String(), `[{"Name":"myName1","Status":"myStatus1"},{"Name":"myName2","Status":"myStatus2"}]
61+
`)
62+
63+
b.Reset()
64+
assert.NilError(t, Print(testList, TemplateLegacyJSON, b, func(w io.Writer) {
65+
for _, t := range testList {
66+
_, _ = fmt.Fprintf(w, "%s\t%s\n", t.Name, t.Status)
67+
}
68+
}, "NAME", "STATUS"))
69+
json := b.String()
70+
assert.Equal(t, json, `{"Name":"myName1","Status":"myStatus1"}
71+
{"Name":"myName2","Status":"myStatus2"}
6172
`)
6273
}

tests/e2e/e2e_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func TestContextDefault(t *testing.T) {
8686
golden.Assert(t, res.Stdout(), GoldenFile("ls-out-json"))
8787

8888
res = c.RunDockerCmd("context", "ls", "--format", "{{ json . }}")
89-
golden.Assert(t, res.Stdout(), GoldenFile("ls-out-json"))
89+
golden.Assert(t, res.Stdout(), GoldenFile("ls-out-legacy-json"))
9090
})
9191

9292
t.Run("inspect", func(t *testing.T) {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[{"Current":true,"Description":"Current DOCKER_HOST based configuration","DockerEndpoint":"npipe:////./pipe/docker_engine","KubernetesEndpoint":"","Type":"moby","Name":"default","StackOrchestrator":"swarm"}]
1+
[{"Current":true,"Description":"Current DOCKER_HOST based configuration","DockerEndpoint":"npipe:////./pipe/docker_engine","KubernetesEndpoint":"","ContextType":"moby","Name":"default","StackOrchestrator":"swarm"}]

tests/e2e/testdata/ls-out-json.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[{"Current":true,"Description":"Current DOCKER_HOST based configuration","DockerEndpoint":"unix:///var/run/docker.sock","KubernetesEndpoint":"","Type":"moby","Name":"default","StackOrchestrator":"swarm"}]
1+
[{"Current":true,"Description":"Current DOCKER_HOST based configuration","DockerEndpoint":"unix:///var/run/docker.sock","KubernetesEndpoint":"","ContextType":"moby","Name":"default","StackOrchestrator":"swarm"}]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"Current":true,"Description":"Current DOCKER_HOST based configuration","DockerEndpoint":"npipe:////./pipe/docker_engine","KubernetesEndpoint":"","ContextType":"moby","Name":"default","StackOrchestrator":"swarm"}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"Current":true,"Description":"Current DOCKER_HOST based configuration","DockerEndpoint":"unix:///var/run/docker.sock","KubernetesEndpoint":"","ContextType":"moby","Name":"default","StackOrchestrator":"swarm"}

0 commit comments

Comments
 (0)