Skip to content

Commit 69aa991

Browse files
author
Katrina Owen
authored
Merge pull request #786 from exercism/validate-usr-cfg
Extract user config validation helper
2 parents c39243d + 483206e commit 69aa991

File tree

4 files changed

+31
-11
lines changed

4 files changed

+31
-11
lines changed

cmd/cmd.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package cmd
22

3+
import (
4+
"fmt"
5+
6+
"github.com/exercism/cli/config"
7+
"github.com/spf13/viper"
8+
)
9+
310
const msgWelcomePleaseConfigure = `
411
512
Welcome to Exercism!
@@ -33,3 +40,18 @@ const msgMissingMetadata = `
3340
Please see https://exercism.io/cli-v1-to-v2 for instructions on how to fix it.
3441
3542
`
43+
44+
// validateUserConfig validates the presense of required user config values
45+
func validateUserConfig(cfg *viper.Viper) error {
46+
if cfg.GetString("token") == "" {
47+
return fmt.Errorf(
48+
msgWelcomePleaseConfigure,
49+
config.SettingsURL(cfg.GetString("apibaseurl")),
50+
BinaryName,
51+
)
52+
}
53+
if cfg.GetString("workspace") == "" || cfg.GetString("apibaseurl") == "" {
54+
return fmt.Errorf(msgRerunConfigure, BinaryName)
55+
}
56+
return nil
57+
}

cmd/download.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,8 @@ Download other people's solutions by providing the UUID.
5050

5151
func runDownload(cfg config.Config, flags *pflag.FlagSet, args []string) error {
5252
usrCfg := cfg.UserViperConfig
53-
if usrCfg.GetString("token") == "" {
54-
return fmt.Errorf(msgWelcomePleaseConfigure, config.SettingsURL(usrCfg.GetString("apibaseurl")), BinaryName)
55-
}
56-
if usrCfg.GetString("workspace") == "" || usrCfg.GetString("apibaseurl") == "" {
57-
return fmt.Errorf(msgRerunConfigure, BinaryName)
53+
if err := validateUserConfig(usrCfg); err != nil {
54+
return err
5855
}
5956

6057
uuid, err := flags.GetString("uuid")

cmd/submit.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,8 @@ var submitCmd = &cobra.Command{
5151
func runSubmit(cfg config.Config, flags *pflag.FlagSet, args []string) error {
5252
usrCfg := cfg.UserViperConfig
5353

54-
if usrCfg.GetString("token") == "" {
55-
return fmt.Errorf(msgWelcomePleaseConfigure, config.SettingsURL(usrCfg.GetString("apibaseurl")), BinaryName)
56-
}
57-
58-
if usrCfg.GetString("workspace") == "" {
59-
return fmt.Errorf(msgRerunConfigure, BinaryName)
54+
if err := validateUserConfig(usrCfg); err != nil {
55+
return err
6056
}
6157

6258
for i, arg := range args {

cmd/submit_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ func TestSubmitNonExistentFile(t *testing.T) {
5454
v := viper.New()
5555
v.Set("token", "abc123")
5656
v.Set("workspace", tmpDir)
57+
v.Set("apibaseurl", "http://api.example.com")
5758

5859
cfg := config.Config{
5960
Persister: config.InMemoryPersister{},
@@ -92,6 +93,7 @@ func TestSubmitExerciseWithoutMetadataFile(t *testing.T) {
9293
v := viper.New()
9394
v.Set("token", "abc123")
9495
v.Set("workspace", tmpDir)
96+
v.Set("apibaseurl", "http://api.example.com")
9597

9698
cfg := config.Config{
9799
Persister: config.InMemoryPersister{},
@@ -113,6 +115,7 @@ func TestSubmitFilesAndDir(t *testing.T) {
113115
v := viper.New()
114116
v.Set("token", "abc123")
115117
v.Set("workspace", tmpDir)
118+
v.Set("apibaseurl", "http://api.example.com")
116119

117120
cfg := config.Config{
118121
Persister: config.InMemoryPersister{},
@@ -426,6 +429,7 @@ func TestSubmitOnlyEmptyFile(t *testing.T) {
426429
v := viper.New()
427430
v.Set("token", "abc123")
428431
v.Set("workspace", tmpDir)
432+
v.Set("apibaseurl", "http://api.example.com")
429433

430434
cfg := config.Config{
431435
Persister: config.InMemoryPersister{},
@@ -465,6 +469,7 @@ func TestSubmitFilesFromDifferentSolutions(t *testing.T) {
465469
v := viper.New()
466470
v.Set("token", "abc123")
467471
v.Set("workspace", tmpDir)
472+
v.Set("apibaseurl", "http://api.example.com")
468473

469474
cfg := config.Config{
470475
Persister: config.InMemoryPersister{},

0 commit comments

Comments
 (0)