Add ability to force colors: option --color=auto|on|off
Add shell completion for --color as well.
--- a/cmd/root.go Mon May 01 12:42:37 2017 +0200
+++ b/cmd/root.go Mon May 01 13:03:06 2017 +0200
@@ -30,6 +30,7 @@
var verbose bool
var outputFormat string
var outputTemplate, outputTemplateFile string
+var colorMode string
// Shell completion functions
const shellComplFunc = `
@@ -39,6 +40,9 @@
__madonctl_output() {
COMPREPLY=( plain json yaml template )
}
+__madonctl_color() {
+ COMPREPLY=( auto on off )
+}
`
// RootCmd represents the base command when called without any subcommands
@@ -124,6 +128,8 @@
"Go template (for output=template)")
RootCmd.PersistentFlags().StringVar(&outputTemplateFile, "template-file", "",
"Go template file (for output=template)")
+ RootCmd.PersistentFlags().StringVar(&colorMode, "color", "",
+ "Color mode (auto|on|off; for output=template)")
// Configuration file bindings
viper.BindPFlag("output", RootCmd.PersistentFlags().Lookup("output"))
@@ -133,12 +139,16 @@
viper.BindPFlag("login", RootCmd.PersistentFlags().Lookup("login"))
viper.BindPFlag("password", RootCmd.PersistentFlags().Lookup("password"))
viper.BindPFlag("token", RootCmd.PersistentFlags().Lookup("token"))
+ viper.BindPFlag("color", RootCmd.PersistentFlags().Lookup("color"))
// Flag completion
- annotation := make(map[string][]string)
- annotation[cobra.BashCompCustom] = []string{"__madonctl_output"}
+ annotationOutput := make(map[string][]string)
+ annotationOutput[cobra.BashCompCustom] = []string{"__madonctl_output"}
+ annotationColor := make(map[string][]string)
+ annotationColor[cobra.BashCompCustom] = []string{"__madonctl_color"}
- RootCmd.PersistentFlags().Lookup("output").Annotations = annotation
+ RootCmd.PersistentFlags().Lookup("output").Annotations = annotationOutput
+ RootCmd.PersistentFlags().Lookup("color").Annotations = annotationColor
}
func checkOutputFormat(cmd *cobra.Command, args []string) error {
@@ -190,6 +200,14 @@
var opt string
of := getOutputFormat()
+ // Initialize color mode
+ switch viper.GetString("color") {
+ case "on", "yes", "force":
+ printer.ColorMode = 1
+ case "off", "no":
+ printer.ColorMode = 2
+ }
+
if of == "template" {
opt = outputTemplate
if outputTemplateFile != "" {
--- a/printer/templateprinter.go Mon May 01 12:42:37 2017 +0200
+++ b/printer/templateprinter.go Mon May 01 13:03:06 2017 +0200
@@ -20,8 +20,11 @@
"github.com/McKael/madonctl/printer/colors"
)
-// DisableColors can be set to true to disable the color template function
-var DisableColors bool
+// ColorMode defines the color behaviour: 0=auto, 1=forced, 2=disabled
+var ColorMode int
+
+// disableColors can be set to true to disable the color template function
+var disableColors bool
// TemplatePrinter represents a Template printer
type TemplatePrinter struct {
@@ -42,9 +45,10 @@
return nil, err
}
- // Check if stdout is a TTY
- if !isatty.IsTerminal(os.Stdout.Fd()) {
- DisableColors = true
+ // Update disableColors.
+ // In auto-mode, check if stdout is a TTY.
+ if ColorMode == 2 || (ColorMode == 0 && !isatty.IsTerminal(os.Stdout.Fd())) {
+ disableColors = true
}
return &TemplatePrinter{
@@ -130,7 +134,7 @@
}
func ansiColor(desc string) (string, error) {
- if DisableColors {
+ if disableColors {
return "", nil
}
return colors.ANSICodeString(desc)