-
-
Notifications
You must be signed in to change notification settings - Fork 369
Expand file tree
/
Copy pathfetch.go
More file actions
45 lines (38 loc) · 953 Bytes
/
fetch.go
File metadata and controls
45 lines (38 loc) · 953 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package cmd
import (
"fmt"
"log"
"github.com/codegangsta/cli"
"github.com/exercism/cli/api"
"github.com/exercism/cli/config"
"github.com/exercism/cli/user"
)
// Fetch returns exercism problems.
func Fetch(ctx *cli.Context) {
c, err := config.Read(ctx.GlobalString("config"))
if err != nil {
log.Fatal(err)
}
args := ctx.Args()
var url string
switch len(args) {
case 0:
url = fmt.Sprintf("%s/%s?key=%s", c.XAPI, "v2/exercises", c.APIKey)
case 1:
url = fmt.Sprintf("%s/%s/%s?key=%s", c.XAPI, "v2/exercises", args[0], c.APIKey)
case 2:
url = fmt.Sprintf("%s/%s/%s/%s", c.XAPI, "v2/exercises", args[0], args[1])
default:
msg := "Usage: exercism fetch\n or: exercism fetch LANGUAGE\n or: exercism fetch LANGUAGE PROBLEM"
log.Fatal(msg)
}
problems, err := api.Fetch(url)
if err != nil {
log.Fatal(err)
}
hw := user.NewHomework(problems, c)
if err := hw.Save(); err != nil {
log.Fatal(err)
}
hw.Summarize()
}