Jelajahi Sumber

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
Guillaume Tardif 5 tahun lalu
induk
melakukan
6fa93db021
4 mengubah file dengan 19 tambahan dan 3 penghapusan
  1. 3 2
      cli/cmd/context/ls.go
  2. 1 1
      cli/cmd/version.go
  3. 2 0
      formatter/consts.go
  4. 13 0
      tests/e2e/e2e_test.go

+ 3 - 2
cli/cmd/context/ls.go

@@ -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
 	}
 

+ 1 - 1
cli/cmd/version.go

@@ -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:

+ 2 - 0
formatter/consts.go

@@ -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"
 )

+ 13 - 0
tests/e2e/e2e_test.go

@@ -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:`})