mirror of
https://github.com/docker/compose.git
synced 2025-07-21 12:44:54 +02:00
Merge pull request #854 from gtardif/retro-compat-context-json-list
Revert breaking change on `docker context ls —format “{{ json . }}`
This commit is contained in:
commit
24b40e1a8d
@ -70,7 +70,7 @@ func runList(cmd *cobra.Command, opts lsOpts) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
format := strings.ToLower(strings.ReplaceAll(opts.format, " ", ""))
|
format := strings.ToLower(strings.ReplaceAll(opts.format, " ", ""))
|
||||||
if format != "" && format != formatter.JSON && format != formatter.PRETTY && format != formatter.TemplateJSON {
|
if format != "" && format != formatter.JSON && format != formatter.PRETTY && format != formatter.TemplateLegacyJSON {
|
||||||
mobycli.Exec(cmd.Root())
|
mobycli.Exec(cmd.Root())
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -94,9 +94,12 @@ func runList(cmd *cobra.Command, opts lsOpts) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if opts.json || format == formatter.JSON || format == formatter.TemplateJSON {
|
if opts.json || format == formatter.JSON {
|
||||||
opts.format = formatter.JSON
|
opts.format = formatter.JSON
|
||||||
}
|
}
|
||||||
|
if format == formatter.TemplateLegacyJSON {
|
||||||
|
opts.format = formatter.TemplateLegacyJSON
|
||||||
|
}
|
||||||
|
|
||||||
view := viewFromContextList(contexts, currentContext)
|
view := viewFromContextList(contexts, currentContext)
|
||||||
return formatter.Print(view, opts.format, os.Stdout,
|
return formatter.Print(view, opts.format, os.Stdout,
|
||||||
@ -108,7 +111,7 @@ func runList(cmd *cobra.Command, opts lsOpts) error {
|
|||||||
}
|
}
|
||||||
_, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\n",
|
_, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\n",
|
||||||
contextName,
|
contextName,
|
||||||
c.Type,
|
c.ContextType,
|
||||||
c.Description,
|
c.Description,
|
||||||
c.DockerEndpoint,
|
c.DockerEndpoint,
|
||||||
c.KubernetesEndpoint,
|
c.KubernetesEndpoint,
|
||||||
@ -141,7 +144,7 @@ type contextView struct {
|
|||||||
Description string
|
Description string
|
||||||
DockerEndpoint string
|
DockerEndpoint string
|
||||||
KubernetesEndpoint string
|
KubernetesEndpoint string
|
||||||
Type string
|
ContextType string
|
||||||
Name string
|
Name string
|
||||||
StackOrchestrator string
|
StackOrchestrator string
|
||||||
}
|
}
|
||||||
@ -155,7 +158,7 @@ func viewFromContextList(contextList []*store.DockerContext, currentContext stri
|
|||||||
DockerEndpoint: getEndpoint("docker", c.Endpoints),
|
DockerEndpoint: getEndpoint("docker", c.Endpoints),
|
||||||
KubernetesEndpoint: getEndpoint("kubernetes", c.Endpoints),
|
KubernetesEndpoint: getEndpoint("kubernetes", c.Endpoints),
|
||||||
Name: c.Name,
|
Name: c.Name,
|
||||||
Type: c.Type(),
|
ContextType: c.Type(),
|
||||||
StackOrchestrator: c.Metadata.StackOrchestrator,
|
StackOrchestrator: c.Metadata.StackOrchestrator,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -59,7 +59,7 @@ func runVersion(cmd *cobra.Command) {
|
|||||||
case formatter.PRETTY, "":
|
case formatter.PRETTY, "":
|
||||||
versionString = strings.Replace(getOutFromMoby(cmd, fixedPrettyArgs(os.Args[1:])...),
|
versionString = strings.Replace(getOutFromMoby(cmd, fixedPrettyArgs(os.Args[1:])...),
|
||||||
"\n Version:", "\n Cloud integration: "+displayedVersion+"\n Version:", 1)
|
"\n Version:", "\n Cloud integration: "+displayedVersion+"\n Version:", 1)
|
||||||
case formatter.JSON, formatter.TemplateJSON: // Try to catch full JSON formats
|
case formatter.JSON, formatter.TemplateLegacyJSON: // Try to catch full JSON formats
|
||||||
versionString = strings.Replace(getOutFromMoby(cmd, fixedJSONArgs(os.Args[1:])...),
|
versionString = strings.Replace(getOutFromMoby(cmd, fixedJSONArgs(os.Args[1:])...),
|
||||||
`"Version":`, fmt.Sprintf(`"CloudIntegration":%q,"Version":`, displayedVersion), 1)
|
`"Version":`, fmt.Sprintf(`"CloudIntegration":%q,"Version":`, displayedVersion), 1)
|
||||||
default:
|
default:
|
||||||
|
@ -19,8 +19,8 @@ package formatter
|
|||||||
const (
|
const (
|
||||||
// JSON is the constant for Json formats on list commands
|
// JSON is the constant for Json formats on list commands
|
||||||
JSON = "json"
|
JSON = "json"
|
||||||
// TemplateJSON the legacy json formatting value using go template
|
// TemplateLegacyJSON the legacy json formatting value using go template
|
||||||
TemplateJSON = "{{json.}}"
|
TemplateLegacyJSON = "{{json.}}"
|
||||||
// PRETTY is the constant for default formats on list commands
|
// PRETTY is the constant for default formats on list commands
|
||||||
PRETTY = "pretty"
|
PRETTY = "pretty"
|
||||||
)
|
)
|
||||||
|
@ -32,6 +32,25 @@ func Print(toJSON interface{}, format string, outWriter io.Writer, writerFn func
|
|||||||
switch strings.ToLower(format) {
|
switch strings.ToLower(format) {
|
||||||
case PRETTY, "":
|
case PRETTY, "":
|
||||||
return PrintPrettySection(outWriter, writerFn, headers...)
|
return PrintPrettySection(outWriter, writerFn, headers...)
|
||||||
|
case TemplateLegacyJSON:
|
||||||
|
switch reflect.TypeOf(toJSON).Kind() {
|
||||||
|
case reflect.Slice:
|
||||||
|
s := reflect.ValueOf(toJSON)
|
||||||
|
for i := 0; i < s.Len(); i++ {
|
||||||
|
obj := s.Index(i).Interface()
|
||||||
|
outJSON, err := ToJSON(obj, "", "")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, _ = fmt.Fprint(outWriter, outJSON)
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
outJSON, err := ToStandardJSON(toJSON)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, _ = fmt.Fprintln(outWriter, outJSON)
|
||||||
|
}
|
||||||
case JSON:
|
case JSON:
|
||||||
switch reflect.TypeOf(toJSON).Kind() {
|
switch reflect.TypeOf(toJSON).Kind() {
|
||||||
case reflect.Slice:
|
case reflect.Slice:
|
||||||
|
@ -58,5 +58,16 @@ func TestPrint(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}, "NAME", "STATUS"))
|
}, "NAME", "STATUS"))
|
||||||
assert.Equal(t, b.String(), `[{"Name":"myName1","Status":"myStatus1"},{"Name":"myName2","Status":"myStatus2"}]
|
assert.Equal(t, b.String(), `[{"Name":"myName1","Status":"myStatus1"},{"Name":"myName2","Status":"myStatus2"}]
|
||||||
|
`)
|
||||||
|
|
||||||
|
b.Reset()
|
||||||
|
assert.NilError(t, Print(testList, TemplateLegacyJSON, b, func(w io.Writer) {
|
||||||
|
for _, t := range testList {
|
||||||
|
_, _ = fmt.Fprintf(w, "%s\t%s\n", t.Name, t.Status)
|
||||||
|
}
|
||||||
|
}, "NAME", "STATUS"))
|
||||||
|
json := b.String()
|
||||||
|
assert.Equal(t, json, `{"Name":"myName1","Status":"myStatus1"}
|
||||||
|
{"Name":"myName2","Status":"myStatus2"}
|
||||||
`)
|
`)
|
||||||
}
|
}
|
||||||
|
@ -86,7 +86,7 @@ func TestContextDefault(t *testing.T) {
|
|||||||
golden.Assert(t, res.Stdout(), GoldenFile("ls-out-json"))
|
golden.Assert(t, res.Stdout(), GoldenFile("ls-out-json"))
|
||||||
|
|
||||||
res = c.RunDockerCmd("context", "ls", "--format", "{{ json . }}")
|
res = c.RunDockerCmd("context", "ls", "--format", "{{ json . }}")
|
||||||
golden.Assert(t, res.Stdout(), GoldenFile("ls-out-json"))
|
golden.Assert(t, res.Stdout(), GoldenFile("ls-out-legacy-json"))
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("inspect", func(t *testing.T) {
|
t.Run("inspect", func(t *testing.T) {
|
||||||
|
@ -1 +1 @@
|
|||||||
[{"Current":true,"Description":"Current DOCKER_HOST based configuration","DockerEndpoint":"npipe:////./pipe/docker_engine","KubernetesEndpoint":"","Type":"moby","Name":"default","StackOrchestrator":"swarm"}]
|
[{"Current":true,"Description":"Current DOCKER_HOST based configuration","DockerEndpoint":"npipe:////./pipe/docker_engine","KubernetesEndpoint":"","ContextType":"moby","Name":"default","StackOrchestrator":"swarm"}]
|
||||||
|
2
tests/e2e/testdata/ls-out-json.golden
vendored
2
tests/e2e/testdata/ls-out-json.golden
vendored
@ -1 +1 @@
|
|||||||
[{"Current":true,"Description":"Current DOCKER_HOST based configuration","DockerEndpoint":"unix:///var/run/docker.sock","KubernetesEndpoint":"","Type":"moby","Name":"default","StackOrchestrator":"swarm"}]
|
[{"Current":true,"Description":"Current DOCKER_HOST based configuration","DockerEndpoint":"unix:///var/run/docker.sock","KubernetesEndpoint":"","ContextType":"moby","Name":"default","StackOrchestrator":"swarm"}]
|
||||||
|
1
tests/e2e/testdata/ls-out-legacy-json-windows.golden
vendored
Normal file
1
tests/e2e/testdata/ls-out-legacy-json-windows.golden
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
{"Current":true,"Description":"Current DOCKER_HOST based configuration","DockerEndpoint":"npipe:////./pipe/docker_engine","KubernetesEndpoint":"","ContextType":"moby","Name":"default","StackOrchestrator":"swarm"}
|
1
tests/e2e/testdata/ls-out-legacy-json.golden
vendored
Normal file
1
tests/e2e/testdata/ls-out-legacy-json.golden
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
{"Current":true,"Description":"Current DOCKER_HOST based configuration","DockerEndpoint":"unix:///var/run/docker.sock","KubernetesEndpoint":"","ContextType":"moby","Name":"default","StackOrchestrator":"swarm"}
|
Loading…
x
Reference in New Issue
Block a user