@@ -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
5559func (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+
336360func escapeDollarSign (marshal []byte ) []byte {
337361 dollar := []byte {'$' }
338362 escDollar := []byte {'$' , '$' }
0 commit comments