diff --git a/cmd/root.go b/cmd/root.go index b565a37..b9e2d76 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -42,6 +42,7 @@ dcmerge docker-compose.yml https://git.example.local/user/repo/docker-compose.ym RunE: run, 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().StringP("output-file", "o", "", "Write instead on stdout into a file") 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 { + 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") if err != nil { 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 { 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) default: dockerComposeConfig.Merge(config) } - } switch {