blob: 8a9207c0b97d2a0686fbb9353f045735f3e12045 [file] [log] [blame]
Serge Bazanskibe538db2020-11-12 00:22:42 +01001// 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
16package cmd
17
18import (
Serge Bazanski59c81492021-09-11 12:39:51 +000019 "fmt"
20 "os"
21
22 "github.com/mattn/go-isatty"
Serge Bazanskibe538db2020-11-12 00:22:42 +010023 "github.com/spf13/cobra"
24
25 "code.hackerspace.pl/hscloud/cluster/tools/kartongips/pkg/kubecfg"
26)
27
28const (
Serge Bazanski59c81492021-09-11 12:39:51 +000029 // TODO(b/49): remove this flag
Serge Bazanskibe538db2020-11-12 00:22:42 +010030 flagDiffStrategy = "diff-strategy"
31 flagOmitSecrets = "omit-secrets"
32)
33
34func init() {
Serge Bazanski59c81492021-09-11 12:39:51 +000035 diffCmd.PersistentFlags().String(flagDiffStrategy, "", "Diff strategy - no op (will be removed soon)")
Serge Bazanskibe538db2020-11-12 00:22:42 +010036 diffCmd.PersistentFlags().Bool(flagOmitSecrets, false, "hide secret details when showing diff")
37 RootCmd.AddCommand(diffCmd)
38}
39
Serge Bazanski59c81492021-09-11 12:39:51 +000040// 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
45func 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 Bazanskibe538db2020-11-12 00:22:42 +010067var 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 Bazanski59c81492021-09-11 12:39:51 +000077 diffStrategy, err := flags.GetString(flagDiffStrategy)
Serge Bazanskibe538db2020-11-12 00:22:42 +010078 if err != nil {
79 return err
80 }
Serge Bazanski59c81492021-09-11 12:39:51 +000081 nagStrategy(diffStrategy)
Serge Bazanskibe538db2020-11-12 00:22:42 +010082
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 Bazanski59c81492021-09-11 12:39:51 +0000103 err = c.Run(cmd.Context(), objs, cmd.OutOrStdout())
104 nagStrategy(diffStrategy)
105 return err
Serge Bazanskibe538db2020-11-12 00:22:42 +0100106 },
107}