Merge pull request #724 from gtardif/json_context_format

Allow `docker context ls —formatter {{json.}}` to not delegate to Moby and include context type in the json
This commit is contained in:
Guillaume Tardif 2020-10-06 18:12:54 +02:00 committed by GitHub
commit 6fa93db021
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 3 deletions

View File

@ -69,7 +69,8 @@ func runList(cmd *cobra.Command, opts lsOpts) error {
if err != nil {
return err
}
if opts.format != "" && opts.format != formatter.JSON && opts.format != formatter.PRETTY {
format := strings.ToLower(strings.ReplaceAll(opts.format, " ", ""))
if format != "" && format != formatter.JSON && format != formatter.PRETTY && format != formatter.TemplateJSON {
mobycli.Exec(cmd.Root())
return nil
}
@ -93,7 +94,7 @@ func runList(cmd *cobra.Command, opts lsOpts) error {
return nil
}
if opts.json {
if opts.json || format == formatter.JSON || format == formatter.TemplateJSON {
opts.format = formatter.JSON
}

View File

@ -58,7 +58,7 @@ func runVersion(cmd *cobra.Command, version string) {
case formatter.PRETTY, "":
versionString = strings.Replace(getOutFromMoby(cmd, fixedPrettyArgs(os.Args[1:])...),
"\n Version:", "\n Cloud integration: "+displayedVersion+"\n Version:", 1)
case formatter.JSON, "{{json.}}": // Try to catch full JSON formats
case formatter.JSON, formatter.TemplateJSON: // Try to catch full JSON formats
versionString = strings.Replace(getOutFromMoby(cmd, fixedJSONArgs(os.Args[1:])...),
`"Version":`, fmt.Sprintf(`"CloudIntegration":%q,"Version":`, displayedVersion), 1)
default:

View File

@ -19,6 +19,8 @@ package formatter
const (
// JSON is the constant for Json formats on list commands
JSON = "json"
// TemplateJSON the legacy json formatting value using go template
TemplateJSON = "{{json.}}"
// PRETTY is the constant for default formats on list commands
PRETTY = "pretty"
)

View File

@ -81,6 +81,12 @@ func TestContextDefault(t *testing.T) {
res = c.RunDockerCmd("context", "ls", "--format", "json")
golden.Assert(t, res.Stdout(), GoldenFile("ls-out-json"))
res = c.RunDockerCmd("context", "ls", "--json")
golden.Assert(t, res.Stdout(), GoldenFile("ls-out-json"))
res = c.RunDockerCmd("context", "ls", "--format", "{{ json . }}")
golden.Assert(t, res.Stdout(), GoldenFile("ls-out-json"))
})
t.Run("inspect", func(t *testing.T) {
@ -422,6 +428,13 @@ func TestVersion(t *testing.T) {
res.Assert(t, icmd.Expected{Out: `"Client":`})
})
t.Run("format legacy", func(t *testing.T) {
res := c.RunDockerCmd("version", "-f", "{{ json .Client }}")
res.Assert(t, icmd.Expected{Out: `"DefaultAPIVersion":`})
res = c.RunDockerCmd("version", "--format", "{{ json .Server }}")
res.Assert(t, icmd.Expected{Out: `"KernelVersion":`})
})
t.Run("format cloud integration", func(t *testing.T) {
res := c.RunDockerCmd("version", "-f", "pretty")
res.Assert(t, icmd.Expected{Out: `Cloud integration:`})