Skip to content

Commit 25671ae

Browse files
committed
introduce config --variables to list compose model variables
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 0191e69 commit 25671ae

File tree

3 files changed

+50
-15
lines changed

3 files changed

+50
-15
lines changed

cmd/compose/config.go

+29-5
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@ import (
2121
"context"
2222
"encoding/json"
2323
"fmt"
24+
"io"
2425
"os"
2526
"sort"
2627
"strings"
2728

2829
"github.com/compose-spec/compose-go/v2/cli"
30+
"github.com/compose-spec/compose-go/v2/template"
2931
"github.com/compose-spec/compose-go/v2/types"
3032
"github.com/docker/cli/cli/command"
33+
"github.com/docker/compose/v2/cmd/formatter"
3134
"github.com/spf13/cobra"
3235
"gopkg.in/yaml.v3"
3336

@@ -50,6 +53,7 @@ type configOptions struct {
5053
images bool
5154
hash string
5255
noConsistency bool
56+
variables bool
5357
}
5458

5559
func (o *configOptions) ToProject(ctx context.Context, dockerCli command.Cli, services []string, po ...cli.ProjectOptionsFn) (*types.Project, error) {
@@ -111,6 +115,9 @@ func configCommand(p *ProjectOptions, dockerCli command.Cli) *cobra.Command {
111115
if opts.images {
112116
return runConfigImages(ctx, dockerCli, opts, args)
113117
}
118+
if opts.variables {
119+
return runVariables(ctx, dockerCli, opts, args)
120+
}
114121

115122
return runConfig(ctx, dockerCli, opts, args)
116123
}),
@@ -125,11 +132,12 @@ func configCommand(p *ProjectOptions, dockerCli command.Cli) *cobra.Command {
125132
flags.BoolVar(&opts.noResolvePath, "no-path-resolution", false, "Don't resolve file paths")
126133
flags.BoolVar(&opts.noConsistency, "no-consistency", false, "Don't check model consistency - warning: may produce invalid Compose output")
127134

128-
flags.BoolVar(&opts.services, "services", false, "Print the service names, one per line")
129-
flags.BoolVar(&opts.volumes, "volumes", false, "Print the volume names, one per line")
130-
flags.BoolVar(&opts.profiles, "profiles", false, "Print the profile names, one per line")
131-
flags.BoolVar(&opts.images, "images", false, "Print the image names, one per line")
132-
flags.StringVar(&opts.hash, "hash", "", "Print the service config hash, one per line")
135+
flags.BoolVar(&opts.services, "services", false, "Print the service names, one per line.")
136+
flags.BoolVar(&opts.volumes, "volumes", false, "Print the volume names, one per line.")
137+
flags.BoolVar(&opts.profiles, "profiles", false, "Print the profile names, one per line.")
138+
flags.BoolVar(&opts.images, "images", false, "Print the image names, one per line.")
139+
flags.StringVar(&opts.hash, "hash", "", "Print the service config hash, one per line.")
140+
flags.BoolVar(&opts.variables, "variables", false, "Print model variables and default values.")
133141
flags.StringVarP(&opts.Output, "output", "o", "", "Save to file (default to stdout)")
134142

135143
return cmd
@@ -333,6 +341,22 @@ func runConfigImages(ctx context.Context, dockerCli command.Cli, opts configOpti
333341
return nil
334342
}
335343

344+
func runVariables(ctx context.Context, dockerCli command.Cli, opts configOptions, services []string) error {
345+
opts.noInterpolate = true
346+
model, err := opts.ToModel(ctx, dockerCli, services, cli.WithoutEnvironmentResolution)
347+
if err != nil {
348+
return err
349+
}
350+
351+
variables := template.ExtractVariables(model, template.DefaultPattern)
352+
353+
return formatter.Print(variables, "", dockerCli.Out(), func(w io.Writer) {
354+
for name, variable := range variables {
355+
_, _ = fmt.Fprintf(w, "%s\t%t\t%s\t%s\n", name, variable.Required, variable.DefaultValue, variable.PresenceValue)
356+
}
357+
}, "NAME", "REQUIRED", "DEFAULT VALUE", "ALTERNATE VALUE")
358+
}
359+
336360
func escapeDollarSign(marshal []byte) []byte {
337361
dollar := []byte{'$'}
338362
escDollar := []byte{'$', '$'}

docs/reference/compose_config.md

+6-5
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,19 @@ Parse, resolve and render compose file in canonical format
1313
|:--------------------------|:---------|:--------|:----------------------------------------------------------------------------|
1414
| `--dry-run` | | | Execute command in dry run mode |
1515
| `--format` | `string` | `yaml` | Format the output. Values: [yaml \| json] |
16-
| `--hash` | `string` | | Print the service config hash, one per line |
17-
| `--images` | | | Print the image names, one per line |
16+
| `--hash` | `string` | | Print the service config hash, one per line. |
17+
| `--images` | | | Print the image names, one per line. |
1818
| `--no-consistency` | | | Don't check model consistency - warning: may produce invalid Compose output |
1919
| `--no-interpolate` | | | Don't interpolate environment variables |
2020
| `--no-normalize` | | | Don't normalize compose model |
2121
| `--no-path-resolution` | | | Don't resolve file paths |
2222
| `-o`, `--output` | `string` | | Save to file (default to stdout) |
23-
| `--profiles` | | | Print the profile names, one per line |
23+
| `--profiles` | | | Print the profile names, one per line. |
2424
| `-q`, `--quiet` | | | Only validate the configuration, don't print anything |
2525
| `--resolve-image-digests` | | | Pin image tags to digests |
26-
| `--services` | | | Print the service names, one per line |
27-
| `--volumes` | | | Print the volume names, one per line |
26+
| `--services` | | | Print the service names, one per line. |
27+
| `--variables` | | | Print model variables and default values. |
28+
| `--volumes` | | | Print the volume names, one per line. |
2829

2930

3031
<!---MARKER_GEN_END-->

docs/reference/docker_compose_config.yaml

+15-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ options:
2121
swarm: false
2222
- option: hash
2323
value_type: string
24-
description: Print the service config hash, one per line
24+
description: Print the service config hash, one per line.
2525
deprecated: false
2626
hidden: false
2727
experimental: false
@@ -31,7 +31,7 @@ options:
3131
- option: images
3232
value_type: bool
3333
default_value: "false"
34-
description: Print the image names, one per line
34+
description: Print the image names, one per line.
3535
deprecated: false
3636
hidden: false
3737
experimental: false
@@ -92,7 +92,7 @@ options:
9292
- option: profiles
9393
value_type: bool
9494
default_value: "false"
95-
description: Print the profile names, one per line
95+
description: Print the profile names, one per line.
9696
deprecated: false
9797
hidden: false
9898
experimental: false
@@ -123,7 +123,17 @@ options:
123123
- option: services
124124
value_type: bool
125125
default_value: "false"
126-
description: Print the service names, one per line
126+
description: Print the service names, one per line.
127+
deprecated: false
128+
hidden: false
129+
experimental: false
130+
experimentalcli: false
131+
kubernetes: false
132+
swarm: false
133+
- option: variables
134+
value_type: bool
135+
default_value: "false"
136+
description: Print model variables and default values.
127137
deprecated: false
128138
hidden: false
129139
experimental: false
@@ -133,7 +143,7 @@ options:
133143
- option: volumes
134144
value_type: bool
135145
default_value: "false"
136-
description: Print the volume names, one per line
146+
description: Print the volume names, one per line.
137147
deprecated: false
138148
hidden: false
139149
experimental: false

0 commit comments

Comments
 (0)