diff --git a/cmd/compose/config.go b/cmd/compose/config.go index e3b85f43ffc..a30c8fe7073 100644 --- a/cmd/compose/config.go +++ b/cmd/compose/config.go @@ -124,12 +124,15 @@ func configCommand(p *ProjectOptions, dockerCli command.Cli) *cobra.Command { return runEnvironment(ctx, dockerCli, opts, args) } + if opts.Format == "" { + opts.Format = "yaml" + } return runConfig(ctx, dockerCli, opts, args) }), ValidArgsFunction: completeServiceNames(dockerCli, p), } flags := cmd.Flags() - flags.StringVar(&opts.Format, "format", "yaml", "Format the output. Values: [yaml | json]") + flags.StringVar(&opts.Format, "format", "", "Format the output. Values: [yaml | json]") flags.BoolVar(&opts.resolveImageDigests, "resolve-image-digests", false, "Pin image tags to digests") flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Only validate the configuration, don't print anything") flags.BoolVar(&opts.noInterpolate, "no-interpolate", false, "Don't interpolate environment variables") @@ -408,7 +411,16 @@ func runVariables(ctx context.Context, dockerCli command.Cli, opts configOptions variables := template.ExtractVariables(model, template.DefaultPattern) - return formatter.Print(variables, "", dockerCli.Out(), func(w io.Writer) { + if opts.Format == "yaml" { + result, err := yaml.Marshal(variables) + if err != nil { + return err + } + fmt.Print(string(result)) + return nil + } + + return formatter.Print(variables, opts.Format, dockerCli.Out(), func(w io.Writer) { for name, variable := range variables { _, _ = fmt.Fprintf(w, "%s\t%t\t%s\t%s\n", name, variable.Required, variable.DefaultValue, variable.PresenceValue) } diff --git a/docs/reference/compose_config.md b/docs/reference/compose_config.md index 9e87efd29cb..78c1835a527 100644 --- a/docs/reference/compose_config.md +++ b/docs/reference/compose_config.md @@ -15,7 +15,7 @@ the canonical format. |:--------------------------|:---------|:--------|:----------------------------------------------------------------------------| | `--dry-run` | `bool` | | Execute command in dry run mode | | `--environment` | `bool` | | Print environment used for interpolation. | -| `--format` | `string` | `yaml` | Format the output. Values: [yaml \| json] | +| `--format` | `string` | | Format the output. Values: [yaml \| json] | | `--hash` | `string` | | Print the service config hash, one per line. | | `--images` | `bool` | | Print the image names, one per line. | | `--no-consistency` | `bool` | | Don't check model consistency - warning: may produce invalid Compose output | diff --git a/docs/reference/docker_compose_config.yaml b/docs/reference/docker_compose_config.yaml index 15b1e7dc398..7ec479b2000 100644 --- a/docs/reference/docker_compose_config.yaml +++ b/docs/reference/docker_compose_config.yaml @@ -21,7 +21,6 @@ options: swarm: false - option: format value_type: string - default_value: yaml description: 'Format the output. Values: [yaml | json]' deprecated: false hidden: false diff --git a/pkg/e2e/config_test.go b/pkg/e2e/config_test.go index 6fef7bc02d8..15d3e3d932a 100644 --- a/pkg/e2e/config_test.go +++ b/pkg/e2e/config_test.go @@ -46,4 +46,25 @@ func TestLocalComposeConfig(t *testing.T) { res := c.RunDockerComposeCmd(t, "-f", "./fixtures/config/compose.yaml", "--project-name", projectName, "config", "--no-interpolate") res.Assert(t, icmd.Expected{Out: `- ${PORT:-8080}:80`}) }) + + t.Run("--variables --format json", func(t *testing.T) { + res := c.RunDockerComposeCmd(t, "-f", "./fixtures/config/compose.yaml", "--project-name", projectName, "config", "--variables", "--format", "json") + res.Assert(t, icmd.Expected{Out: `{ + "PORT": { + "Name": "PORT", + "DefaultValue": "8080", + "PresenceValue": "", + "Required": false + } +}`}) + }) + + t.Run("--variables --format yaml", func(t *testing.T) { + res := c.RunDockerComposeCmd(t, "-f", "./fixtures/config/compose.yaml", "--project-name", projectName, "config", "--variables", "--format", "yaml") + res.Assert(t, icmd.Expected{Out: `PORT: + name: PORT + defaultvalue: "8080" + presencevalue: "" + required: false`}) + }) }