Skip to content

Commit bcdaae0

Browse files
authored
Make color output configurable (#63)
1 parent 3b4a3db commit bcdaae0

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

cmd/kubectl-tree/rootcmd.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222
"os"
2323
"strings"
2424

25+
"github.com/fatih/color"
26+
"github.com/pkg/errors"
2527
"github.com/spf13/cobra"
2628
"github.com/spf13/pflag"
2729
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -34,6 +36,7 @@ import (
3436

3537
const (
3638
allNamespacesFlag = "all-namespaces"
39+
colorFlag = "color"
3740
)
3841

3942
var cf *genericclioptions.ConfigFlags
@@ -64,12 +67,23 @@ func versionString() string {
6467
}
6568

6669
func run(command *cobra.Command, args []string) error {
67-
6870
allNs, err := command.Flags().GetBool(allNamespacesFlag)
6971
if err != nil {
7072
allNs = false
7173
}
7274

75+
colorArg, err := command.Flags().GetString(colorFlag)
76+
if err != nil {
77+
return err
78+
}
79+
if colorArg == "always" {
80+
color.NoColor = false
81+
} else if colorArg == "never" {
82+
color.NoColor = true
83+
} else if colorArg != "auto" {
84+
return errors.Errorf("invalid value for --%s", colorFlag)
85+
}
86+
7387
restConfig, err := cf.ToRESTConfig()
7488
if err != nil {
7589
return err
@@ -165,6 +179,7 @@ func init() {
165179
cf = genericclioptions.NewConfigFlags(true)
166180

167181
rootCmd.Flags().BoolP(allNamespacesFlag, "A", false, "query all objects in all API groups, both namespaced and non-namespaced")
182+
rootCmd.Flags().StringP(colorFlag, "c", "auto", "Enable or disable color output. This can be 'always', 'never', or 'auto' (default = use color only if using tty). The flag is overridden by the NO_COLOR env variable if set.")
168183

169184
cf.AddFlags(rootCmd.Flags())
170185
if err := flag.Set("logtostderr", "true"); err != nil {

0 commit comments

Comments
 (0)