Skip to content

Commit 1227075

Browse files
committed
Merge pull request #147 from morphatic/master
added --api option to exercism configure; updated tests
2 parents 69f85c1 + 526554d commit 1227075

File tree

5 files changed

+36
-6
lines changed

5 files changed

+36
-6
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ If that throws an error, try ```brew install go --cross-compile-common --with-ll
1919

2020
Development
2121
===========
22+
1. fork this repo
2223
1. `go get github.com/exercism/cli/exercism`
2324
1. `cd $GOPATH/src/github.com/exercism/cli`
25+
1. `git remote set-url origin https://github.com/<your-github-username>/cli`
2426
1. `go get`
2527
1. `go get github.com/levicook/glitch`
2628
1. `go install github.com/levicook/glitch`

cmd/configure.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ func Configure(ctx *cli.Context) {
2222
key := ctx.String("key")
2323
host := ctx.String("host")
2424
dir := ctx.String("dir")
25-
c.Update(key, host, dir)
25+
api := ctx.String("api")
26+
c.Update(key, host, dir, api)
2627

2728
if err := os.MkdirAll(c.Dir, os.ModePerm); err != nil {
2829
log.Fatalf("Error creating exercism directory %s\n", err)

config/config.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,18 @@ func Read(file string) (*Config, error) {
7979

8080
// New returns a new config.
8181
// It will attempt to set defaults where no value is passed in.
82-
func New(key, host, dir string) (*Config, error) {
82+
func New(key, host, dir, xapi string) (*Config, error) {
8383
c := &Config{
8484
APIKey: key,
8585
API: host,
8686
Dir: dir,
87+
XAPI: xapi,
8788
}
8889
return c.configure()
8990
}
9091

9192
// Update sets new values where given.
92-
func (c *Config) Update(key, host, dir string) {
93+
func (c *Config) Update(key, host, dir, xapi string) {
9394
if key != "" {
9495
c.APIKey = key
9596
}
@@ -101,6 +102,11 @@ func (c *Config) Update(key, host, dir string) {
101102
if dir != "" {
102103
c.Dir = dir
103104
}
105+
106+
if xapi != "" {
107+
c.XAPI = xapi
108+
}
109+
104110
c.configure()
105111
}
106112

config/config_test.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ func TestCustomValues(t *testing.T) {
2626
APIKey: "abc123",
2727
API: "http://example.org",
2828
Dir: "/path/to/exercises",
29+
XAPI: "http://x.example.org",
2930
}
3031
c.configure()
3132
assert.Equal(t, "abc123", c.APIKey)
3233
assert.Equal(t, "http://example.org", c.API)
3334
assert.Equal(t, "/path/to/exercises", c.Dir)
35+
assert.Equal(t, "http://x.example.org", c.XAPI)
3436
}
3537

3638
func TestExpandHomeDir(t *testing.T) {
@@ -45,11 +47,13 @@ func TestSanitizeWhitespace(t *testing.T) {
4547
APIKey: " abc123\n\r\n ",
4648
API: " ",
4749
Dir: " \r\n/path/to/exercises \r\n",
50+
XAPI: " ",
4851
}
4952
c.configure()
5053
assert.Equal(t, "abc123", c.APIKey)
5154
assert.Equal(t, "http://exercism.io", c.API)
5255
assert.Equal(t, "/path/to/exercises", c.Dir)
56+
assert.Equal(t, "http://x.exercism.io", c.XAPI)
5357
}
5458

5559
func TestFilePath(t *testing.T) {
@@ -71,6 +75,7 @@ func TestReadNonexistantConfig(t *testing.T) {
7175
assert.NoError(t, err)
7276
assert.Equal(t, c.APIKey, "")
7377
assert.Equal(t, c.API, "http://exercism.io")
78+
assert.Equal(t, c.XAPI, "http://x.exercism.io")
7479
assert.False(t, c.IsAuthenticated())
7580
if !strings.HasSuffix(c.Dir, filepath.FromSlash("/exercism")) {
7681
t.Fatal("Default unconfigured config should use home dir")
@@ -86,6 +91,7 @@ func TestReadingWritingConfig(t *testing.T) {
8691
APIKey: "MyKey",
8792
Dir: "/exercism/directory",
8893
API: "localhost",
94+
XAPI: "localhost",
8995
}
9096
c1.configure()
9197

@@ -98,29 +104,40 @@ func TestReadingWritingConfig(t *testing.T) {
98104
assert.Equal(t, c1.APIKey, c2.APIKey)
99105
assert.Equal(t, c1.Dir, c2.Dir)
100106
assert.Equal(t, c1.API, c2.API)
107+
assert.Equal(t, c1.XAPI, c2.XAPI)
101108
}
102109

103110
func TestUpdateConfig(t *testing.T) {
104111
c := &Config{
105112
APIKey: "MyKey",
106113
Dir: "/exercism/directory",
107114
API: "localhost",
115+
XAPI: "localhost",
108116
}
109117

110-
c.Update("NewKey", "", "")
118+
c.Update("NewKey", "", "", "")
111119
assert.Equal(t, "NewKey", c.APIKey)
112120
assert.Equal(t, "localhost", c.API)
113121
assert.Equal(t, "/exercism/directory", c.Dir)
122+
assert.Equal(t, "localhost", c.XAPI)
114123

115-
c.Update("", "http://example.com", "")
124+
c.Update("", "http://example.com", "", "")
116125
assert.Equal(t, "NewKey", c.APIKey)
117126
assert.Equal(t, "http://example.com", c.API)
118127
assert.Equal(t, "/exercism/directory", c.Dir)
128+
assert.Equal(t, "localhost", c.XAPI)
129+
130+
c.Update("", "", "/tmp/exercism", "")
131+
assert.Equal(t, "NewKey", c.APIKey)
132+
assert.Equal(t, "http://example.com", c.API)
133+
assert.Equal(t, "/tmp/exercism", c.Dir)
134+
assert.Equal(t, "localhost", c.XAPI)
119135

120-
c.Update("", "", "/tmp/exercism")
136+
c.Update("", "", "", "http://x.example.org")
121137
assert.Equal(t, "NewKey", c.APIKey)
122138
assert.Equal(t, "http://example.com", c.API)
123139
assert.Equal(t, "/tmp/exercism", c.Dir)
140+
assert.Equal(t, "http://x.example.org", c.XAPI)
124141
}
125142

126143
func TestReadDefaultConfig(t *testing.T) {

exercism/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ func main() {
7272
Name: "key, k",
7373
Usage: "exercism.io API key (see http://exercism.io/account)",
7474
},
75+
cli.StringFlag{
76+
Name: "api, a",
77+
Usage: "exercism xapi host",
78+
},
7579
},
7680
Action: cmd.Configure,
7781
},

0 commit comments

Comments
 (0)