From d6e3fa6d7432e061be1d22147aa06f8b7aeea6af Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Tue, 6 May 2025 18:41:48 +0200 Subject: [PATCH] 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 e3b85f43f..e1121d3a8 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) }