Skip to content

Commit

Permalink
introduce config --variables to list compose model variables
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
  • Loading branch information
ndeloof committed Mar 22, 2024
1 parent 0191e69 commit 25671ae
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 15 deletions.
34 changes: 29 additions & 5 deletions cmd/compose/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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) {
Expand Down Expand Up @@ -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)
}),
Expand All @@ -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
Expand Down Expand Up @@ -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{'$', '$'}
Expand Down
11 changes: 6 additions & 5 deletions docs/reference/compose_config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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-->
Expand Down
20 changes: 15 additions & 5 deletions docs/reference/docker_compose_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 25671ae

Please sign in to comment.