Skip to content

Commit

Permalink
Merge pull request exercism#786 from exercism/validate-usr-cfg
Browse files Browse the repository at this point in the history
Extract user config validation helper
  • Loading branch information
Katrina Owen authored Dec 26, 2018
2 parents c39243d + 483206e commit 69aa991
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
22 changes: 22 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package cmd

import (
"fmt"

"github.com/exercism/cli/config"
"github.com/spf13/viper"
)

const msgWelcomePleaseConfigure = `
Welcome to Exercism!
Expand Down Expand Up @@ -33,3 +40,18 @@ const msgMissingMetadata = `
Please see https://exercism.io/cli-v1-to-v2 for instructions on how to fix it.
`

// validateUserConfig validates the presense of required user config values
func validateUserConfig(cfg *viper.Viper) error {
if cfg.GetString("token") == "" {
return fmt.Errorf(
msgWelcomePleaseConfigure,
config.SettingsURL(cfg.GetString("apibaseurl")),
BinaryName,
)
}
if cfg.GetString("workspace") == "" || cfg.GetString("apibaseurl") == "" {
return fmt.Errorf(msgRerunConfigure, BinaryName)
}
return nil
}
7 changes: 2 additions & 5 deletions cmd/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,8 @@ Download other people's solutions by providing the UUID.

func runDownload(cfg config.Config, flags *pflag.FlagSet, args []string) error {
usrCfg := cfg.UserViperConfig
if usrCfg.GetString("token") == "" {
return fmt.Errorf(msgWelcomePleaseConfigure, config.SettingsURL(usrCfg.GetString("apibaseurl")), BinaryName)
}
if usrCfg.GetString("workspace") == "" || usrCfg.GetString("apibaseurl") == "" {
return fmt.Errorf(msgRerunConfigure, BinaryName)
if err := validateUserConfig(usrCfg); err != nil {
return err
}

uuid, err := flags.GetString("uuid")
Expand Down
8 changes: 2 additions & 6 deletions cmd/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,8 @@ var submitCmd = &cobra.Command{
func runSubmit(cfg config.Config, flags *pflag.FlagSet, args []string) error {
usrCfg := cfg.UserViperConfig

if usrCfg.GetString("token") == "" {
return fmt.Errorf(msgWelcomePleaseConfigure, config.SettingsURL(usrCfg.GetString("apibaseurl")), BinaryName)
}

if usrCfg.GetString("workspace") == "" {
return fmt.Errorf(msgRerunConfigure, BinaryName)
if err := validateUserConfig(usrCfg); err != nil {
return err
}

for i, arg := range args {
Expand Down
5 changes: 5 additions & 0 deletions cmd/submit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func TestSubmitNonExistentFile(t *testing.T) {
v := viper.New()
v.Set("token", "abc123")
v.Set("workspace", tmpDir)
v.Set("apibaseurl", "http://api.example.com")

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

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

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

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

cfg := config.Config{
Persister: config.InMemoryPersister{},
Expand Down

0 comments on commit 69aa991

Please sign in to comment.