|
1 | 1 | package cmd |
2 | 2 |
|
3 | | -import ( |
4 | | - "encoding/json" |
5 | | - "errors" |
6 | | - "fmt" |
7 | | - "net/http" |
8 | | - |
9 | | - "github.com/exercism/cli/api" |
10 | | - "github.com/exercism/cli/config" |
11 | | - "github.com/spf13/cobra" |
12 | | - "github.com/spf13/pflag" |
13 | | - "github.com/spf13/viper" |
14 | | -) |
| 3 | +import "github.com/spf13/cobra" |
15 | 4 |
|
16 | 5 | // prepareCmd does necessary setup for Exercism and its tracks. |
17 | 6 | var prepareCmd = &cobra.Command{ |
18 | 7 | Use: "prepare", |
19 | 8 | Aliases: []string{"p"}, |
20 | 9 | Short: "Prepare does setup for Exercism and its tracks.", |
21 | 10 | Long: `Prepare downloads settings and dependencies for Exercism and the language tracks. |
22 | | -
|
23 | | -When called with a track ID, it will do specific setup for that track. This |
24 | | -might include downloading the files that the track maintainers have said are |
25 | | -necessary for the track in general. Any files that are only necessary for a specific |
26 | | -exercise will be downloaded along with the exercise. |
27 | | -
|
28 | | -To customize the CLI to suit your own preferences, use the configure command. |
29 | 11 | `, |
30 | 12 | RunE: func(cmd *cobra.Command, args []string) error { |
31 | | - cfg := config.NewConfiguration() |
32 | | - |
33 | | - v := viper.New() |
34 | | - v.AddConfigPath(cfg.Dir) |
35 | | - v.SetConfigName("user") |
36 | | - v.SetConfigType("json") |
37 | | - // Ignore error. If the file doesn't exist, that is fine. |
38 | | - _ = v.ReadInConfig() |
39 | | - cfg.UserViperConfig = v |
40 | | - |
41 | | - return runPrepare(cfg, cmd.Flags(), args) |
42 | | - }, |
43 | | -} |
44 | | - |
45 | | -func runPrepare(cfg config.Configuration, flags *pflag.FlagSet, args []string) error { |
46 | | - v := cfg.UserViperConfig |
47 | | - |
48 | | - track, err := flags.GetString("track") |
49 | | - if err != nil { |
50 | | - return err |
51 | | - } |
52 | | - |
53 | | - if track == "" { |
54 | 13 | return nil |
55 | | - } |
56 | | - client, err := api.NewClient(v.GetString("token"), v.GetString("apibaseurl")) |
57 | | - if err != nil { |
58 | | - return err |
59 | | - } |
60 | | - url := fmt.Sprintf("%s/tracks/%s", v.GetString("apibaseurl"), track) |
61 | | - |
62 | | - req, err := client.NewRequest("GET", url, nil) |
63 | | - if err != nil { |
64 | | - return err |
65 | | - } |
66 | | - |
67 | | - res, err := client.Do(req) |
68 | | - if err != nil { |
69 | | - return err |
70 | | - } |
71 | | - defer res.Body.Close() |
72 | | - |
73 | | - var payload prepareTrackPayload |
74 | | - |
75 | | - if err := json.NewDecoder(res.Body).Decode(&payload); err != nil { |
76 | | - return fmt.Errorf("unable to parse API response - %s", err) |
77 | | - } |
78 | | - |
79 | | - if res.StatusCode != http.StatusOK { |
80 | | - return errors.New(payload.Error.Message) |
81 | | - } |
82 | | - |
83 | | - cliCfg, err := config.NewCLIConfig() |
84 | | - if err != nil { |
85 | | - return err |
86 | | - } |
87 | | - |
88 | | - t, ok := cliCfg.Tracks[track] |
89 | | - if !ok { |
90 | | - t = config.NewTrack(track) |
91 | | - } |
92 | | - if payload.Track.TestPattern != "" { |
93 | | - t.IgnorePatterns = append(t.IgnorePatterns, payload.Track.TestPattern) |
94 | | - } |
95 | | - cliCfg.Tracks[track] = t |
96 | | - |
97 | | - return cliCfg.Write() |
98 | | -} |
99 | | - |
100 | | -type prepareTrackPayload struct { |
101 | | - Track struct { |
102 | | - ID string `json:"id"` |
103 | | - Language string `json:"language"` |
104 | | - TestPattern string `json:"test_pattern"` |
105 | | - } `json:"track"` |
106 | | - Error struct { |
107 | | - Type string `json:"type"` |
108 | | - Message string `json:"message"` |
109 | | - } `json:"error,omitempty"` |
110 | | -} |
111 | | - |
112 | | -func initPrepareCmd() { |
113 | | - setupPrepareFlags(prepareCmd.Flags()) |
114 | | -} |
115 | | - |
116 | | -func setupPrepareFlags(flags *pflag.FlagSet) { |
117 | | - flags.StringP("track", "t", "", "the track you want to prepare") |
| 14 | + }, |
118 | 15 | } |
119 | 16 |
|
120 | 17 | func init() { |
121 | 18 | RootCmd.AddCommand(prepareCmd) |
122 | | - initPrepareCmd() |
123 | 19 | } |
0 commit comments