-
-
Notifications
You must be signed in to change notification settings - Fork 369
Expand file tree
/
Copy pathsubmit.go
More file actions
245 lines (199 loc) · 4.88 KB
/
submit.go
File metadata and controls
245 lines (199 loc) · 4.88 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
package cmd
import (
"bytes"
"errors"
"fmt"
"io"
"mime/multipart"
"os"
"path/filepath"
"github.com/exercism/cli/api"
"github.com/exercism/cli/config"
"github.com/exercism/cli/workspace"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"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.
Call the command with the list of files you want to submit.
`,
RunE: func(cmd *cobra.Command, args []string) error {
cfg := config.NewConfig()
usrCfg := viper.New()
usrCfg.AddConfigPath(cfg.Dir)
usrCfg.SetConfigName("user")
usrCfg.SetConfigType("json")
// Ignore error. If the file doesn't exist, that is fine.
_ = usrCfg.ReadInConfig()
cfg.UserViperConfig = usrCfg
v := viper.New()
v.AddConfigPath(cfg.Dir)
v.SetConfigName("cli")
v.SetConfigType("json")
// Ignore error. If the file doesn't exist, that is fine.
_ = v.ReadInConfig()
return runSubmit(cfg, cmd.Flags(), args)
},
}
func runSubmit(cfg config.Config, flags *pflag.FlagSet, args []string) error {
usrCfg := cfg.UserViperConfig
if usrCfg.GetString("token") == "" {
return fmt.Errorf(msgWelcomePleaseConfigure, config.SettingsURL(usrCfg.GetString("apibaseurl")), BinaryName)
}
if usrCfg.GetString("workspace") == "" {
return fmt.Errorf(msgRerunConfigure, BinaryName)
}
for i, arg := range args {
var err error
arg, err = filepath.Abs(arg)
if err != nil {
return err
}
info, err := os.Lstat(arg)
if err != nil {
if os.IsNotExist(err) {
msg := `
The file you are trying to submit cannot be found.
%s
`
return fmt.Errorf(msg, arg)
}
return err
}
if info.IsDir() {
msg := `
You are submitting a directory, which is not currently supported.
%s
`
return fmt.Errorf(msg, arg)
}
src, err := filepath.EvalSymlinks(arg)
if err != nil {
return err
}
args[i] = src
}
ws, err := workspace.New(usrCfg.GetString("workspace"))
if err != nil {
return err
}
var exerciseDir string
for _, arg := range args {
dir, err := ws.SolutionDir(arg)
if err != nil {
if workspace.IsMissingMetadata(err) {
return errors.New(msgMissingMetadata)
}
return err
}
if exerciseDir != "" && dir != exerciseDir {
msg := `
You are submitting files belonging to different solutions.
Please submit the files for one solution at a time.
`
return errors.New(msg)
}
exerciseDir = dir
}
exercise := workspace.NewExerciseFromDir(exerciseDir)
solution, err := workspace.NewSolution(exerciseDir)
if err != nil {
return err
}
if !solution.IsRequester {
// TODO: add test
msg := `
The solution you are submitting is not connected to your account.
Please re-download the exercise to make sure it has the data it needs.
%s download --exercise=%s --track=%s
`
return fmt.Errorf(msg, BinaryName, solution.Exercise, solution.Track)
}
exercise.Documents = make([]workspace.Document, 0, len(args))
for _, file := range args {
// Don't submit empty files
info, err := os.Stat(file)
if err != nil {
return err
}
if info.Size() == 0 {
msg := `
WARNING: Skipping empty file
%s
`
fmt.Fprintf(Err, msg, file)
continue
}
doc, err := workspace.NewDocument(exercise.Filepath(), file)
if err != nil {
return err
}
exercise.Documents = append(exercise.Documents, doc)
}
if len(exercise.Documents) == 0 {
msg := `
No files found to submit.
`
return errors.New(msg)
}
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
for _, doc := range exercise.Documents {
file, err := os.Open(doc.Filepath())
if err != nil {
return err
}
defer file.Close()
part, err := writer.CreateFormFile("files[]", doc.Path())
if err != nil {
return err
}
_, err = io.Copy(part, file)
if err != nil {
return err
}
}
err = writer.Close()
if err != nil {
return err
}
client, err := api.NewClient(usrCfg.GetString("token"), usrCfg.GetString("apibaseurl"))
if err != nil {
return err
}
url := fmt.Sprintf("%s/solutions/%s", usrCfg.GetString("apibaseurl"), solution.ID)
req, err := client.NewRequest("PATCH", url, body)
if err != nil {
return err
}
req.Header.Set("Content-Type", writer.FormDataContentType())
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
bb := &bytes.Buffer{}
_, err = bb.ReadFrom(resp.Body)
if err != nil {
return err
}
msg := `
Your solution has been submitted successfully.
%s
`
suffix := "View it at:\n\n "
if solution.AutoApprove {
suffix = "You can complete the exercise and unlock the next core exercise at:\n"
}
fmt.Fprintf(Err, msg, suffix)
fmt.Fprintf(Out, " %s\n\n", solution.URL)
return nil
}
func init() {
RootCmd.AddCommand(submitCmd)
}