forked from exercism/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
297 lines (269 loc) · 7.78 KB
/
main.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/codegangsta/cli"
"github.com/exercism/cli/configuration"
)
func main() {
app := cli.NewApp()
app.Name = "exercism"
app.Usage = "A command line tool to interact with http://exercism.io"
app.Version = VERSION
app.Flags = []cli.Flag{
cli.StringFlag{"config, c", configuration.Filename(configuration.HomeDir()), "path to config file"},
}
app.Commands = []cli.Command{
{
Name: "current",
ShortName: "c",
Usage: "Show the current assignments",
Action: func(c *cli.Context) {
var language string
argc := len(c.Args())
if argc != 0 && argc != 1 {
fmt.Println("Usage: exercism current\n or: exercism current LANGUAGE")
return
}
config, err := configuration.FromFile(c.GlobalString("config"))
if err != nil {
fmt.Println("Are you sure you are logged in? Please login again.")
return
}
currentAssignments, err := FetchAssignments(config, FetchEndpoints["current"])
if err != nil {
fmt.Println(err)
return
}
if argc == 1 {
language = c.Args()[0]
fmt.Println("Current Assignments for", strings.Title(language))
} else {
fmt.Println("Current Assignments")
}
for _, a := range currentAssignments {
if argc == 1 {
if strings.ToLower(language) == strings.ToLower(a.Track) {
fmt.Printf("%v: %v\n", strings.Title(a.Track), a.Slug)
}
} else {
fmt.Printf("%v: %v\n", strings.Title(a.Track), a.Slug)
}
}
},
},
{
Name: "demo",
ShortName: "d",
Usage: "Fetch first assignment for each language from exercism.io",
Action: func(c *cli.Context) {
config, err := configuration.FromFile(c.GlobalString("config"))
if err != nil {
config, err = configuration.Demo()
if err != nil {
fmt.Println(err)
return
}
}
assignments, err := FetchAssignments(config, FetchEndpoints["demo"])
if err != nil {
fmt.Println(err)
return
}
for _, a := range assignments {
err := SaveAssignment(config.ExercismDirectory, a)
if err != nil {
fmt.Println(err)
}
}
msg := "\nThe demo exercises have been written to %s, in subdirectories by language.\n\nTo try an exercise, change directory to a language/exercise, read the README and run the tests.\n\n"
fmt.Printf(msg, config.ExercismDirectory)
},
},
{
Name: "fetch",
ShortName: "f",
Usage: "Fetch assignments from exercism.io",
Action: func(c *cli.Context) {
argCount := len(c.Args())
if argCount < 0 || argCount > 2 {
fmt.Println("Usage: exercism fetch\n or: exercism fetch LANGUAGE\n or: exercism fetch LANGUAGE EXERCISE")
return
}
config, err := configuration.FromFile(c.GlobalString("config"))
if err != nil {
if argCount == 0 || argCount == 1 {
fmt.Println("Are you sure you are logged in? Please login again.")
return
} else {
config, err = configuration.Demo()
if err != nil {
fmt.Println(err)
return
}
}
}
assignments, err := FetchAssignments(config, FetchEndpoint(c.Args()))
if err != nil {
fmt.Println(err)
return
}
if len(assignments) == 0 {
noAssignmentMessage := "No assignments found"
if argCount == 2 {
fmt.Printf("%s for %s - %s\n", noAssignmentMessage, c.Args()[0], c.Args()[1])
} else if argCount == 1 {
fmt.Printf("%s for %s\n", noAssignmentMessage, c.Args()[0])
} else {
fmt.Printf("%s\n", noAssignmentMessage)
}
return
}
for _, a := range assignments {
err := SaveAssignment(config.ExercismDirectory, a)
if err != nil {
fmt.Println(err)
}
}
fmt.Printf("Exercises written to %s\n", config.ExercismDirectory)
},
},
{
Name: "login",
ShortName: "l",
Usage: "Save exercism.io api credentials",
Action: func(c *cli.Context) {
config, err := askForConfigInfo()
if err != nil {
fmt.Println(err)
return
}
configuration.ToFile(c.GlobalString("config"), config)
fmt.Printf("Your exercism directory can be found at %s\n", config.ExercismDirectory)
},
},
{
Name: "logout",
ShortName: "o",
Usage: "Clear exercism.io api credentials",
Action: func(c *cli.Context) {
logout(c.GlobalString("config"))
},
},
{
Name: "restore",
ShortName: "r",
Usage: "Restore completed and current assignments from exercism.io",
Description: "Restore will pull the latest revisions of exercises that have already been " +
"submitted. It will *not* overwrite existing files. If you have made changes " +
"to a file and have not submitted it, and you're trying to restore the last " +
"submitted version, first move that file out of the way, then call restore.",
Action: func(c *cli.Context) {
config, err := configuration.FromFile(c.GlobalString("config"))
if err != nil {
fmt.Println("Are you sure you are logged in? Please login again.")
return
}
assignments, err := FetchAssignments(config, FetchEndpoints["restore"])
if err != nil {
fmt.Println(err)
return
}
for _, a := range assignments {
err := SaveAssignment(config.ExercismDirectory, a)
if err != nil {
fmt.Println(err)
}
}
fmt.Printf("Exercises written to %s\n", config.ExercismDirectory)
},
},
{
Name: "submit",
ShortName: "s",
Usage: "Submit code to exercism.io on your current assignment",
Action: func(c *cli.Context) {
config, err := configuration.FromFile(c.GlobalString("config"))
if err != nil {
fmt.Println("Are you sure you are logged in? Please login again.")
return
}
if len(c.Args()) == 0 {
fmt.Println("Please enter a file name")
return
}
filename := c.Args()[0]
// Make filename relative to config.ExercismDirectory.
absPath, err := absolutePath(filename)
if err != nil {
fmt.Printf("Couldn't find %v: %v\n", filename, err)
return
}
exDir := config.ExercismDirectory + string(filepath.Separator)
if !strings.HasPrefix(absPath, exDir) {
fmt.Printf("%v is not under your exercism project path (%v)\n", absPath, exDir)
return
}
filename = absPath[len(exDir):]
if IsTest(filename) {
fmt.Println("It looks like this is a test, please enter an example file name.")
return
}
code, err := ioutil.ReadFile(absPath)
if err != nil {
fmt.Printf("Error reading %v: %v\n", absPath, err)
return
}
response, err := SubmitAssignment(config, filename, code)
if err != nil {
fmt.Printf("There was an issue with your submission: %v\n", err)
return
}
fmt.Printf("For feedback on your submission visit %s%s%s\n",
config.Hostname, "/submissions/", response.Id)
},
},
{
Name: "unsubmit",
ShortName: "u",
Usage: "Delete the last submission",
Action: func(c *cli.Context) {
config, err := configuration.FromFile(c.GlobalString("config"))
if err != nil {
fmt.Println("Are you sure you are logged in? Please login again.")
return
}
response, err := UnsubmitAssignment(config)
if err != nil {
fmt.Println(err)
return
}
if response != "" {
return
}
fmt.Println("The last submission was successfully deleted.")
},
},
{
Name: "whoami",
ShortName: "w",
Usage: "Get the github username that you are logged in as",
Action: func(c *cli.Context) {
config, err := configuration.FromFile(c.GlobalString("config"))
if err != nil {
fmt.Println("Are you sure you are logged in? Please login again.")
return
}
fmt.Println(config.GithubUsername)
},
},
}
err := app.Run(os.Args)
if err != nil {
fmt.Errorf("%v", err)
os.Exit(1)
}
}