Skip to content

Commit 0d25a87

Browse files
committed
Deprecate Hostname field on configure
1 parent 1776574 commit 0d25a87

8 files changed

Lines changed: 48 additions & 40 deletions

File tree

‎config/config.go‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,14 @@ var (
3838
type Config struct {
3939
APIKey string `json:"apiKey"`
4040
Dir string `json:"dir"`
41-
Hostname string `json:"hostname"`
41+
API string `json:"api"`
4242
ProblemsHost string `json:"problemsHost"`
4343
home string // cache user's home directory
4444
file string // full path to config file
4545

46-
// deprecated
46+
// deprecated, get rid of them when nobody uses 1.7.0 anymore
4747
ExercismDirectory string `json:"exercismDirectory,omitempty"`
48+
Hostname string `json:"hostname,omitempty"`
4849
}
4950

5051
// Home returns the user's canonical home directory.
@@ -78,9 +79,9 @@ func Read(file string) (*Config, error) {
7879
// It will attempt to set defaults where no value is passed in.
7980
func New(key, host, dir string) (*Config, error) {
8081
c := &Config{
81-
APIKey: key,
82-
Hostname: host,
83-
Dir: dir,
82+
APIKey: key,
83+
API: host,
84+
Dir: dir,
8485
}
8586
return c.configure()
8687
}
@@ -137,6 +138,7 @@ func (c *Config) File() string {
137138
func (c *Config) Write() error {
138139
renameLegacy()
139140
c.ExercismDirectory = ""
141+
c.Hostname = ""
140142

141143
// truncates existing file if it exists
142144
f, err := os.Create(c.file)
@@ -152,8 +154,12 @@ func (c *Config) Write() error {
152154
func (c *Config) configure() (*Config, error) {
153155
c.sanitize()
154156

155-
if c.Hostname == "" {
156-
c.Hostname = hostAPI
157+
if c.Hostname != "" {
158+
c.API = c.Hostname
159+
}
160+
161+
if c.API == "" {
162+
c.API = hostAPI
157163
}
158164

159165
if c.ProblemsHost == "" {
@@ -210,6 +216,7 @@ func (c *Config) homeDir() (string, error) {
210216
func (c *Config) sanitize() {
211217
c.APIKey = strings.TrimSpace(c.APIKey)
212218
c.Dir = strings.TrimSpace(c.Dir)
219+
c.API = strings.TrimSpace(c.API)
213220
c.Hostname = strings.TrimSpace(c.Hostname)
214221
c.ProblemsHost = strings.TrimSpace(c.ProblemsHost)
215222
}

‎config/config_test.go‎

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ func TestDefaultValues(t *testing.T) {
1515
c.home = "/home/alice"
1616
c.configure()
1717
assert.Equal(t, "", c.APIKey)
18-
assert.Equal(t, "http://exercism.io", c.Hostname)
18+
assert.Equal(t, "http://exercism.io", c.API)
1919
assert.Equal(t, "/home/alice/exercism", c.Dir)
2020
}
2121

2222
func TestCustomValues(t *testing.T) {
2323
c := &Config{
24-
APIKey: "abc123",
25-
Hostname: "http://example.org",
26-
Dir: "/path/to/exercises",
24+
APIKey: "abc123",
25+
API: "http://example.org",
26+
Dir: "/path/to/exercises",
2727
}
2828
c.configure()
2929
assert.Equal(t, "abc123", c.APIKey)
30-
assert.Equal(t, "http://example.org", c.Hostname)
30+
assert.Equal(t, "http://example.org", c.API)
3131
assert.Equal(t, "/path/to/exercises", c.Dir)
3232
}
3333

@@ -40,13 +40,13 @@ func TestExpandHomeDir(t *testing.T) {
4040

4141
func TestSanitizeWhitespace(t *testing.T) {
4242
c := &Config{
43-
APIKey: " abc123\n\r\n ",
44-
Hostname: " ",
45-
Dir: " \r\n/path/to/exercises \r\n",
43+
APIKey: " abc123\n\r\n ",
44+
API: " ",
45+
Dir: " \r\n/path/to/exercises \r\n",
4646
}
4747
c.configure()
4848
assert.Equal(t, "abc123", c.APIKey)
49-
assert.Equal(t, "http://exercism.io", c.Hostname)
49+
assert.Equal(t, "http://exercism.io", c.API)
5050
assert.Equal(t, "/path/to/exercises", c.Dir)
5151
}
5252

@@ -68,7 +68,7 @@ func TestReadNonexistantConfig(t *testing.T) {
6868
c, err := Read("/no/such/config.json")
6969
assert.NoError(t, err)
7070
assert.Equal(t, c.APIKey, "")
71-
assert.Equal(t, c.Hostname, "http://exercism.io")
71+
assert.Equal(t, c.API, "http://exercism.io")
7272
assert.False(t, c.IsAuthenticated())
7373
if !strings.HasSuffix(c.Dir, "/exercism") {
7474
t.Fatal("Default unconfigured config should use home dir")
@@ -81,10 +81,11 @@ func TestReadingWritingConfig(t *testing.T) {
8181
assert.NoError(t, err)
8282

8383
c1 := &Config{
84-
APIKey: "MyKey",
85-
Dir: "/exercism/directory",
86-
Hostname: "localhost",
84+
APIKey: "MyKey",
85+
Dir: "/exercism/directory",
86+
API: "localhost",
8787
}
88+
c1.configure()
8889

8990
c1.SavePath(filename)
9091
c1.Write()
@@ -94,7 +95,7 @@ func TestReadingWritingConfig(t *testing.T) {
9495

9596
assert.Equal(t, c1.APIKey, c2.APIKey)
9697
assert.Equal(t, c1.Dir, c2.Dir)
97-
assert.Equal(t, c1.Hostname, c2.Hostname)
98+
assert.Equal(t, c1.API, c2.API)
9899
}
99100

100101
func TestReadDefaultConfig(t *testing.T) {
@@ -106,7 +107,7 @@ func TestReadDefaultConfig(t *testing.T) {
106107
assert.NoError(t, err)
107108
assert.Equal(t, "abc123", c.APIKey)
108109
assert.Equal(t, "/path/to/exercism", c.Dir)
109-
assert.Equal(t, "http://example.com", c.Hostname)
110+
assert.Equal(t, "http://example.com", c.API)
110111
assert.Equal(t, "http://x.example.com", c.ProblemsHost)
111112
}
112113

@@ -120,7 +121,7 @@ func TestReadCustomConfig(t *testing.T) {
120121
assert.NoError(t, err)
121122
assert.Equal(t, "xyz000", c.APIKey)
122123
assert.Equal(t, "/tmp/exercism", c.Dir)
123-
assert.Equal(t, "http://example.org", c.Hostname)
124+
assert.Equal(t, "http://example.org", c.API)
124125
assert.Equal(t, "http://x.example.org", c.ProblemsHost)
125126
}
126127

@@ -134,6 +135,6 @@ func TestReadLegacyConfig(t *testing.T) {
134135
assert.NoError(t, err)
135136
assert.Equal(t, "prq567", c.APIKey)
136137
assert.Equal(t, "/tmp/stuff", c.Dir)
137-
assert.Equal(t, "http://api.example.com", c.Hostname)
138+
assert.Equal(t, "http://api.example.com", c.API)
138139
assert.Equal(t, "http://problems.example.com", c.ProblemsHost)
139140
}

‎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","hostname":"http://example.com","problemsHost":"http://x.example.com"}
1+
{"apiKey":"abc123","dir":"/path/to/exercism","api":"http://example.com","problemsHost":"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","hostname":"http://example.org","problemsHost":"http://x.example.org"}
1+
{"apiKey":"xyz000","dir":"/tmp/exercism","api":"http://example.org","problemsHost":"http://x.example.org"}

‎handlers/debug.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,6 @@ func Debug(ctx *cli.Context) {
4646
fmt.Println("Config file: <not configured>")
4747
fmt.Println("API Key: <not configured>")
4848
}
49-
fmt.Printf("API: %s\n", c.Hostname)
49+
fmt.Printf("API: %s\n", c.API)
5050
fmt.Printf("Exercises Directory: %s\n", c.Dir)
5151
}

‎handlers/restore.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func Restore(ctx *cli.Context) {
1616
log.Fatal(err)
1717
}
1818

19-
url := fmt.Sprintf("%s/api/v1/iterations/%s/restore", c.Hostname, c.APIKey)
19+
url := fmt.Sprintf("%s/api/v1/iterations/%s/restore", c.API, c.APIKey)
2020

2121
problems, err := api.Fetch(url)
2222
if err != nil {

‎main.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ func main() {
183183
}
184184

185185
fmt.Printf("For feedback on your submission visit %s%s%s\n",
186-
c.Hostname, "/submissions/", response.ID)
186+
c.API, "/submissions/", response.ID)
187187

188188
},
189189
},
@@ -243,7 +243,7 @@ type submitRequest struct {
243243
}
244244

245245
func FetchAssignments(c *config.Config, path string) ([]Assignment, error) {
246-
url := fmt.Sprintf("%s%s?key=%s", c.Hostname, path, c.APIKey)
246+
url := fmt.Sprintf("%s%s?key=%s", c.API, path, c.APIKey)
247247
req, err := http.NewRequest("GET", url, nil)
248248
if err != nil {
249249
return nil, err
@@ -293,7 +293,7 @@ func FetchAssignments(c *config.Config, path string) ([]Assignment, error) {
293293
func UnsubmitAssignment(c *config.Config) error {
294294
path := "api/v1/user/assignments"
295295

296-
url := fmt.Sprintf("%s/%s?key=%s", c.Hostname, path, c.APIKey)
296+
url := fmt.Sprintf("%s/%s?key=%s", c.API, path, c.APIKey)
297297

298298
req, err := http.NewRequest("DELETE", url, nil)
299299
if err != nil {
@@ -334,7 +334,7 @@ func UnsubmitAssignment(c *config.Config) error {
334334
func SubmitAssignment(c *config.Config, filePath string, code []byte) (*submitResponse, error) {
335335
path := "api/v1/user/assignments"
336336

337-
url := fmt.Sprintf("%s/%s", c.Hostname, path)
337+
url := fmt.Sprintf("%s/%s", c.API, path)
338338

339339
submission := submitRequest{Key: c.APIKey, Code: string(code), Path: filePath}
340340
submissionJSON, err := json.Marshal(submission)

‎main_test.go‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ func TestFetchWithKey(t *testing.T) {
5252
server := httptest.NewServer(http.HandlerFunc(fetchHandler))
5353

5454
c := &config.Config{
55-
Hostname: server.URL,
56-
APIKey: "myAPIKey",
55+
API: server.URL,
56+
APIKey: "myAPIKey",
5757
}
5858

5959
assignments, err := FetchAssignments(c, "/api/v1/user/assignments/current")
@@ -76,8 +76,8 @@ func TestFetchWithIncorrectKey(t *testing.T) {
7676
server := httptest.NewServer(http.HandlerFunc(fetchHandler))
7777

7878
c := &config.Config{
79-
Hostname: server.URL,
80-
APIKey: "myWrongAPIKey",
79+
API: server.URL,
80+
APIKey: "myWrongAPIKey",
8181
}
8282

8383
assignments, err := FetchAssignments(c, "/api/v1/user/assignments/current")
@@ -174,8 +174,8 @@ func TestSubmitWithKey(t *testing.T) {
174174

175175
var code = []byte("My source code\n")
176176
c := &config.Config{
177-
Hostname: server.URL,
178-
APIKey: "myAPIKey",
177+
API: server.URL,
178+
APIKey: "myAPIKey",
179179
}
180180
response, err := SubmitAssignment(c, "ruby/bob/bob.rb", code)
181181
assert.NoError(t, err)
@@ -191,8 +191,8 @@ func TestSubmitWithIncorrectKey(t *testing.T) {
191191
defer server.Close()
192192

193193
c := &config.Config{
194-
Hostname: server.URL,
195-
APIKey: "myWrongAPIKey",
194+
API: server.URL,
195+
APIKey: "myWrongAPIKey",
196196
}
197197

198198
var code = []byte("My source code\n")

0 commit comments

Comments
 (0)