From 14bc441bc34e8aa321b5bd0e3429ff577e073cb3 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Tue, 6 May 2025 18:41:48 +0200 Subject: [PATCH 1/3] Fix config --variables not honoring the --format flag When providing the --variables with --format flag, the current implementation always printed in human readable form. This patch correctly add the missing format in the formatter.Print function, making the commands behave as an user would expect. Example: `config --variables --format json` Signed-off-by: Alessio Perugini --- cmd/compose/config.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/cmd/compose/config.go b/cmd/compose/config.go index e3b85f43ffc..e1121d3a8ad 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.Println(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) } From abecf66837b79f89920b58a019ee4b9569859787 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Tue, 6 May 2025 19:42:25 +0200 Subject: [PATCH 2/3] e2e: add tests Signed-off-by: Alessio Perugini --- cmd/compose/config.go | 2 +- pkg/e2e/config_test.go | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/cmd/compose/config.go b/cmd/compose/config.go index e1121d3a8ad..a30c8fe7073 100644 --- a/cmd/compose/config.go +++ b/cmd/compose/config.go @@ -416,7 +416,7 @@ func runVariables(ctx context.Context, dockerCli command.Cli, opts configOptions if err != nil { return err } - fmt.Println(string(result)) + fmt.Print(string(result)) return nil } 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`}) + }) } From 7558912b6ae13ea5723d3b9dfb3879fc731c73c9 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Tue, 6 May 2025 20:54:30 +0200 Subject: [PATCH 3/3] docs: regenerate Signed-off-by: Alessio Perugini --- docs/reference/compose_config.md | 2 +- docs/reference/docker_compose_config.yaml | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) 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