Skip to content

Commit 56eda46

Browse files
author
Katrina Owen
authored
Merge pull request #652 from exercism/teams-download
Add --team flag to download command
2 parents fc64c80 + ad5e545 commit 56eda46

3 files changed

Lines changed: 35 additions & 6 deletions

File tree

cmd/download.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,20 @@ func runDownload(cfg config.Configuration, flags *pflag.FlagSet, args []string)
9090
return err
9191
}
9292

93+
team, err := flags.GetString("team")
94+
if err != nil {
95+
return err
96+
}
97+
9398
if uuid == "" {
9499
q := req.URL.Query()
95100
q.Add("exercise_id", exercise)
96101
if track != "" {
97102
q.Add("track_id", track)
98103
}
104+
if team != "" {
105+
q.Add("team_id", team)
106+
}
99107
req.URL.RawQuery = q.Encode()
100108
}
101109

@@ -127,6 +135,7 @@ func runDownload(cfg config.Configuration, flags *pflag.FlagSet, args []string)
127135
solution := workspace.Solution{
128136
AutoApprove: payload.Solution.Exercise.AutoApprove,
129137
Track: payload.Solution.Exercise.Track.ID,
138+
Team: payload.Solution.Team.Slug,
130139
Exercise: payload.Solution.Exercise.ID,
131140
ID: payload.Solution.ID,
132141
URL: payload.Solution.URL,
@@ -135,6 +144,9 @@ func runDownload(cfg config.Configuration, flags *pflag.FlagSet, args []string)
135144
}
136145

137146
dir := usrCfg.GetString("workspace")
147+
if solution.Team != "" {
148+
dir = filepath.Join(dir, "teams", solution.Team)
149+
}
138150
if !solution.IsRequester {
139151
dir = filepath.Join(dir, "users", solution.Handle)
140152
}
@@ -205,6 +217,10 @@ type downloadPayload struct {
205217
Solution struct {
206218
ID string `json:"id"`
207219
URL string `json:"url"`
220+
Team struct {
221+
Name string `json:"name"`
222+
Slug string `json:"slug"`
223+
} `json:"team"`
208224
User struct {
209225
Handle string `json:"handle"`
210226
IsRequester bool `json:"is_requester"`
@@ -235,6 +251,7 @@ func setupDownloadFlags(flags *pflag.FlagSet) {
235251
flags.StringP("uuid", "u", "", "the solution UUID")
236252
flags.StringP("track", "t", "", "the track ID")
237253
flags.StringP("exercise", "e", "", "the exercise slug")
254+
flags.StringP("team", "T", "", "the team slug")
238255
}
239256

240257
func init() {

cmd/download_test.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,19 @@ func TestDownload(t *testing.T) {
107107
expectedDir: filepath.Join("users", "alice"),
108108
flags: map[string]string{"uuid": "bogus-id"},
109109
},
110+
{
111+
requester: true,
112+
expectedDir: filepath.Join("teams", "bogus-team"),
113+
flags: map[string]string{"exercise": "bogus-exercise", "track": "bogus-track", "team": "bogus-team"},
114+
},
110115
}
111116

112117
for _, tc := range testCases {
113118
tmpDir, err := ioutil.TempDir("", "download-cmd")
114119
defer os.RemoveAll(tmpDir)
115120
assert.NoError(t, err)
116121

117-
ts := fakeDownloadServer(strconv.FormatBool(tc.requester))
122+
ts := fakeDownloadServer(strconv.FormatBool(tc.requester), tc.flags["team"])
118123
defer ts.Close()
119124

120125
v := viper.New()
@@ -149,7 +154,7 @@ func TestDownload(t *testing.T) {
149154
}
150155
}
151156

152-
func fakeDownloadServer(requestor string) *httptest.Server {
157+
func fakeDownloadServer(requestor, teamSlug string) *httptest.Server {
153158
mux := http.NewServeMux()
154159
server := httptest.NewServer(mux)
155160

@@ -165,11 +170,16 @@ func fakeDownloadServer(requestor string) *httptest.Server {
165170
fmt.Fprint(w, "")
166171
})
167172

168-
payloadBody := fmt.Sprintf(payloadTemplate, requestor, server.URL+"/")
169173
mux.HandleFunc("/solutions/latest", func(w http.ResponseWriter, r *http.Request) {
174+
team := "null"
175+
if teamSlug := r.FormValue("team_id"); teamSlug != "" {
176+
team = fmt.Sprintf(`{"name": "Bogus Team", "slug": "%s"}`, teamSlug)
177+
}
178+
payloadBody := fmt.Sprintf(payloadTemplate, requestor, team, server.URL+"/")
170179
fmt.Fprint(w, payloadBody)
171180
})
172181
mux.HandleFunc("/solutions/bogus-id", func(w http.ResponseWriter, r *http.Request) {
182+
payloadBody := fmt.Sprintf(payloadTemplate, requestor, "null", server.URL+"/")
173183
fmt.Fprint(w, payloadBody)
174184
})
175185

@@ -215,6 +225,7 @@ const payloadTemplate = `
215225
"handle": "alice",
216226
"is_requester": %s
217227
},
228+
"team": %s,
218229
"exercise": {
219230
"id": "bogus-exercise",
220231
"instructions_url": "http://example.com/bogus-exercise",
@@ -226,9 +237,9 @@ const payloadTemplate = `
226237
},
227238
"file_download_base_url": "%s",
228239
"files": [
229-
"/file-1.txt",
230-
"/subdir/file-2.txt",
231-
"/file-3.txt"
240+
"/file-1.txt",
241+
"/subdir/file-2.txt",
242+
"/file-3.txt"
232243
],
233244
"iteration": {
234245
"submitted_at": "2017-08-21t10:11:12.130z"

workspace/solution.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type Solution struct {
1919
Track string `json:"track"`
2020
Exercise string `json:"exercise"`
2121
ID string `json:"id"`
22+
Team string `json:"team,omitempty"`
2223
URL string `json:"url"`
2324
Handle string `json:"handle"`
2425
IsRequester bool `json:"is_requester"`

0 commit comments

Comments
 (0)