package cmd import ( "fmt" "os" "git.cryptic.systems/volker.raschek/dcmerge/pkg/domain/dockerCompose" "git.cryptic.systems/volker.raschek/dcmerge/pkg/fetcher" "github.com/spf13/cobra" "gopkg.in/yaml.v2" ) func Execute(version string) error { completionCmd := &cobra.Command{ Use: "completion [bash|zsh|fish|powershell]", Short: "Generate completion script", Long: "To load completions", DisableFlagsInUseLine: true, ValidArgs: []string{"bash", "zsh", "fish", "powershell"}, Args: cobra.ExactValidArgs(1), Run: func(cmd *cobra.Command, args []string) { switch args[0] { case "bash": cmd.Root().GenBashCompletion(os.Stdout) case "zsh": cmd.Root().GenZshCompletion(os.Stdout) case "fish": cmd.Root().GenFishCompletion(os.Stdout, true) case "powershell": cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout) } }, } rootCmd := &cobra.Command{ Use: "dcmerge", Args: cobra.MinimumNArgs(1), Short: "Merge docker-compose files from multiple resources", Example: `dcmerge docker-compose.yml ./integration-test/docker-compose.yml dcmerge docker-compose.yml https://git.example.local/user/repo/docker-compose.yml`, RunE: run, Version: version, } rootCmd.Flags().BoolP("merge-last-win", "l", true, "Overwrite existing attributes") rootCmd.AddCommand(completionCmd) return rootCmd.Execute() } func run(cmd *cobra.Command, args []string) error { mergeLastWin, err := cmd.Flags().GetBool("merge-last-win") if err != nil { return fmt.Errorf("Failed to parse flag merge-last-win: %s", err) } dockerComposeConfig := dockerCompose.NewConfig() dockerComposeConfigs, err := fetcher.Fetch(args...) if err != nil { return err } for _, config := range dockerComposeConfigs { switch { case mergeLastWin: dockerComposeConfig.MergeLastWin(config) } } yamlEncoder := yaml.NewEncoder(os.Stdout) return yamlEncoder.Encode(dockerComposeConfig) }