-
-
Notifications
You must be signed in to change notification settings - Fork 369
Expand file tree
/
Copy pathprepare.go
More file actions
115 lines (95 loc) · 2.65 KB
/
prepare.go
File metadata and controls
115 lines (95 loc) · 2.65 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package cmd
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"github.com/exercism/cli/api"
"github.com/exercism/cli/config"
"github.com/spf13/cobra"
)
// prepareCmd does necessary setup for Exercism and its tracks.
var prepareCmd = &cobra.Command{
Use: "prepare",
Aliases: []string{"p"},
Short: "Prepare does setup for Exercism and its tracks.",
Long: `Prepare downloads settings and dependencies for Exercism and the language tracks.
When called without any arguments, this downloads all the copy for the CLI so we
know what to say in all the various situations. It also provides an up-to-date list
of the API endpoints to use.
When called with a track ID, it will do specific setup for that track. This
might include downloading the files that the track maintainers have said are
necessary for the track in general. Any files that are only necessary for a specific
exercise will be downloaded along with the exercise.
To customize the CLI to suit your own preferences, use the configure command.
`,
RunE: func(cmd *cobra.Command, args []string) error {
track, err := cmd.Flags().GetString("track")
if err != nil {
return err
}
if track == "" {
fmt.Println("prepare called")
return nil
}
return prepareTrack(track)
},
}
func prepareTrack(id string) error {
cfg, err := config.NewUserConfig()
if err != nil {
return err
}
client, err := api.NewClient(cfg.Token, cfg.APIBaseURL)
if err != nil {
return err
}
url := fmt.Sprintf("%s/tracks/%s", cfg.APIBaseURL, id)
req, err := client.NewRequest("GET", url, nil)
if err != nil {
return err
}
res, err := client.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
var payload prepareTrackPayload
if err := json.NewDecoder(res.Body).Decode(&payload); err != nil {
return fmt.Errorf("unable to parse API response - %s", err)
}
if res.StatusCode != http.StatusOK {
return errors.New(payload.Error.Message)
}
cliCfg, err := config.NewCLIConfig()
if err != nil {
return err
}
t, ok := cliCfg.Tracks[id]
if !ok {
t = config.NewTrack(id)
}
if payload.Track.TestPattern != "" {
t.IgnorePatterns = append(t.IgnorePatterns, payload.Track.TestPattern)
}
cliCfg.Tracks[id] = t
return cliCfg.Write()
}
type prepareTrackPayload struct {
Track struct {
ID string `json:"id"`
Language string `json:"language"`
TestPattern string `json:"test_pattern"`
} `json:"track"`
Error struct {
Type string `json:"type"`
Message string `json:"message"`
} `json:"error,omitempty"`
}
func initPrepareCmd() {
prepareCmd.Flags().StringP("track", "t", "", "the track you want to prepare")
}
func init() {
RootCmd.AddCommand(prepareCmd)
initPrepareCmd()
}