Skip to content

Commit 5c5f24f

Browse files
author
Katrina Owen
authored
Merge pull request #651 from exercism/no-base-url
Ensure welcome message has full link to settings
2 parents 4f04d21 + 5d367aa commit 5c5f24f

7 files changed

Lines changed: 17 additions & 15 deletions

File tree

cmd/configure.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,8 @@ func runConfigure(configuration config.Configuration, flags *pflag.FlagSet) erro
6060
// If the command is run 'bare' and we have no token,
6161
// explain how to set the token.
6262
if flags.NFlag() == 0 && cfg.GetString("token") == "" {
63-
baseURL := cfg.GetString("apibaseurl")
64-
if baseURL != "" {
65-
// If we have a base URL, then give the exact link.
66-
tokenURL := config.InferSiteURL(baseURL) + "/my/settings"
67-
return fmt.Errorf("There is no token configured. Find your token on %s, and call this command again with --token=<your-token>.", tokenURL)
68-
}
69-
// If we don't, then do our best.
70-
return fmt.Errorf("There is no token configured. Find your token in your settings on the website, and call this command again with --token=<your-token>.")
63+
tokenURL := config.SettingsURL(cfg.GetString("apibaseurl"))
64+
return fmt.Errorf("There is no token configured. Find your token on %s, and call this command again with --token=<your-token>.", tokenURL)
7165
}
7266

7367
// Determine the base API URL.
@@ -114,8 +108,7 @@ func runConfigure(configuration config.Configuration, flags *pflag.FlagSet) erro
114108
token = cfg.GetString("token")
115109
}
116110

117-
// Infer the URL where the token can be found.
118-
tokenURL := config.InferSiteURL(cfg.GetString("apibaseurl")) + "/my/settings"
111+
tokenURL := config.SettingsURL(cfg.GetString("apibaseurl"))
119112

120113
// If we don't have a token then explain how to set it and bail.
121114
if token == "" {

cmd/download.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ Download other people's solutions by providing the UUID.
4949
func runDownload(cfg config.Configuration, flags *pflag.FlagSet, args []string) error {
5050
usrCfg := cfg.UserViperConfig
5151
if usrCfg.GetString("token") == "" {
52-
tokenURL := config.InferSiteURL(usrCfg.GetString("apibaseurl")) + "/my/settings"
53-
return fmt.Errorf(msgWelcomePleaseConfigure, tokenURL, BinaryName)
52+
return fmt.Errorf(msgWelcomePleaseConfigure, config.SettingsURL(usrCfg.GetString("apibaseurl")), BinaryName)
5453
}
5554
if usrCfg.GetString("workspace") == "" || usrCfg.GetString("apibaseurl") == "" {
5655
return fmt.Errorf(msgRerunConfigure, BinaryName)

cmd/download_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ func TestDownloadWithoutToken(t *testing.T) {
2323
err := runDownload(cfg, pflag.NewFlagSet("fake", pflag.PanicOnError), []string{})
2424
if assert.Error(t, err) {
2525
assert.Regexp(t, "Welcome to Exercism", err.Error())
26+
// It uses the default base API url to infer the host
27+
assert.Regexp(t, "exercism.io/my/settings", err.Error())
2628
}
2729
}
2830

cmd/submit.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ func runSubmit(cfg config.Configuration, flags *pflag.FlagSet, args []string) er
5353
usrCfg := cfg.UserViperConfig
5454

5555
if usrCfg.GetString("token") == "" {
56-
tokenURL := config.InferSiteURL(usrCfg.GetString("apibaseurl")) + "/my/settings"
57-
return fmt.Errorf(msgWelcomePleaseConfigure, tokenURL, BinaryName)
56+
return fmt.Errorf(msgWelcomePleaseConfigure, config.SettingsURL(usrCfg.GetString("apibaseurl")), BinaryName)
5857
}
5958

6059
if usrCfg.GetString("workspace") == "" {

cmd/submit_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ func TestSubmitWithoutToken(t *testing.T) {
1919
cfg := config.Configuration{
2020
Persister: config.InMemoryPersister{},
2121
UserViperConfig: viper.New(),
22-
DefaultBaseURL: "http://example.com",
2322
}
2423

2524
err := runSubmit(cfg, pflag.NewFlagSet("fake", pflag.PanicOnError), []string{})
2625
assert.Regexp(t, "Welcome to Exercism", err.Error())
26+
assert.Regexp(t, "exercism.io/my/settings", err.Error())
2727
}
2828

2929
func TestSubmitWithoutWorkspace(t *testing.T) {

config/config.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,17 @@ func ensureDir(f filer) error {
6969
// InferSiteURL guesses what the website URL is.
7070
// The basis for the guess is which API we're submitting to.
7171
func InferSiteURL(apiURL string) string {
72+
if apiURL == "" {
73+
apiURL = defaultBaseURL
74+
}
7275
if apiURL == "https://api.exercism.io/v1" {
7376
return "https://exercism.io"
7477
}
7578
re := regexp.MustCompile("^(https?://[^/]*).*")
7679
return re.ReplaceAllString(apiURL, "$1")
7780
}
81+
82+
// SettingsURL provides a link to where the user can find their API token.
83+
func SettingsURL(apiURL string) string {
84+
return fmt.Sprintf("%s%s", InferSiteURL(apiURL), "/my/settings")
85+
}

config/config_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ func TestInferSiteURL(t *testing.T) {
104104
{"https://v2.exercism.io/api/v1", "https://v2.exercism.io"},
105105
{"https://mentors-beta.exercism.io/api/v1", "https://mentors-beta.exercism.io"},
106106
{"http://localhost:3000/api/v1", "http://localhost:3000"},
107+
{"", "https://exercism.io"}, // use the default
107108
{"http://whatever", "http://whatever"}, // you're on your own, pal
108109
}
109110

0 commit comments

Comments
 (0)