Skip to content

Commit 6a13d00

Browse files
Convert base URL from run/submit flag to config (#76)
* Convert `-b` from run/submit to config * Rename "submit base URL" to "override base URL"
1 parent b283943 commit 6a13d00

File tree

7 files changed

+127
-44
lines changed

7 files changed

+127
-44
lines changed

Diff for: checks/checks.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ func runHTTPRequest(
8888

8989
resp, err := client.Do(req)
9090
if err != nil {
91-
result = api.HTTPRequestResult{Err: "Failed to fetch"}
91+
errString := fmt.Sprintf("Failed to fetch: %s", err.Error())
92+
result = api.HTTPRequestResult{Err: errString}
9293
return result
9394
}
9495
defer resp.Body.Close()
@@ -116,15 +117,15 @@ func runHTTPRequest(
116117
return result
117118
}
118119

119-
func CLIChecks(cliData api.CLIData, submitBaseURL *string) (results []api.CLIStepResult) {
120+
func CLIChecks(cliData api.CLIData, overrideBaseURL *string) (results []api.CLIStepResult) {
120121
client := &http.Client{}
121122
variables := make(map[string]string)
122123
results = make([]api.CLIStepResult, len(cliData.Steps))
123124

124-
// use cli arg url if specified or default lesson data url
125+
// use cli config url if specified or default lesson data url
125126
baseURL := ""
126-
if submitBaseURL != nil && *submitBaseURL != "" {
127-
baseURL = *submitBaseURL
127+
if overrideBaseURL != nil && *overrideBaseURL != "" {
128+
baseURL = *overrideBaseURL
128129
} else if cliData.BaseURL != nil && *cliData.BaseURL != "" {
129130
baseURL = *cliData.BaseURL
130131
}

Diff for: cmd/configure.go

+115-32
Original file line numberDiff line numberDiff line change
@@ -2,62 +2,145 @@ package cmd
22

33
import (
44
"fmt"
5+
"net/url"
56

67
"github.com/charmbracelet/lipgloss"
78
"github.com/spf13/cobra"
89
"github.com/spf13/viper"
910
)
1011

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+
}
1418

1519
var defaultColors = map[string]string{
1620
"gray": "8",
1721
"red": "1",
1822
"green": "2",
1923
}
2024

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+
2660
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
4264
}
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) + "!")
4371
}
72+
4473
if showHelp {
45-
cmd.Help()
46-
} else {
47-
viper.WriteConfig()
74+
return cmd.Help()
4875
}
4976

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
50131
},
51132
}
52133

53134
func init() {
54135
rootCmd.AddCommand(configureCmd)
55136

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)
62145
}
63146
}

Diff for: cmd/login.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ var loginCmd = &cobra.Command{
8989
fmt.Print("Welcome to the boot.dev CLI!\n\n")
9090
}
9191

92-
loginUrl := viper.GetString("base_url") + "/cli/login"
92+
loginUrl := viper.GetString("frontend_url") + "/cli/login"
9393

9494
fmt.Println("Please navigate to:\n" + loginUrl)
9595

Diff for: cmd/root.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func readViperConfig(paths []string) error {
5050

5151
// initConfig reads in config file and ENV variables if set.
5252
func initConfig() {
53-
viper.SetDefault("base_url", "https://boot.dev")
53+
viper.SetDefault("frontend_url", "https://boot.dev")
5454
viper.SetDefault("api_url", "https://api.boot.dev")
5555
viper.SetDefault("access_token", "")
5656
viper.SetDefault("refresh_token", "")

Diff for: cmd/run.go

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66

77
func init() {
88
rootCmd.AddCommand(runCmd)
9-
runCmd.Flags().StringVarP(&submitBaseURL, "baseurl", "b", "", "set the base URL for HTTP tests, overriding any default")
109
runCmd.Flags().BoolVarP(&forceSubmit, "submit", "s", false, "shortcut flag to submit instead of run")
1110
}
1211

Diff for: cmd/submit.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@ import (
77
api "github.com/bootdotdev/bootdev/client"
88
"github.com/bootdotdev/bootdev/render"
99
"github.com/spf13/cobra"
10+
"github.com/spf13/viper"
1011
)
1112

12-
var submitBaseURL string
1313
var forceSubmit bool
1414

1515
func init() {
1616
rootCmd.AddCommand(submitCmd)
17-
submitCmd.Flags().StringVarP(&submitBaseURL, "baseurl", "b", "", "set the base URL for HTTP tests, overriding any default")
1817
}
1918

2019
// submitCmd represents the submit command
@@ -43,7 +42,8 @@ func submissionHandler(cmd *cobra.Command, args []string) error {
4342
}
4443

4544
data := lesson.Lesson.LessonDataCLI.CLIData
46-
results := checks.CLIChecks(data, &submitBaseURL)
45+
overrideBaseURL := viper.GetString("override_base_url")
46+
results := checks.CLIChecks(data, &overrideBaseURL)
4747
if isSubmit {
4848
failure, err := api.SubmitCLILesson(lessonUUID, results)
4949
if err != nil {

Diff for: version.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v1.15.1
1+
v1.16.0

0 commit comments

Comments
 (0)