Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion cmd/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ func Configure(ctx *cli.Context) {
host := ctx.String("host")
dir := ctx.String("dir")
api := ctx.String("api")
c.Update(key, host, dir, api)

if err = c.Update(key, host, dir, api); err != nil {
log.Fatalf("Error updating your configuration %s\n", err)
}

if err := os.MkdirAll(c.Dir, os.ModePerm); err != nil {
log.Fatalf("Error creating exercism directory %s\n", err)
Expand Down
30 changes: 25 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func New(path string) (*Config, error) {
}

// Update sets new values where given.
func (c *Config) Update(key, host, dir, xapi string) {
func (c *Config) Update(key, host, dir, xapi string) error {
key = strings.TrimSpace(key)
if key != "" {
c.APIKey = key
Expand All @@ -89,13 +89,17 @@ func (c *Config) Update(key, host, dir, xapi string) {

dir = strings.TrimSpace(dir)
if dir != "" {
c.SetDir(dir)
if err := c.SetDir(dir); err != nil {
return err
}
}

xapi = strings.TrimSpace(xapi)
if xapi != "" {
c.XAPI = xapi
}

return nil
}

// Write saves the config as JSON.
Expand Down Expand Up @@ -253,13 +257,29 @@ func (c *Config) SetDir(path string) error {
if err != nil {
return err
}

var dir string

if path == "" {
c.Dir = filepath.Join(home, DirExercises)
dir = filepath.Join(home, DirExercises)
} else {
c.Dir = path
dir = path
}

c.Dir = expandHome(c.Dir, home)
dir = expandHome(dir, home)

// if the user has provided us with a relative path, make it absolute so
// it will always work
if !filepath.IsAbs(dir) {
wd, err := os.Getwd()
if err != nil {
return err
}
dir = filepath.Join(wd, dir)
}

c.Dir = dir

return nil
}

Expand Down
14 changes: 10 additions & 4 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import (
)

func TestSetDir(t *testing.T) {
dir, err := os.Getwd()
assert.NoError(t, err)

testCases := []struct {
givenPath string
expectedPath string
Expand All @@ -20,7 +23,7 @@ func TestSetDir(t *testing.T) {
{"~/foobar", "/test/home/foobar"},
{"/foobar/~/noexpand", "/foobar/~/noexpand"},
{"/no/modification", "/no/modification"},
{"nomodification", "nomodification"},
{"relativePath", filepath.Join(dir, "relativePath")},
}

for _, testCase := range testCases {
Expand Down Expand Up @@ -184,22 +187,25 @@ func TestUpdateConfig(t *testing.T) {
}

// Test the blank values don't overwrite existing values
c.Update("", "", "", "")
err = c.Update("", "", "", "")
assert.Equal(t, "MyKey", c.APIKey)
assert.Equal(t, "localhost", c.API)
assert.Equal(t, "/exercism/directory", c.Dir)
assert.Equal(t, "localhost", c.XAPI)
assert.NoError(t, err)

// Test that each value can be overwritten
c.Update("NewKey", "http://example.com", "/tmp/exercism", "http://x.example.org")
err = c.Update("NewKey", "http://example.com", "/tmp/exercism", "http://x.example.org")
assert.Equal(t, "NewKey", c.APIKey)
assert.Equal(t, "http://example.com", c.API)
assert.Equal(t, "/tmp/exercism", c.Dir)
assert.Equal(t, "http://x.example.org", c.XAPI)
assert.NoError(t, err)

// Test home is expanded on update
c.Update("", "", "~/myexercism", "")
err = c.Update("", "", "~/myexercism", "")
assert.Equal(t, filepath.Join(tmpDir, "myexercism"), c.Dir)
assert.NoError(t, err)
}

func fixturePath(t *testing.T, filename string) string {
Expand Down