Skip to content

Commit 616f75a

Browse files
committed
add disable color flag
1 parent 6c5fd22 commit 616f75a

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

pkg/df-pv/root.go

+22-15
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ func InitAndExecute() {
3636
type flagpole struct {
3737
logLevel string
3838
genericCliConfigFlags *genericclioptions.ConfigFlags
39+
disableColor bool
3940
}
4041

4142
func setupRootCommand() *cobra.Command {
@@ -55,6 +56,7 @@ It colors the values based on "severity" [red: > 75% (too high); yellow: < 25% (
5556
}
5657

5758
rootCmd.PersistentFlags().StringVarP(&flags.logLevel, "verbosity", "v", "info", "log level; one of [info, debug, trace, warn, error, fatal, panic]")
59+
rootCmd.Flags().BoolVarP(&flags.disableColor, "disable-color", "d", false, "boolean flag for disabling colored output")
5860

5961
flags.genericCliConfigFlags = genericclioptions.NewConfigFlags(false)
6062
flags.genericCliConfigFlags.AddFlags(rootCmd.Flags())
@@ -82,28 +84,33 @@ func runRootCommand(flags *flagpole) error {
8284
}
8385
log.Infof("Either no volumes found in namespace/s: '%s' or the storage provisioner used for the volumes does not publish metrics to kubelet", ns)
8486
} else {
85-
PrintUsingGoPretty(sliceOfOutputRowPVC)
87+
PrintUsingGoPretty(sliceOfOutputRowPVC, flags.disableColor)
8688
}
8789

8890
return nil
8991
}
9092

91-
func PrintUsingGoPretty(sliceOfOutputRowPVC []*OutputRowPVC) {
93+
func PrintUsingGoPretty(sliceOfOutputRowPVC []*OutputRowPVC, disableColor bool) {
94+
if disableColor {
95+
text.DisableColors()
96+
}
9297
// https://github.com/jedib0t/go-pretty/tree/v6.0.4/table
9398
t := table.NewWriter()
9499

95100
t.AppendHeader(table.Row{"PV Name", "PVC Name", "Namespace", "Node Name", "Pod Name", "Volume Mount Name", "Size", "Used", "Available", "%Used", "iused", "ifree", "%iused"})
96-
hiWhiteColor := text.FgHiWhite
101+
var whiteColor = text.FgWhite
102+
var percentageUsedColor text.Color
103+
var percentageIUsedColor text.Color
97104
for _, pvcRow := range sliceOfOutputRowPVC {
98-
percentageUsedColor := GetColorFromPercentageUsed(pvcRow.PercentageUsed)
99-
percentageIUsedColor := GetColorFromPercentageUsed(pvcRow.PercentageIUsed)
105+
percentageUsedColor = GetColorFromPercentageUsed(pvcRow.PercentageUsed)
106+
percentageIUsedColor = GetColorFromPercentageUsed(pvcRow.PercentageIUsed)
100107
t.AppendRow([]interface{}{
101-
hiWhiteColor.Sprintf("%s", pvcRow.PVName),
102-
hiWhiteColor.Sprintf("%s", pvcRow.PVCName),
103-
hiWhiteColor.Sprintf("%s", pvcRow.Namespace),
104-
hiWhiteColor.Sprintf("%s", pvcRow.NodeName),
105-
hiWhiteColor.Sprintf("%s", pvcRow.PodName),
106-
hiWhiteColor.Sprintf("%s", pvcRow.VolumeMountName),
108+
fmt.Sprintf("%s", pvcRow.PVName),
109+
fmt.Sprintf("%s", pvcRow.PVCName),
110+
fmt.Sprintf("%s", pvcRow.Namespace),
111+
fmt.Sprintf("%s", pvcRow.NodeName),
112+
fmt.Sprintf("%s", pvcRow.PodName),
113+
fmt.Sprintf("%s", pvcRow.VolumeMountName),
107114
percentageUsedColor.Sprintf("%s", ConvertQuantityValueToHumanReadableIECString(pvcRow.CapacityBytes)),
108115
percentageUsedColor.Sprintf("%s", ConvertQuantityValueToHumanReadableIECString(pvcRow.UsedBytes)),
109116
percentageUsedColor.Sprintf("%s", ConvertQuantityValueToHumanReadableIECString(pvcRow.AvailableBytes)),
@@ -118,7 +125,7 @@ func PrintUsingGoPretty(sliceOfOutputRowPVC []*OutputRowPVC) {
118125
styleBold := table.StyleBold
119126
styleBold.Options = table.OptionsNoBordersAndSeparators
120127
t.SetStyle(styleBold)
121-
t.Style().Color.Header = text.Colors{hiWhiteColor, text.Bold}
128+
t.Style().Color.Header = text.Colors{whiteColor, text.Bold}
122129
// t.Style().Options.SeparateRows = true
123130
// t.SetAutoIndex(true)
124131
// t.SetOutputMirror(os.Stdout)
@@ -127,11 +134,11 @@ func PrintUsingGoPretty(sliceOfOutputRowPVC []*OutputRowPVC) {
127134

128135
func GetColorFromPercentageUsed(percentageUsed float64) text.Color {
129136
if percentageUsed > 75 {
130-
return text.FgHiRed
137+
return text.FgRed
131138
} else if percentageUsed < 25 {
132-
return text.FgHiYellow
139+
return text.FgYellow
133140
} else {
134-
return text.FgHiGreen
141+
return text.FgGreen
135142
}
136143
}
137144

0 commit comments

Comments
 (0)