| // Copyright 2017 The kubecfg authors |
| // |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| package cmd |
| |
| import ( |
| "fmt" |
| "os" |
| |
| "github.com/mattn/go-isatty" |
| "github.com/spf13/cobra" |
| |
| "code.hackerspace.pl/hscloud/cluster/tools/kartongips/pkg/kubecfg" |
| ) |
| |
| const ( |
| // TODO(b/49): remove this flag |
| flagDiffStrategy = "diff-strategy" |
| flagOmitSecrets = "omit-secrets" |
| ) |
| |
| func init() { |
| diffCmd.PersistentFlags().String(flagDiffStrategy, "", "Diff strategy - no op (will be removed soon)") |
| diffCmd.PersistentFlags().Bool(flagOmitSecrets, false, "hide secret details when showing diff") |
| RootCmd.AddCommand(diffCmd) |
| } |
| |
| // nagStrategy nags the user about selecting strategy=subset explicitly - |
| // either hint at not having to use it if subset is specified, or fail hard if |
| // something else is set. |
| // |
| // TODO(b/49): remove this |
| func nagStrategy(chosen string) { |
| if chosen == "" { |
| return |
| } |
| color := isatty.IsTerminal(os.Stdout.Fd()) |
| |
| if chosen == "subset" { |
| if color { |
| fmt.Fprintf(os.Stdout, "\x1b[92m") |
| } |
| fmt.Fprintf(os.Stdout, "--diff-strategy=subset is now the default behaviour of kartongips/kubecfg, no need to explicitly set it.\n") |
| fmt.Fprintf(os.Stdout, "Work on your muscle memory and fix your scripts! This flag will be removed soon (see: b.hswaw.net/49).\n") |
| if color { |
| fmt.Fprintf(os.Stdout, "\x1b[0m") |
| } |
| return |
| } |
| |
| fmt.Fprintf(os.Stderr, "--diff-strategy is deprecated, the default behaviour is now 'subset' and all other modes of operation have been removed. See: b.hswaw.net/49.\n") |
| os.Exit(1) |
| } |
| |
| var diffCmd = &cobra.Command{ |
| Use: "diff", |
| Short: "Display differences between server and local config", |
| Args: cobra.ArbitraryArgs, |
| RunE: func(cmd *cobra.Command, args []string) error { |
| flags := cmd.Flags() |
| var err error |
| |
| c := kubecfg.DiffCmd{} |
| |
| diffStrategy, err := flags.GetString(flagDiffStrategy) |
| if err != nil { |
| return err |
| } |
| nagStrategy(diffStrategy) |
| |
| c.OmitSecrets, err = flags.GetBool(flagOmitSecrets) |
| if err != nil { |
| return err |
| } |
| |
| c.Client, c.Mapper, _, err = getDynamicClients(cmd) |
| if err != nil { |
| return err |
| } |
| |
| c.DefaultNamespace, err = defaultNamespace(clientConfig) |
| if err != nil { |
| return err |
| } |
| |
| objs, err := readObjs(cmd, args) |
| if err != nil { |
| return err |
| } |
| |
| err = c.Run(cmd.Context(), objs, cmd.OutOrStdout()) |
| nagStrategy(diffStrategy) |
| return err |
| }, |
| } |