-
-
Notifications
You must be signed in to change notification settings - Fork 369
Expand file tree
/
Copy pathsubmit.go
More file actions
252 lines (217 loc) · 5.38 KB
/
submit.go
File metadata and controls
252 lines (217 loc) · 5.38 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
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
package cmd
import (
"bytes"
"errors"
"fmt"
"io"
"mime/multipart"
"os"
"path/filepath"
"strings"
"github.com/exercism/cli/api"
"github.com/exercism/cli/comms"
"github.com/exercism/cli/config"
"github.com/exercism/cli/workspace"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// submitCmd lets people upload a solution to the website.
var submitCmd = &cobra.Command{
Use: "submit",
Aliases: []string{"s"},
Short: "Submit your solution to an exercise.",
Long: `Submit your solution to an Exercism exercise.
The CLI will do its best to figure out what to submit.
If you call the command without any arguments, it will
submit the exercise contained in the current directory.
If called with the path to a directory, it will submit it.
If called with the name of an exercise, it will work out which
track it is on and submit it. The command will ask for help
figuring things out if necessary.
`,
RunE: func(cmd *cobra.Command, args []string) error {
usrCfg, err := config.NewUserConfig()
if err != nil {
return err
}
cliCfg, err := config.NewCLIConfig()
if err != nil {
return err
}
if len(args) == 0 {
cwd, err := os.Getwd()
if err != nil {
return err
}
args = []string{cwd}
}
// TODO: make sure we get the workspace configured.
if usrCfg.Workspace == "" {
cwd, err := os.Getwd()
if err != nil {
return err
}
usrCfg.Workspace = filepath.Dir(filepath.Dir(cwd))
}
ws := workspace.New(usrCfg.Workspace)
tx, err := workspace.NewTransmission(ws.Dir, args)
if err != nil {
return err
}
dirs, err := ws.Locate(tx.Dir)
if err != nil {
return err
}
sx, err := workspace.NewSolutions(dirs)
if err != nil {
return err
}
var solution *workspace.Solution
selection := comms.NewSelection()
for _, s := range sx {
selection.Items = append(selection.Items, s)
}
for {
prompt := `
We found more than one. Which one did you mean?
Type the number of the one you want to select.
%s
> `
option, err := selection.Pick(prompt)
if err != nil {
return err
}
s, ok := option.(*workspace.Solution)
if !ok {
fmt.Fprintf(Out, "something went wrong trying to pick that solution, not sure what happened")
continue
}
solution = s
break
}
if !solution.IsRequester {
return errors.New("not your solution")
}
track := cliCfg.Tracks[solution.Track]
if track == nil {
err := prepareTrack(solution.Track)
if err != nil {
return err
}
cliCfg.Load(viper.New())
track = cliCfg.Tracks[solution.Track]
}
paths := tx.Files
if len(paths) == 0 {
walkFn := func(path string, info os.FileInfo, err error) error {
if err != nil || info.IsDir() {
return err
}
ok, err := track.AcceptFilename(path)
if err != nil || !ok {
return err
}
paths = append(paths, path)
return nil
}
filepath.Walk(solution.Dir, walkFn)
}
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
if len(paths) == 0 {
return errors.New("no files found to submit")
}
// If the user submits a directory, confirm the list of files.
if len(tx.ArgDirs) > 0 {
prompt := "You specified a directory, which contains these files:\n"
for i, path := range paths {
prompt += fmt.Sprintf(" [%d] %s\n", i+1, path)
}
prompt += "\nPress ENTER to submit, or control + c to cancel: "
confirmQuestion := &comms.Question{
Prompt: prompt,
DefaultValue: "y",
Reader: In,
Writer: Out,
}
answer, err := confirmQuestion.Ask()
if err != nil {
return err
}
if strings.ToLower(answer) != "y" {
fmt.Fprintf(Out, "Submit cancelled.\nTry submitting individually instead.")
return nil
}
fmt.Fprintf(Out, "Submitting files now...")
}
for _, path := range paths {
// Don't submit empty files
info, err := os.Stat(path)
if err != nil {
return err
}
if info.Size() == 0 {
fmt.Printf("Warning: file %s was empty, skipping...", path)
continue
}
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
dirname := fmt.Sprintf("%s%s%s", string(os.PathSeparator), solution.Exercise, string(os.PathSeparator))
pieces := strings.Split(path, dirname)
filename := fmt.Sprintf("%s%s", string(os.PathSeparator), pieces[len(pieces)-1])
part, err := writer.CreateFormFile("files[]", filename)
if err != nil {
return err
}
_, err = io.Copy(part, file)
if err != nil {
return err
}
}
err = writer.Close()
if err != nil {
return err
}
apiCfg, err := config.NewAPIConfig()
if err != nil {
return err
}
client, err := api.NewClient()
if err != nil {
return err
}
req, err := client.NewRequest("PATCH", apiCfg.URL("submit", solution.ID), body)
if err != nil {
return err
}
req.Header.Set("Content-Type", writer.FormDataContentType())
resp, err := client.Do(req, nil)
if err != nil {
return err
}
defer resp.Body.Close()
bb := &bytes.Buffer{}
_, err = bb.ReadFrom(resp.Body)
if err != nil {
return err
}
if solution.AutoApprove == true {
fmt.Fprintf(Out, "Your solution has been submitted " +
"successfully and has been auto-approved. You can complete " +
"the exercise and unlock the next core exercise at %s\n",
solution.URL)
} else {
//TODO
}
return nil
},
}
func initSubmitCmd() {
// TODO
}
func init() {
RootCmd.AddCommand(submitCmd)
}