Serge Bazanski | be538db | 2020-11-12 00:22:42 +0100 | [diff] [blame] | 1 | // Copyright 2017 The kubecfg authors |
| 2 | // |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | package cmd |
| 17 | |
| 18 | import ( |
Serge Bazanski | 59c8149 | 2021-09-11 12:39:51 +0000 | [diff] [blame] | 19 | "fmt" |
| 20 | "os" |
| 21 | |
| 22 | "github.com/mattn/go-isatty" |
Serge Bazanski | be538db | 2020-11-12 00:22:42 +0100 | [diff] [blame] | 23 | "github.com/spf13/cobra" |
| 24 | |
| 25 | "code.hackerspace.pl/hscloud/cluster/tools/kartongips/pkg/kubecfg" |
| 26 | ) |
| 27 | |
| 28 | const ( |
Serge Bazanski | 59c8149 | 2021-09-11 12:39:51 +0000 | [diff] [blame] | 29 | // TODO(b/49): remove this flag |
Serge Bazanski | be538db | 2020-11-12 00:22:42 +0100 | [diff] [blame] | 30 | flagDiffStrategy = "diff-strategy" |
| 31 | flagOmitSecrets = "omit-secrets" |
| 32 | ) |
| 33 | |
| 34 | func init() { |
Serge Bazanski | 59c8149 | 2021-09-11 12:39:51 +0000 | [diff] [blame] | 35 | diffCmd.PersistentFlags().String(flagDiffStrategy, "", "Diff strategy - no op (will be removed soon)") |
Serge Bazanski | be538db | 2020-11-12 00:22:42 +0100 | [diff] [blame] | 36 | diffCmd.PersistentFlags().Bool(flagOmitSecrets, false, "hide secret details when showing diff") |
| 37 | RootCmd.AddCommand(diffCmd) |
| 38 | } |
| 39 | |
Serge Bazanski | 59c8149 | 2021-09-11 12:39:51 +0000 | [diff] [blame] | 40 | // nagStrategy nags the user about selecting strategy=subset explicitly - |
| 41 | // either hint at not having to use it if subset is specified, or fail hard if |
| 42 | // something else is set. |
| 43 | // |
| 44 | // TODO(b/49): remove this |
| 45 | func nagStrategy(chosen string) { |
| 46 | if chosen == "" { |
| 47 | return |
| 48 | } |
| 49 | color := isatty.IsTerminal(os.Stdout.Fd()) |
| 50 | |
| 51 | if chosen == "subset" { |
| 52 | if color { |
| 53 | fmt.Fprintf(os.Stdout, "\x1b[92m") |
| 54 | } |
| 55 | fmt.Fprintf(os.Stdout, "--diff-strategy=subset is now the default behaviour of kartongips/kubecfg, no need to explicitly set it.\n") |
| 56 | 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") |
| 57 | if color { |
| 58 | fmt.Fprintf(os.Stdout, "\x1b[0m") |
| 59 | } |
| 60 | return |
| 61 | } |
| 62 | |
| 63 | 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") |
| 64 | os.Exit(1) |
| 65 | } |
| 66 | |
Serge Bazanski | be538db | 2020-11-12 00:22:42 +0100 | [diff] [blame] | 67 | var diffCmd = &cobra.Command{ |
| 68 | Use: "diff", |
| 69 | Short: "Display differences between server and local config", |
| 70 | Args: cobra.ArbitraryArgs, |
| 71 | RunE: func(cmd *cobra.Command, args []string) error { |
| 72 | flags := cmd.Flags() |
| 73 | var err error |
| 74 | |
| 75 | c := kubecfg.DiffCmd{} |
| 76 | |
Serge Bazanski | 59c8149 | 2021-09-11 12:39:51 +0000 | [diff] [blame] | 77 | diffStrategy, err := flags.GetString(flagDiffStrategy) |
Serge Bazanski | be538db | 2020-11-12 00:22:42 +0100 | [diff] [blame] | 78 | if err != nil { |
| 79 | return err |
| 80 | } |
Serge Bazanski | 59c8149 | 2021-09-11 12:39:51 +0000 | [diff] [blame] | 81 | nagStrategy(diffStrategy) |
Serge Bazanski | be538db | 2020-11-12 00:22:42 +0100 | [diff] [blame] | 82 | |
| 83 | c.OmitSecrets, err = flags.GetBool(flagOmitSecrets) |
| 84 | if err != nil { |
| 85 | return err |
| 86 | } |
| 87 | |
| 88 | c.Client, c.Mapper, _, err = getDynamicClients(cmd) |
| 89 | if err != nil { |
| 90 | return err |
| 91 | } |
| 92 | |
| 93 | c.DefaultNamespace, err = defaultNamespace(clientConfig) |
| 94 | if err != nil { |
| 95 | return err |
| 96 | } |
| 97 | |
| 98 | objs, err := readObjs(cmd, args) |
| 99 | if err != nil { |
| 100 | return err |
| 101 | } |
| 102 | |
Serge Bazanski | 59c8149 | 2021-09-11 12:39:51 +0000 | [diff] [blame] | 103 | err = c.Run(cmd.Context(), objs, cmd.OutOrStdout()) |
| 104 | nagStrategy(diffStrategy) |
| 105 | return err |
Serge Bazanski | be538db | 2020-11-12 00:22:42 +0100 | [diff] [blame] | 106 | }, |
| 107 | } |