fix(cmd): add flag first-win

This commit is contained in:
Markus Pesch 2023-08-28 10:57:26 +02:00
parent 2ea83b7591
commit 4ad9da6d63
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982

View File

@ -42,6 +42,7 @@ dcmerge docker-compose.yml https://git.example.local/user/repo/docker-compose.ym
RunE: run, RunE: run,
Version: version, Version: version,
} }
rootCmd.Flags().BoolP("first-win", "f", false, "Add only missing attributes")
rootCmd.Flags().BoolP("last-win", "l", false, "Overwrite existing attributes") rootCmd.Flags().BoolP("last-win", "l", false, "Overwrite existing attributes")
rootCmd.Flags().StringP("output-file", "o", "", "Write instead on stdout into a file") rootCmd.Flags().StringP("output-file", "o", "", "Write instead on stdout into a file")
rootCmd.AddCommand(completionCmd) rootCmd.AddCommand(completionCmd)
@ -50,6 +51,11 @@ dcmerge docker-compose.yml https://git.example.local/user/repo/docker-compose.ym
} }
func run(cmd *cobra.Command, args []string) error { func run(cmd *cobra.Command, args []string) error {
mergeFirstWin, err := cmd.Flags().GetBool("first-win")
if err != nil {
return fmt.Errorf("Failed to parse flag last-win: %s", err)
}
mergeLastWin, err := cmd.Flags().GetBool("last-win") mergeLastWin, err := cmd.Flags().GetBool("last-win")
if err != nil { if err != nil {
return fmt.Errorf("Failed to parse flag last-win: %s", err) return fmt.Errorf("Failed to parse flag last-win: %s", err)
@ -69,12 +75,15 @@ func run(cmd *cobra.Command, args []string) error {
for _, config := range dockerComposeConfigs { for _, config := range dockerComposeConfigs {
switch { switch {
case mergeLastWin: case mergeFirstWin && mergeLastWin:
return fmt.Errorf("Neither --first-win or --last-win can be specified - not booth.")
case mergeFirstWin && !mergeLastWin:
dockerComposeConfig.MergeFirstWin(config)
case !mergeFirstWin && mergeLastWin:
dockerComposeConfig.MergeLastWin(config) dockerComposeConfig.MergeLastWin(config)
default: default:
dockerComposeConfig.Merge(config) dockerComposeConfig.Merge(config)
} }
} }
switch { switch {