Skip to content

Commit 7f0b911

Browse files
committed
Deprecate problems host in config
1 parent 0d25a87 commit 7f0b911

6 files changed

Lines changed: 24 additions & 17 deletions

File tree

‎api/api.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func Fetch(url string) ([]*Problem, error) {
5454
}
5555

5656
func Demo(c *config.Config) ([]*Problem, error) {
57-
url := fmt.Sprintf("%s/problems/demo?key=%s", c.ProblemsHost, c.APIKey)
57+
url := fmt.Sprintf("%s/problems/demo?key=%s", c.XAPI, c.APIKey)
5858

5959
return Fetch(url)
6060
}

‎config/config.go‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,17 @@ var (
3636
// This defines both the auth for talking to the API, as well as
3737
// where to put problems that get downloaded.
3838
type Config struct {
39-
APIKey string `json:"apiKey"`
40-
Dir string `json:"dir"`
41-
API string `json:"api"`
42-
ProblemsHost string `json:"problemsHost"`
43-
home string // cache user's home directory
44-
file string // full path to config file
39+
APIKey string `json:"apiKey"`
40+
Dir string `json:"dir"`
41+
API string `json:"api"`
42+
XAPI string `json:"xapi"`
43+
home string // cache user's home directory
44+
file string // full path to config file
4545

4646
// deprecated, get rid of them when nobody uses 1.7.0 anymore
4747
ExercismDirectory string `json:"exercismDirectory,omitempty"`
4848
Hostname string `json:"hostname,omitempty"`
49+
ProblemsHost string `json:"problemsHost,omitempty"`
4950
}
5051

5152
// Home returns the user's canonical home directory.
@@ -139,6 +140,7 @@ func (c *Config) Write() error {
139140
renameLegacy()
140141
c.ExercismDirectory = ""
141142
c.Hostname = ""
143+
c.ProblemsHost = ""
142144

143145
// truncates existing file if it exists
144146
f, err := os.Create(c.file)
@@ -162,8 +164,12 @@ func (c *Config) configure() (*Config, error) {
162164
c.API = hostAPI
163165
}
164166

165-
if c.ProblemsHost == "" {
166-
c.ProblemsHost = hostXAPI
167+
if c.ProblemsHost != "" {
168+
c.XAPI = c.ProblemsHost
169+
}
170+
171+
if c.XAPI == "" {
172+
c.XAPI = hostXAPI
167173
}
168174

169175
dir, err := c.homeDir()
@@ -217,6 +223,7 @@ func (c *Config) sanitize() {
217223
c.APIKey = strings.TrimSpace(c.APIKey)
218224
c.Dir = strings.TrimSpace(c.Dir)
219225
c.API = strings.TrimSpace(c.API)
226+
c.XAPI = strings.TrimSpace(c.XAPI)
220227
c.Hostname = strings.TrimSpace(c.Hostname)
221228
c.ProblemsHost = strings.TrimSpace(c.ProblemsHost)
222229
}

‎config/config_test.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func TestReadDefaultConfig(t *testing.T) {
108108
assert.Equal(t, "abc123", c.APIKey)
109109
assert.Equal(t, "/path/to/exercism", c.Dir)
110110
assert.Equal(t, "http://example.com", c.API)
111-
assert.Equal(t, "http://x.example.com", c.ProblemsHost)
111+
assert.Equal(t, "http://x.example.com", c.XAPI)
112112
}
113113

114114
func TestReadCustomConfig(t *testing.T) {
@@ -122,7 +122,7 @@ func TestReadCustomConfig(t *testing.T) {
122122
assert.Equal(t, "xyz000", c.APIKey)
123123
assert.Equal(t, "/tmp/exercism", c.Dir)
124124
assert.Equal(t, "http://example.org", c.API)
125-
assert.Equal(t, "http://x.example.org", c.ProblemsHost)
125+
assert.Equal(t, "http://x.example.org", c.XAPI)
126126
}
127127

128128
func TestReadLegacyConfig(t *testing.T) {
@@ -136,5 +136,5 @@ func TestReadLegacyConfig(t *testing.T) {
136136
assert.Equal(t, "prq567", c.APIKey)
137137
assert.Equal(t, "/tmp/stuff", c.Dir)
138138
assert.Equal(t, "http://api.example.com", c.API)
139-
assert.Equal(t, "http://problems.example.com", c.ProblemsHost)
139+
assert.Equal(t, "http://problems.example.com", c.XAPI)
140140
}

‎fixtures/home/.exercism.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"apiKey":"abc123","dir":"/path/to/exercism","api":"http://example.com","problemsHost":"http://x.example.com"}
1+
{"apiKey":"abc123","dir":"/path/to/exercism","api":"http://example.com","xapi":"http://x.example.com"}

‎fixtures/home/custom.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"apiKey":"xyz000","dir":"/tmp/exercism","api":"http://example.org","problemsHost":"http://x.example.org"}
1+
{"apiKey":"xyz000","dir":"/tmp/exercism","api":"http://example.org","xapi":"http://x.example.org"}

‎handlers/fetch.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ func Fetch(ctx *cli.Context) {
2020
var url string
2121
switch len(args) {
2222
case 0:
23-
url = fmt.Sprintf("%s/%s?key=%s", c.ProblemsHost, "v2/exercises", c.APIKey)
23+
url = fmt.Sprintf("%s/%s?key=%s", c.XAPI, "v2/exercises", c.APIKey)
2424
case 1:
25-
url = fmt.Sprintf("%s/%s/%s?key=%s", c.ProblemsHost, "v2/exercises", args[0], c.APIKey)
25+
url = fmt.Sprintf("%s/%s/%s?key=%s", c.XAPI, "v2/exercises", args[0], c.APIKey)
2626
case 2:
27-
url = fmt.Sprintf("%s/%s/%s/%s", c.ProblemsHost, "v2/exercises", args[0], args[1])
27+
url = fmt.Sprintf("%s/%s/%s/%s", c.XAPI, "v2/exercises", args[0], args[1])
2828
default:
2929
msg := "Usage: exercism fetch\n or: exercism fetch LANGUAGE\n or: exercism fetch LANGUAGE PROBLEM"
3030
log.Fatal(msg)

0 commit comments

Comments
 (0)