dcmerge/cmd/root.go
Markus Pesch c975cd40eb
All checks were successful
continuous-integration/drone/push Build is passing
Initial Commit
2023-07-26 09:44:55 +02:00

76 lines
2.0 KiB
Go

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(2),
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)
default:
dockerComposeConfig.Merge(config)
}
}
yamlEncoder := yaml.NewEncoder(os.Stdout)
return yamlEncoder.Encode(dockerComposeConfig)
}