Skip to content

Commit

Permalink
Fix: Add option to disable colors
Browse files Browse the repository at this point in the history
  • Loading branch information
AP2008 committed Oct 29, 2021
1 parent 4580459 commit d1ca7d9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ Flags:
--insecure-skip-tls-verify If true, the server's certificate will not be checked for validity. This will make your HTTPS connections insecure
--kubeconfig string Path of kubeconfig file of cluster to be scanned
--kubecontext string Kubecontext to be selected
--no-color Display results without color
--select-kinds strings A comma-separated list of kinds to be selected, if left empty all kinds are selected
--select-namespaces strings A comma-separated list of namespaces to be selected, if left empty all namespaces are selected
--source-kubernetes-version string Version of Kubernetes of the cluster on which kubernetes objects are deployed currently, ignored in case cluster is provided. In case of directory defaults to same as target-kubernetes-version.
Expand Down
10 changes: 7 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ var (
ignoredPathPatterns = make([]string, 0)
kubeconfig = ""
kubecontext = ""
noColor = false
// forceColor tells kubedd to use colored output even if
// stdout is not a TTY
forceColor bool
Expand Down Expand Up @@ -96,7 +97,9 @@ var RootCmd = &cobra.Command{
// Assert that colors will definitely be used if requested
if forceColor {
color.NoColor = false
}
} else if noColor {
color.NoColor = true
}

//if len(args) < 1 && len(directories) < 1 && len(kubeconfig) < 1 {
// log.Error(errors.New("at least one file or one directory or kubeconfig path should be passed as argument"))
Expand All @@ -115,7 +118,7 @@ var RootCmd = &cobra.Command{

func processFiles(args []string) bool {
success := true
outputManager := pkg.GetOutputManager(config.OutputFormat)
outputManager := pkg.GetOutputManager(config.OutputFormat, noColor)
files, err := aggregateFiles(args)
if err != nil {
log.Error(err)
Expand Down Expand Up @@ -163,7 +166,7 @@ func processFiles(args []string) bool {

func processCluster() bool {
success := true
outputManager := pkg.GetOutputManager(config.OutputFormat)
outputManager := pkg.GetOutputManager(config.OutputFormat, noColor)
cluster := pkg.NewCluster(kubeconfig, kubecontext)
results, err := kubedd.ValidateCluster(cluster, config)
if err != nil {
Expand Down Expand Up @@ -269,6 +272,7 @@ func init() {
RootCmd.Use = fmt.Sprintf("%s <file> [file...]", rootCmdName)
pkg.AddKubeaddFlags(RootCmd, config)
RootCmd.Flags().BoolVarP(&forceColor, "force-color", "", false, "Force colored output even if stdout is not a TTY")
RootCmd.Flags().BoolVarP(&noColor, "no-color", "", false, "Display results without color")
RootCmd.SetVersionTemplate(`{{.Version}}`)
RootCmd.Flags().StringSliceVarP(&directories, "directories", "d", []string{}, "A comma-separated list of directories to recursively search for YAML documents")
RootCmd.Flags().StringSliceVarP(&ignoredPathPatterns, "ignored-path-patterns", "i", []string{}, "A comma-separated list of regular expressions specifying paths to ignore")
Expand Down
18 changes: 13 additions & 5 deletions pkg/Output.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,26 +64,27 @@ func validOutputs() []string {
}
}

func GetOutputManager(outFmt string) OutputManager {
func GetOutputManager(outFmt string, noColor bool) OutputManager {
switch outFmt {
case outputSTD:
return newSTDOutputManager()
return newSTDOutputManager(noColor)
case outputJSON:
return newDefaultJSONOutputManager()
case outputTAP:
return newDefaultTAPOutputManager()
default:
return newSTDOutputManager()
return newSTDOutputManager(noColor)
}
}

// STDOutputManager reports `kubedd` results to stdout.
type STDOutputManager struct {
noColor bool
}

// newSTDOutputManager instantiates a new instance of STDOutputManager.
func newSTDOutputManager() *STDOutputManager {
return &STDOutputManager{}
func newSTDOutputManager(noColor bool) *STDOutputManager {
return &STDOutputManager{noColor}
}

func (s *STDOutputManager) PutBulk(results []ValidationResult) error {
Expand Down Expand Up @@ -117,7 +118,11 @@ func (s *STDOutputManager) PutBulk(results []ValidationResult) error {
sort.Slice(deleted, func(i, j int) bool {
return len(deleted[i].ErrorsForLatest) > len(deleted[j].ErrorsForLatest)
})
color.NoColor = false
red := color.New(color.FgHiRed, color.Underline).SprintFunc()
if s.noColor {
color.NoColor = true
}
fmt.Printf("%s\n", red(">>>> Removed API Version's <<<<"))
s.SummaryTableBodyOutput(deleted)
fmt.Println("")
Expand Down Expand Up @@ -181,6 +186,7 @@ func (s *STDOutputManager) SummaryTableBodyOutput(results []ValidationResult) {
}
t.Rows = append(t.Rows, []string{result.ResourceNamespace, result.ResourceName, result.Kind, result.APIVersion, result.LatestAPIVersion, migrationStatus})
}
c.Color = !s.noColor
t.WriteTable(os.Stdout, c)
}

Expand Down Expand Up @@ -228,6 +234,7 @@ func (s *STDOutputManager) DeprecationTableBodyOutput(results []ValidationResult
t.Rows = append(t.Rows, []string{result.ResourceNamespace, result.ResourceName, result.Kind, apiVersion, strings.Join(e.JSONPointer(), "/"), e.Reason})
}
}
c.Color = !s.noColor
t.WriteTable(os.Stdout, c)
fmt.Println("")
}
Expand Down Expand Up @@ -278,6 +285,7 @@ func (s *STDOutputManager) ValidationErrorTableBodyOutput(results []ValidationRe
}
}
}
c.Color = !s.noColor
t.WriteTable(os.Stdout, c)
fmt.Println("")
}
Expand Down

0 comments on commit d1ca7d9

Please sign in to comment.