github.1git.de/docker/cli@v26.1.3+incompatible/cli/command/stack/config.go (about)

     1  package stack
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/docker/cli/cli"
     7  	"github.com/docker/cli/cli/command"
     8  	"github.com/docker/cli/cli/command/completion"
     9  	"github.com/docker/cli/cli/command/stack/loader"
    10  	"github.com/docker/cli/cli/command/stack/options"
    11  	composeLoader "github.com/docker/cli/cli/compose/loader"
    12  	composetypes "github.com/docker/cli/cli/compose/types"
    13  	"github.com/spf13/cobra"
    14  	yaml "gopkg.in/yaml.v2"
    15  )
    16  
    17  func newConfigCommand(dockerCli command.Cli) *cobra.Command {
    18  	var opts options.Config
    19  
    20  	cmd := &cobra.Command{
    21  		Use:   "config [OPTIONS]",
    22  		Short: "Outputs the final config file, after doing merges and interpolations",
    23  		Args:  cli.NoArgs,
    24  		RunE: func(cmd *cobra.Command, args []string) error {
    25  			configDetails, err := loader.GetConfigDetails(opts.Composefiles, dockerCli.In())
    26  			if err != nil {
    27  				return err
    28  			}
    29  
    30  			cfg, err := outputConfig(configDetails, opts.SkipInterpolation)
    31  			if err != nil {
    32  				return err
    33  			}
    34  
    35  			_, err = fmt.Fprintf(dockerCli.Out(), "%s", cfg)
    36  			return err
    37  		},
    38  		ValidArgsFunction: completion.NoComplete,
    39  	}
    40  
    41  	flags := cmd.Flags()
    42  	flags.StringSliceVarP(&opts.Composefiles, "compose-file", "c", []string{}, `Path to a Compose file, or "-" to read from stdin`)
    43  	flags.BoolVar(&opts.SkipInterpolation, "skip-interpolation", false, "Skip interpolation and output only merged config")
    44  	return cmd
    45  }
    46  
    47  // outputConfig returns the merged and interpolated config file
    48  func outputConfig(configFiles composetypes.ConfigDetails, skipInterpolation bool) (string, error) {
    49  	optsFunc := func(opts *composeLoader.Options) {
    50  		opts.SkipInterpolation = skipInterpolation
    51  	}
    52  	config, err := composeLoader.Load(configFiles, optsFunc)
    53  	if err != nil {
    54  		return "", err
    55  	}
    56  
    57  	d, err := yaml.Marshal(&config)
    58  	if err != nil {
    59  		return "", err
    60  	}
    61  	return string(d), nil
    62  }