@@ -21,13 +21,16 @@ import (
21
21
"context"
22
22
"encoding/json"
23
23
"fmt"
24
+ "io"
24
25
"os"
25
26
"sort"
26
27
"strings"
27
28
28
29
"github.com/compose-spec/compose-go/v2/cli"
30
+ "github.com/compose-spec/compose-go/v2/template"
29
31
"github.com/compose-spec/compose-go/v2/types"
30
32
"github.com/docker/cli/cli/command"
33
+ "github.com/docker/compose/v2/cmd/formatter"
31
34
"github.com/spf13/cobra"
32
35
"gopkg.in/yaml.v3"
33
36
@@ -50,6 +53,7 @@ type configOptions struct {
50
53
images bool
51
54
hash string
52
55
noConsistency bool
56
+ variables bool
53
57
}
54
58
55
59
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 {
111
115
if opts .images {
112
116
return runConfigImages (ctx , dockerCli , opts , args )
113
117
}
118
+ if opts .variables {
119
+ return runVariables (ctx , dockerCli , opts , args )
120
+ }
114
121
115
122
return runConfig (ctx , dockerCli , opts , args )
116
123
}),
@@ -125,11 +132,12 @@ func configCommand(p *ProjectOptions, dockerCli command.Cli) *cobra.Command {
125
132
flags .BoolVar (& opts .noResolvePath , "no-path-resolution" , false , "Don't resolve file paths" )
126
133
flags .BoolVar (& opts .noConsistency , "no-consistency" , false , "Don't check model consistency - warning: may produce invalid Compose output" )
127
134
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." )
133
141
flags .StringVarP (& opts .Output , "output" , "o" , "" , "Save to file (default to stdout)" )
134
142
135
143
return cmd
@@ -333,6 +341,22 @@ func runConfigImages(ctx context.Context, dockerCli command.Cli, opts configOpti
333
341
return nil
334
342
}
335
343
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
+
336
360
func escapeDollarSign (marshal []byte ) []byte {
337
361
dollar := []byte {'$' }
338
362
escDollar := []byte {'$' , '$' }
0 commit comments