This replaces the visitAll recursive function with a test that verifies that the option is set for all commands and subcommands, so that it doesn't have to be modified at runtime. We currently still have to loop over all functions for the setValidateArgs call, but that can be looked at separately. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
67 lines
1.8 KiB
Go
67 lines
1.8 KiB
Go
package stack
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
|
|
"github.com/docker/cli/cli"
|
|
"github.com/docker/cli/cli/command"
|
|
"github.com/docker/cli/cli/command/stack/loader"
|
|
"github.com/docker/cli/cli/command/stack/options"
|
|
composeLoader "github.com/docker/cli/cli/compose/loader"
|
|
composetypes "github.com/docker/cli/cli/compose/types"
|
|
"github.com/spf13/cobra"
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
func newConfigCommand(dockerCLI command.Cli) *cobra.Command {
|
|
var opts options.Config
|
|
|
|
cmd := &cobra.Command{
|
|
Use: "config [OPTIONS]",
|
|
Short: "Outputs the final config file, after doing merges and interpolations",
|
|
Args: cli.NoArgs,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
configDetails, err := loader.GetConfigDetails(opts.Composefiles, dockerCLI.In())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
cfg, err := outputConfig(configDetails, opts.SkipInterpolation)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = fmt.Fprintf(dockerCLI.Out(), "%s", cfg)
|
|
return err
|
|
},
|
|
ValidArgsFunction: cobra.NoFileCompletions,
|
|
DisableFlagsInUseLine: true,
|
|
}
|
|
|
|
flags := cmd.Flags()
|
|
flags.StringSliceVarP(&opts.Composefiles, "compose-file", "c", []string{}, `Path to a Compose file, or "-" to read from stdin`)
|
|
flags.BoolVar(&opts.SkipInterpolation, "skip-interpolation", false, "Skip interpolation and output only merged config")
|
|
return cmd
|
|
}
|
|
|
|
// outputConfig returns the merged and interpolated config file
|
|
func outputConfig(configFiles composetypes.ConfigDetails, skipInterpolation bool) (string, error) {
|
|
optsFunc := func(opts *composeLoader.Options) {
|
|
opts.SkipInterpolation = skipInterpolation
|
|
}
|
|
config, err := composeLoader.Load(configFiles, optsFunc)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
enc := yaml.NewEncoder(&buf)
|
|
enc.SetIndent(2)
|
|
err = enc.Encode(&config)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return buf.String(), nil
|
|
}
|