dcmerge/cmd/root.go

111 lines
3.0 KiB
Go
Raw Normal View History

2023-07-19 19:44:02 +00:00
package cmd
import (
"fmt"
"os"
"path/filepath"
"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",
2023-07-26 08:43:49 +00:00
Args: cobra.MinimumNArgs(1),
2023-07-19 19:44:02 +00:00
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,
}
2023-08-28 09:13:32 +00:00
rootCmd.Flags().BoolP("existing-win", "f", false, "Protect existing attributes")
2023-07-26 08:28:08 +00:00
rootCmd.Flags().BoolP("last-win", "l", false, "Overwrite existing attributes")
2023-07-26 08:37:56 +00:00
rootCmd.Flags().StringP("output-file", "o", "", "Write instead on stdout into a file")
2023-07-19 19:44:02 +00:00
rootCmd.AddCommand(completionCmd)
return rootCmd.Execute()
}
func run(cmd *cobra.Command, args []string) error {
2023-08-28 09:13:32 +00:00
mergeExisting, err := cmd.Flags().GetBool("existing-win")
2023-08-28 08:57:26 +00:00
if err != nil {
return fmt.Errorf("Failed to parse flag last-win: %s", err)
}
2023-07-26 08:28:08 +00:00
mergeLastWin, err := cmd.Flags().GetBool("last-win")
2023-07-19 19:44:02 +00:00
if err != nil {
2023-07-26 08:28:08 +00:00
return fmt.Errorf("Failed to parse flag last-win: %s", err)
2023-07-19 19:44:02 +00:00
}
outputFile, err := cmd.Flags().GetString("output-file")
if err != nil {
return fmt.Errorf("Failed to parse flag output-file: %s", err)
}
dockerComposeConfig := dockerCompose.NewConfig()
dockerComposeConfigs, err := fetcher.Fetch(args...)
if err != nil {
return err
}
for _, config := range dockerComposeConfigs {
switch {
2023-08-28 09:13:32 +00:00
case mergeExisting && mergeLastWin:
2023-08-28 08:57:26 +00:00
return fmt.Errorf("Neither --first-win or --last-win can be specified - not booth.")
2023-08-28 09:13:32 +00:00
case mergeExisting && !mergeLastWin:
dockerComposeConfig.MergeExistingWin(config)
case !mergeExisting && mergeLastWin:
2023-07-19 19:44:02 +00:00
dockerComposeConfig.MergeLastWin(config)
default:
dockerComposeConfig.Merge(config)
}
}
switch {
case len(outputFile) > 0:
err = os.MkdirAll(filepath.Dir(outputFile), 0755)
if err != nil {
return err
}
f, err := os.Create(outputFile)
if err != nil {
return err
}
defer f.Close()
yamlEncoder := yaml.NewEncoder(f)
return yamlEncoder.Encode(dockerComposeConfig)
default:
yamlEncoder := yaml.NewEncoder(os.Stdout)
return yamlEncoder.Encode(dockerComposeConfig)
}
}