New creates a new cobra client
()
| 33 | |
| 34 | // New creates a new cobra client |
| 35 | func New() *cobra.Command { |
| 36 | chartCommand := newChartCommand() |
| 37 | |
| 38 | cmd := &cobra.Command{ |
| 39 | Use: "diff", |
| 40 | Short: "Show manifest differences", |
| 41 | Long: rootCmdLongUsage, |
| 42 | //Alias root command to chart subcommand |
| 43 | Args: chartCommand.Args, |
| 44 | // parse the flags and check for actions like suppress-secrets, no-colors |
| 45 | PersistentPreRun: func(cmd *cobra.Command, args []string) { |
| 46 | var fc *bool |
| 47 | |
| 48 | if cmd.Flags().Changed("color") { |
| 49 | v, _ := cmd.Flags().GetBool("color") |
| 50 | fc = &v |
| 51 | } else { |
| 52 | v, err := strconv.ParseBool(os.Getenv("HELM_DIFF_COLOR")) |
| 53 | if err == nil { |
| 54 | fc = &v |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | if !cmd.Flags().Changed("output") { |
| 59 | v, set := os.LookupEnv("HELM_DIFF_OUTPUT") |
| 60 | if set && strings.TrimSpace(v) != "" { |
| 61 | _ = cmd.Flags().Set("output", v) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | // Dyff relies on bunt, default to color=on |
| 66 | bunt.SetColorSettings(bunt.ON, bunt.ON) |
| 67 | nc, _ := cmd.Flags().GetBool("no-color") |
| 68 | |
| 69 | if nc || (fc != nil && !*fc) { |
| 70 | ansi.DisableColors(true) |
| 71 | bunt.SetColorSettings(bunt.OFF, bunt.OFF) |
| 72 | } else if !cmd.Flags().Changed("no-color") && fc == nil { |
| 73 | term := term.IsTerminal(int(os.Stdout.Fd())) |
| 74 | // https://github.com/databus23/helm-diff/issues/281 |
| 75 | dumb := os.Getenv("TERM") == "dumb" |
| 76 | ansi.DisableColors(!term || dumb) |
| 77 | bunt.SetColorSettings(bunt.OFF, bunt.OFF) |
| 78 | } |
| 79 | }, |
| 80 | RunE: func(cmd *cobra.Command, args []string) error { |
| 81 | cmd.Println(`Command "helm diff" is deprecated, use "helm diff upgrade" instead`) |
| 82 | return chartCommand.RunE(cmd, args) |
| 83 | }, |
| 84 | } |
| 85 | |
| 86 | // add no-color as global flag |
| 87 | cmd.PersistentFlags().Bool("no-color", false, "remove colors from the output. If both --no-color and --color are unspecified, coloring enabled only when the stdout is a term and TERM is not \"dumb\"") |
| 88 | cmd.PersistentFlags().Bool("color", false, "color output. You can control the value for this flag via HELM_DIFF_COLOR=[true|false]. If both --no-color and --color are unspecified, coloring enabled only when the stdout is a term and TERM is not \"dumb\"") |
| 89 | // add flagset from chartCommand |
| 90 | cmd.Flags().AddFlagSet(chartCommand.Flags()) |
| 91 | cmd.AddCommand(newVersionCmd(), chartCommand) |
| 92 | // add subcommands |