Browse Source

introduce config --variables to list compose model variables

Signed-off-by: Nicolas De Loof <[email protected]>
Nicolas De Loof 1 năm trước cách đây
mục cha
commit
25671ae622

+ 29 - 5
cmd/compose/config.go

@@ -21,13 +21,16 @@ import (
 	"context"
 	"encoding/json"
 	"fmt"
+	"io"
 	"os"
 	"sort"
 	"strings"
 
 	"github.com/compose-spec/compose-go/v2/cli"
+	"github.com/compose-spec/compose-go/v2/template"
 	"github.com/compose-spec/compose-go/v2/types"
 	"github.com/docker/cli/cli/command"
+	"github.com/docker/compose/v2/cmd/formatter"
 	"github.com/spf13/cobra"
 	"gopkg.in/yaml.v3"
 
@@ -50,6 +53,7 @@ type configOptions struct {
 	images              bool
 	hash                string
 	noConsistency       bool
+	variables           bool
 }
 
 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 {
 			if opts.images {
 				return runConfigImages(ctx, dockerCli, opts, args)
 			}
+			if opts.variables {
+				return runVariables(ctx, dockerCli, opts, args)
+			}
 
 			return runConfig(ctx, dockerCli, opts, args)
 		}),
@@ -125,11 +132,12 @@ func configCommand(p *ProjectOptions, dockerCli command.Cli) *cobra.Command {
 	flags.BoolVar(&opts.noResolvePath, "no-path-resolution", false, "Don't resolve file paths")
 	flags.BoolVar(&opts.noConsistency, "no-consistency", false, "Don't check model consistency - warning: may produce invalid Compose output")
 
-	flags.BoolVar(&opts.services, "services", false, "Print the service names, one per line")
-	flags.BoolVar(&opts.volumes, "volumes", false, "Print the volume names, one per line")
-	flags.BoolVar(&opts.profiles, "profiles", false, "Print the profile names, one per line")
-	flags.BoolVar(&opts.images, "images", false, "Print the image names, one per line")
-	flags.StringVar(&opts.hash, "hash", "", "Print the service config hash, one per line")
+	flags.BoolVar(&opts.services, "services", false, "Print the service names, one per line.")
+	flags.BoolVar(&opts.volumes, "volumes", false, "Print the volume names, one per line.")
+	flags.BoolVar(&opts.profiles, "profiles", false, "Print the profile names, one per line.")
+	flags.BoolVar(&opts.images, "images", false, "Print the image names, one per line.")
+	flags.StringVar(&opts.hash, "hash", "", "Print the service config hash, one per line.")
+	flags.BoolVar(&opts.variables, "variables", false, "Print model variables and default values.")
 	flags.StringVarP(&opts.Output, "output", "o", "", "Save to file (default to stdout)")
 
 	return cmd
@@ -333,6 +341,22 @@ func runConfigImages(ctx context.Context, dockerCli command.Cli, opts configOpti
 	return nil
 }
 
+func runVariables(ctx context.Context, dockerCli command.Cli, opts configOptions, services []string) error {
+	opts.noInterpolate = true
+	model, err := opts.ToModel(ctx, dockerCli, services, cli.WithoutEnvironmentResolution)
+	if err != nil {
+		return err
+	}
+
+	variables := template.ExtractVariables(model, template.DefaultPattern)
+
+	return formatter.Print(variables, "", 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)
+		}
+	}, "NAME", "REQUIRED", "DEFAULT VALUE", "ALTERNATE VALUE")
+}
+
 func escapeDollarSign(marshal []byte) []byte {
 	dollar := []byte{'$'}
 	escDollar := []byte{'$', '$'}

+ 6 - 5
docs/reference/compose_config.md

@@ -13,18 +13,19 @@ Parse, resolve and render compose file in canonical format
 |:--------------------------|:---------|:--------|:----------------------------------------------------------------------------|
 | `--dry-run`               |          |         | Execute command in dry run mode                                             |
 | `--format`                | `string` | `yaml`  | Format the output. Values: [yaml \| json]                                   |
-| `--hash`                  | `string` |         | Print the service config hash, one per line                                 |
-| `--images`                |          |         | Print the image names, one per line                                         |
+| `--hash`                  | `string` |         | Print the service config hash, one per line.                                |
+| `--images`                |          |         | Print the image names, one per line.                                        |
 | `--no-consistency`        |          |         | Don't check model consistency - warning: may produce invalid Compose output |
 | `--no-interpolate`        |          |         | Don't interpolate environment variables                                     |
 | `--no-normalize`          |          |         | Don't normalize compose model                                               |
 | `--no-path-resolution`    |          |         | Don't resolve file paths                                                    |
 | `-o`, `--output`          | `string` |         | Save to file (default to stdout)                                            |
-| `--profiles`              |          |         | Print the profile names, one per line                                       |
+| `--profiles`              |          |         | Print the profile names, one per line.                                      |
 | `-q`, `--quiet`           |          |         | Only validate the configuration, don't print anything                       |
 | `--resolve-image-digests` |          |         | Pin image tags to digests                                                   |
-| `--services`              |          |         | Print the service names, one per line                                       |
-| `--volumes`               |          |         | Print the volume names, one per line                                        |
+| `--services`              |          |         | Print the service names, one per line.                                      |
+| `--variables`             |          |         | Print model variables and default values.                                   |
+| `--volumes`               |          |         | Print the volume names, one per line.                                       |
 
 
 <!---MARKER_GEN_END-->

+ 15 - 5
docs/reference/docker_compose_config.yaml

@@ -21,7 +21,7 @@ options:
       swarm: false
     - option: hash
       value_type: string
-      description: Print the service config hash, one per line
+      description: Print the service config hash, one per line.
       deprecated: false
       hidden: false
       experimental: false
@@ -31,7 +31,7 @@ options:
     - option: images
       value_type: bool
       default_value: "false"
-      description: Print the image names, one per line
+      description: Print the image names, one per line.
       deprecated: false
       hidden: false
       experimental: false
@@ -92,7 +92,7 @@ options:
     - option: profiles
       value_type: bool
       default_value: "false"
-      description: Print the profile names, one per line
+      description: Print the profile names, one per line.
       deprecated: false
       hidden: false
       experimental: false
@@ -123,7 +123,17 @@ options:
     - option: services
       value_type: bool
       default_value: "false"
-      description: Print the service names, one per line
+      description: Print the service names, one per line.
+      deprecated: false
+      hidden: false
+      experimental: false
+      experimentalcli: false
+      kubernetes: false
+      swarm: false
+    - option: variables
+      value_type: bool
+      default_value: "false"
+      description: Print model variables and default values.
       deprecated: false
       hidden: false
       experimental: false
@@ -133,7 +143,7 @@ options:
     - option: volumes
       value_type: bool
       default_value: "false"
-      description: Print the volume names, one per line
+      description: Print the volume names, one per line.
       deprecated: false
       hidden: false
       experimental: false