Explorar o código

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 <[email protected]>
Alessio Perugini hai 8 meses
pai
achega
d6e3fa6d74
Modificáronse 1 ficheiros con 14 adicións e 2 borrados
  1. 14 2
      cmd/compose/config.go

+ 14 - 2
cmd/compose/config.go

@@ -124,12 +124,15 @@ func configCommand(p *ProjectOptions, dockerCli command.Cli) *cobra.Command {
 				return runEnvironment(ctx, dockerCli, opts, args)
 				return runEnvironment(ctx, dockerCli, opts, args)
 			}
 			}
 
 
+			if opts.Format == "" {
+				opts.Format = "yaml"
+			}
 			return runConfig(ctx, dockerCli, opts, args)
 			return runConfig(ctx, dockerCli, opts, args)
 		}),
 		}),
 		ValidArgsFunction: completeServiceNames(dockerCli, p),
 		ValidArgsFunction: completeServiceNames(dockerCli, p),
 	}
 	}
 	flags := cmd.Flags()
 	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.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.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")
 	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)
 	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 {
 		for name, variable := range variables {
 			_, _ = fmt.Fprintf(w, "%s\t%t\t%s\t%s\n", name, variable.Required, variable.DefaultValue, variable.PresenceValue)
 			_, _ = fmt.Fprintf(w, "%s\t%t\t%s\t%s\n", name, variable.Required, variable.DefaultValue, variable.PresenceValue)
 		}
 		}