Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions cmd/compose/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/compose_config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
1 change: 0 additions & 1 deletion docs/reference/docker_compose_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions pkg/e2e/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`})
})
}