@@ -2,62 +2,145 @@ package cmd
2
2
3
3
import (
4
4
"fmt"
5
+ "net/url"
5
6
6
7
"github.com/charmbracelet/lipgloss"
7
8
"github.com/spf13/cobra"
8
9
"github.com/spf13/viper"
9
10
)
10
11
11
- var configColors []* string
12
-
13
- var defaultColorKeys []string
12
+ // configureCmd represents the configure command which is a container for other
13
+ // sub-commands (e.g., colors, base URL override)
14
+ var configureCmd = & cobra.Command {
15
+ Use : "configure" ,
16
+ Short : "Change configuration of the CLI" ,
17
+ }
14
18
15
19
var defaultColors = map [string ]string {
16
20
"gray" : "8" ,
17
21
"red" : "1" ,
18
22
"green" : "2" ,
19
23
}
20
24
21
- // configureCmd represents the configure command
22
- var configureCmd = & cobra.Command {
23
- Use : "configure" ,
24
- Short : "Change configuration of the CLI" ,
25
- Run : func (cmd * cobra.Command , args []string ) {
25
+ // configureColorsCmd represents the `configure colors` command for changing
26
+ // the colors of the text output
27
+ var configureColorsCmd = & cobra.Command {
28
+ Use : "colors" ,
29
+ Short : "Change the CLI text colors" ,
30
+ RunE : func (cmd * cobra.Command , args []string ) error {
31
+ resetColors , err := cmd .Flags ().GetBool ("reset" )
32
+ if err != nil {
33
+ return fmt .Errorf ("couldn't get the reset flag value: %v" , err )
34
+ }
35
+
36
+ if resetColors {
37
+ for color , defaultVal := range defaultColors {
38
+ viper .Set ("color." + color , defaultVal )
39
+ }
40
+
41
+ err := viper .WriteConfig ()
42
+ if err != nil {
43
+ return fmt .Errorf ("failed to write config: %v" , err )
44
+ }
45
+
46
+ fmt .Println ("Reset colors!" )
47
+ return err
48
+ }
49
+
50
+ configColors := map [string ]string {}
51
+ for color := range defaultColors {
52
+ configVal , err := cmd .Flags ().GetString (color )
53
+ if err != nil {
54
+ return fmt .Errorf ("couldn't get the %v flag value: %v" , color , err )
55
+ }
56
+
57
+ configColors [color ] = configVal
58
+ }
59
+
26
60
showHelp := true
27
- for i , color := range configColors {
28
- key := "color." + defaultColorKeys [i ]
29
- if color != nil {
30
- if * color == "" {
31
- viper .Set (key , defaultColors [defaultColorKeys [i ]])
32
- fmt .Println ("unset " + key )
33
- } else {
34
- if viper .GetString (key ) == * color {
35
- continue
36
- }
37
- viper .Set (key , * color )
38
- style := lipgloss .NewStyle ().Foreground (lipgloss .Color (* color ))
39
- fmt .Println ("set " + style .Render (key ) + "!" )
40
- }
41
- showHelp = false
61
+ for color , configVal := range configColors {
62
+ if configVal == "" {
63
+ continue
42
64
}
65
+
66
+ showHelp = false
67
+ key := "color." + color
68
+ viper .Set (key , configVal )
69
+ style := lipgloss .NewStyle ().Foreground (lipgloss .Color (configVal ))
70
+ fmt .Println ("set " + style .Render (key ) + "!" )
43
71
}
72
+
44
73
if showHelp {
45
- cmd .Help ()
46
- } else {
47
- viper .WriteConfig ()
74
+ return cmd .Help ()
48
75
}
49
76
77
+ err = viper .WriteConfig ()
78
+ if err != nil {
79
+ return fmt .Errorf ("failed to write config: %v" , err )
80
+ }
81
+ return err
82
+ },
83
+ }
84
+
85
+ // configureBaseURLCmd represents the `configure base_url` command
86
+ var configureBaseURLCmd = & cobra.Command {
87
+ Use : "base_url" ,
88
+ Short : "Set the base URL for HTTP tests, overriding lesson defaults" ,
89
+ Args : cobra .RangeArgs (0 , 1 ),
90
+ RunE : func (cmd * cobra.Command , args []string ) error {
91
+ resetOverrideBaseURL , err := cmd .Flags ().GetBool ("reset" )
92
+ if err != nil {
93
+ return fmt .Errorf ("couldn't get the reset flag value: %v" , err )
94
+ }
95
+
96
+ if resetOverrideBaseURL {
97
+ viper .Set ("override_base_url" , "" )
98
+ err := viper .WriteConfig ()
99
+ if err != nil {
100
+ return fmt .Errorf ("failed to write config: %v" , err )
101
+ }
102
+ fmt .Println ("Reset base URL!" )
103
+ return err
104
+ }
105
+
106
+ if len (args ) == 0 {
107
+ return cmd .Help ()
108
+ }
109
+
110
+ overrideBaseURL , err := url .Parse (args [0 ])
111
+ if err != nil {
112
+ return fmt .Errorf ("failed to parse base URL: %v" , err )
113
+ }
114
+ // for urls like "localhost:8080" the parser reads "localhost" into
115
+ // `Scheme` and leaves `Host` as an empty string, so we must check for
116
+ // both
117
+ if overrideBaseURL .Scheme == "" || overrideBaseURL .Host == "" {
118
+ return fmt .Errorf ("invalid URL: provide both protocol scheme and hostname" )
119
+ }
120
+ if overrideBaseURL .Scheme == "https" {
121
+ fmt .Println ("warning: protocol scheme is set to https" )
122
+ }
123
+
124
+ viper .Set ("override_base_url" , overrideBaseURL .String ())
125
+ err = viper .WriteConfig ()
126
+ if err != nil {
127
+ return fmt .Errorf ("failed to write config: %v" , err )
128
+ }
129
+ fmt .Printf ("Base URL set to %v\n " , overrideBaseURL .String ())
130
+ return err
50
131
},
51
132
}
52
133
53
134
func init () {
54
135
rootCmd .AddCommand (configureCmd )
55
136
56
- configColors = make ([]* string , len (defaultColors ))
57
- defaultColorKeys = make ([]string , len (defaultColors ))
58
- for color , def := range defaultColors {
59
- configColors = append (configColors , configureCmd .Flags ().String ("color-" + color , def , "ANSI number or hex string" ))
60
- defaultColorKeys = append (defaultColorKeys , color )
61
- viper .SetDefault ("color." + color , def )
137
+ configureCmd .AddCommand (configureBaseURLCmd )
138
+ configureBaseURLCmd .Flags ().Bool ("reset" , false , "reset the base URL to use the lesson's defaults" )
139
+
140
+ configureCmd .AddCommand (configureColorsCmd )
141
+ configureColorsCmd .Flags ().Bool ("reset" , false , "reset colors to their default values" )
142
+ for color , defaultVal := range defaultColors {
143
+ configureColorsCmd .Flags ().String (color , "" , "ANSI number or hex string" )
144
+ viper .SetDefault ("color." + color , defaultVal )
62
145
}
63
146
}
0 commit comments