/
color.go
71 lines (65 loc) · 1.79 KB
/
color.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* Copyright (c) 2023 Oracle and/or its affiliates.
* Licensed under the Universal Permissive License v 1.0 as shown at
* https://oss.oracle.com/licenses/upl.
*/
package cmd
import (
"errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
const (
setColorMsg = "Color formatting is now set to "
setColorError = "you can only specify 'on' or 'off'"
getColorMsg = "Color formatting is: "
)
// setColorCmd represents the set color command.
var setColorCmd = &cobra.Command{
Use: "color {on|off}",
Short: "set color formatting to be on or off",
Long: `The 'set color' command sets color formatting to on or off. If 'on' then formatting
of output when using a terminal highlights columns requiring attention.`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
displayErrorAndExit(cmd, setError)
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
value := args[0]
var color string
if value == on {
color = on
} else if value == off {
color = off
} else {
return errors.New(setColorError)
}
viper.Set(colorContextKey, color)
err := WriteConfig()
if err != nil {
return err
}
cmd.Println(setColorMsg + value)
return nil
},
}
// getColorCmd represents the get color command.
var getColorCmd = &cobra.Command{
Use: "color",
Short: "display the current color formatting setting",
Long: `The 'get color' command displays the current color formatting setting. If 'on' then formatting
of output when using a terminal highlights columns requiring attention.`,
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
var color string
if Config.Color == "" || Config.Color == on {
color = on
} else {
color = off
}
cmd.Printf("%s%v\n", getColorMsg, color)
return nil
},
}