/
fetch.go
92 lines (77 loc) · 1.79 KB
/
fetch.go
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package cmd
import (
"fmt"
"log"
"path/filepath"
"github.com/exercism/cli/api"
"github.com/exercism/cli/config"
"github.com/exercism/cli/user"
"github.com/urfave/cli"
)
// Fetch downloads exercism problems and writes them to disk.
func Fetch(ctx *cli.Context) error {
c, err := config.New(ctx.GlobalString("config"))
if err != nil {
log.Fatal(err)
}
client := api.NewClient(c)
args := ctx.Args()
var problems []*api.Problem
if ctx.Bool("all") {
if len(args) > 0 {
trackID := args[0]
fmt.Printf("\nFetching all problems for the %s track...\n\n", trackID)
p, err := client.FetchAll(trackID)
if err != nil {
log.Fatal(err)
}
problems = p
} else {
log.Fatalf("You must supply a track to fetch all exercises")
}
} else {
p, err := client.Fetch(args)
if err != nil {
log.Fatal(err)
}
problems = p
}
submissionInfo, err := client.Submissions()
if err != nil {
log.Fatal(err)
}
if err := setSubmissionState(problems, submissionInfo); err != nil {
log.Fatal(err)
}
dirs, err := filepath.Glob(filepath.Join(c.Dir, "*"))
if err != nil {
log.Fatal(err)
}
dirMap := make(map[string]bool)
for _, dir := range dirs {
dirMap[dir] = true
}
hw := user.NewHomework(problems, c)
if len(ctx.Args()) == 0 {
if err := hw.RejectMissingTracks(dirMap); err != nil {
log.Fatal(err)
}
}
if err := hw.Save(); err != nil {
log.Fatal(err)
}
hw.Summarize(user.HWAll)
return nil
// return cli.NewExitError("no good", 10)
}
func setSubmissionState(problems []*api.Problem, submissionInfo map[string][]api.SubmissionInfo) error {
for _, problem := range problems {
langSubmissions := submissionInfo[problem.TrackID]
for _, submission := range langSubmissions {
if submission.Slug == problem.Slug {
problem.Submitted = true
}
}
}
return nil
}