@@ -51,16 +51,14 @@ var submitCmd = &cobra.Command{
51
51
}
52
52
53
53
type submission struct {
54
- exercise workspace.Exercise
55
- metadata * workspace.ExerciseMetadata
56
54
documents []workspace.Document
55
+ metadata * workspace.ExerciseMetadata
57
56
}
58
57
59
58
// submitContext is a context for submitting solutions to the API.
60
59
type submitContext struct {
61
60
usrCfg * viper.Viper
62
61
flags * pflag.FlagSet
63
- args []string
64
62
submission
65
63
}
66
64
@@ -74,7 +72,7 @@ func runSubmit(cfg config.Config, flags *pflag.FlagSet, args []string) error {
74
72
return err
75
73
}
76
74
77
- if err := ctx . submitDocuments (); err != nil {
75
+ if err := submitDocuments (cfg . UserViperConfig , ctx . metadata , ctx . documents ); err != nil {
78
76
return err
79
77
}
80
78
@@ -84,44 +82,43 @@ func runSubmit(cfg config.Config, flags *pflag.FlagSet, args []string) error {
84
82
85
83
// newSubmitContext creates a submitContext.
86
84
func newSubmitContext (usrCfg * viper.Viper , flags * pflag.FlagSet , args []string ) (* submitContext , error ) {
87
- ctx := & submitContext {usrCfg : usrCfg , flags : flags , args : args }
85
+ ctx := & submitContext {usrCfg : usrCfg , flags : flags }
88
86
89
- if err := ctx .sanitizeArgs (); err != nil {
87
+ filepaths , err := ctx .sanitizeArgs (args )
88
+ if err != nil {
90
89
return nil , err
91
90
}
92
91
93
- exercise , err := ctx ._exercise ( )
92
+ exercise , err := ctx .exercise ( filepaths )
94
93
if err != nil {
95
94
return nil , err
96
95
}
97
- ctx .exercise = exercise
98
96
99
- if err = ctx .migrateLegacyMetadata (); err != nil {
97
+ if err = ctx .migrateLegacyMetadata (exercise ); err != nil {
100
98
return nil , err
101
99
}
102
100
103
- metadata , err := ctx ._metadata ( )
101
+ documents , err := ctx ._documents ( filepaths , exercise )
104
102
if err != nil {
105
103
return nil , err
106
104
}
107
- ctx .metadata = metadata
108
105
109
- documents , err := ctx ._documents ( )
106
+ metadata , err := ctx ._metadata ( exercise )
110
107
if err != nil {
111
108
return nil , err
112
109
}
113
- ctx .documents = documents
114
110
111
+ ctx .submission = submission {documents : documents , metadata : metadata }
115
112
return ctx , nil
116
113
}
117
114
118
115
// sanitizeArgs validates args and swaps with evaluated symlink paths.
119
- func (s * submitContext ) sanitizeArgs () error {
120
- for i , arg := range s . args {
116
+ func (s * submitContext ) sanitizeArgs (args [] string ) ([] string , error ) {
117
+ for i , arg := range args {
121
118
var err error
122
119
arg , err = filepath .Abs (arg )
123
120
if err != nil {
124
- return err
121
+ return nil , err
125
122
}
126
123
127
124
info , err := os .Lstat (arg )
@@ -134,9 +131,9 @@ func (s *submitContext) sanitizeArgs() error {
134
131
%s
135
132
136
133
`
137
- return fmt .Errorf (msg , arg )
134
+ return nil , fmt .Errorf (msg , arg )
138
135
}
139
- return err
136
+ return nil , err
140
137
}
141
138
if info .IsDir () {
142
139
msg := `
@@ -150,27 +147,27 @@ func (s *submitContext) sanitizeArgs() error {
150
147
%s submit FILENAME
151
148
152
149
`
153
- return fmt .Errorf (msg , arg , BinaryName )
150
+ return nil , fmt .Errorf (msg , arg , BinaryName )
154
151
}
155
152
156
153
src , err := filepath .EvalSymlinks (arg )
157
154
if err != nil {
158
- return err
155
+ return nil , err
159
156
}
160
- s . args [i ] = src
157
+ args [i ] = src
161
158
}
162
- return nil
159
+ return args , nil
163
160
}
164
161
165
- func (s * submitContext ) _exercise ( ) (workspace.Exercise , error ) {
162
+ func (s * submitContext ) exercise ( filepaths [] string ) (workspace.Exercise , error ) {
166
163
ws , err := workspace .New (s .usrCfg .GetString ("workspace" ))
167
164
if err != nil {
168
165
return workspace.Exercise {}, err
169
166
}
170
167
171
168
var exerciseDir string
172
- for _ , arg := range s . args {
173
- dir , err := ws .ExerciseDir (arg )
169
+ for _ , f := range filepaths {
170
+ dir , err := ws .ExerciseDir (f )
174
171
if err != nil {
175
172
if workspace .IsMissingMetadata (err ) {
176
173
return workspace.Exercise {}, errors .New (msgMissingMetadata )
@@ -192,8 +189,8 @@ func (s *submitContext) _exercise() (workspace.Exercise, error) {
192
189
return workspace .NewExerciseFromDir (exerciseDir ), nil
193
190
}
194
191
195
- func (s * submitContext ) migrateLegacyMetadata () error {
196
- migrationStatus , err := s . exercise .MigrateLegacyMetadataFile ()
192
+ func (s * submitContext ) migrateLegacyMetadata (exercise workspace. Exercise ) error {
193
+ migrationStatus , err := exercise .MigrateLegacyMetadataFile ()
197
194
if err != nil {
198
195
return err
199
196
}
@@ -203,13 +200,13 @@ func (s *submitContext) migrateLegacyMetadata() error {
203
200
return nil
204
201
}
205
202
206
- func (s * submitContext ) _metadata () (* workspace.ExerciseMetadata , error ) {
207
- metadata , err := workspace .NewExerciseMetadata (s . exercise .Filepath ())
203
+ func (s * submitContext ) _metadata (exercise workspace. Exercise ) (* workspace.ExerciseMetadata , error ) {
204
+ metadata , err := workspace .NewExerciseMetadata (exercise .Filepath ())
208
205
if err != nil {
209
206
return nil , err
210
207
}
211
208
212
- if metadata .Exercise != s . exercise .Slug {
209
+ if metadata .Exercise != exercise .Slug {
213
210
// TODO: error msg should suggest running future doctor command
214
211
msg := `
215
212
@@ -220,7 +217,7 @@ func (s *submitContext) _metadata() (*workspace.ExerciseMetadata, error) {
220
217
Please rename the directory '%[1]s' to '%[2]s' and try again.
221
218
222
219
`
223
- return nil , fmt .Errorf (msg , s . exercise .Slug , metadata .Exercise )
220
+ return nil , fmt .Errorf (msg , exercise .Slug , metadata .Exercise )
224
221
}
225
222
226
223
if ! metadata .IsRequester {
@@ -238,9 +235,9 @@ func (s *submitContext) _metadata() (*workspace.ExerciseMetadata, error) {
238
235
return metadata , nil
239
236
}
240
237
241
- func (s * submitContext ) _documents () ([]workspace.Document , error ) {
242
- docs := make ([]workspace.Document , 0 , len (s . args ))
243
- for _ , file := range s . args {
238
+ func (s * submitContext ) _documents (filepaths [] string , exercise workspace. Exercise ) ([]workspace.Document , error ) {
239
+ docs := make ([]workspace.Document , 0 , len (filepaths ))
240
+ for _ , file := range filepaths {
244
241
// Don't submit empty files
245
242
info , err := os .Stat (file )
246
243
if err != nil {
@@ -267,7 +264,7 @@ func (s *submitContext) _documents() ([]workspace.Document, error) {
267
264
fmt .Fprintf (Err , msg , file )
268
265
continue
269
266
}
270
- doc , err := workspace .NewDocument (s . exercise .Filepath (), file )
267
+ doc , err := workspace .NewDocument (exercise .Filepath (), file )
271
268
if err != nil {
272
269
return nil , err
273
270
}
@@ -284,19 +281,33 @@ func (s *submitContext) _documents() ([]workspace.Document, error) {
284
281
return docs , nil
285
282
}
286
283
287
- // submitDocuments submits the documents to the API via HTTP.
288
- func (s * submitContext ) submitDocuments () error {
289
- if s .metadata .ID == "" {
284
+ func (s * submitContext ) printResult () {
285
+ msg := `
286
+
287
+ Your solution has been submitted successfully.
288
+ %s
289
+ `
290
+ suffix := "View it at:\n \n "
291
+ if s .metadata .AutoApprove && s .metadata .Team == "" {
292
+ suffix = "You can complete the exercise and unlock the next core exercise at:\n "
293
+ }
294
+ fmt .Fprintf (Err , msg , suffix )
295
+ fmt .Fprintf (Out , " %s\n \n " , s .metadata .URL )
296
+ }
297
+
298
+ // submitDocuments submits the documents to the Exercism API.
299
+ func submitDocuments (usrCfg * viper.Viper , metadata * workspace.ExerciseMetadata , docs []workspace.Document ) error {
300
+ if metadata .ID == "" {
290
301
return errors .New ("id is empty" )
291
302
}
292
- if len (s . documents ) == 0 {
303
+ if len (docs ) == 0 {
293
304
return errors .New ("documents is empty" )
294
305
}
295
306
296
307
body := & bytes.Buffer {}
297
308
writer := multipart .NewWriter (body )
298
309
299
- for _ , doc := range s . documents {
310
+ for _ , doc := range docs {
300
311
file , err := os .Open (doc .Filepath ())
301
312
if err != nil {
302
313
return err
@@ -316,11 +327,11 @@ func (s *submitContext) submitDocuments() error {
316
327
return err
317
328
}
318
329
319
- client , err := api .NewClient (s . usrCfg .GetString ("token" ), s . usrCfg .GetString ("apibaseurl" ))
330
+ client , err := api .NewClient (usrCfg .GetString ("token" ), usrCfg .GetString ("apibaseurl" ))
320
331
if err != nil {
321
332
return err
322
333
}
323
- url := fmt .Sprintf ("%s/solutions/%s" , s . usrCfg .GetString ("apibaseurl" ), s . metadata .ID )
334
+ url := fmt .Sprintf ("%s/solutions/%s" , usrCfg .GetString ("apibaseurl" ), metadata .ID )
324
335
req , err := client .NewRequest ("PATCH" , url , body )
325
336
if err != nil {
326
337
return err
@@ -350,20 +361,6 @@ func (s *submitContext) submitDocuments() error {
350
361
return nil
351
362
}
352
363
353
- func (s * submitContext ) printResult () {
354
- msg := `
355
-
356
- Your solution has been submitted successfully.
357
- %s
358
- `
359
- suffix := "View it at:\n \n "
360
- if s .metadata .AutoApprove && s .metadata .Team == "" {
361
- suffix = "You can complete the exercise and unlock the next core exercise at:\n "
362
- }
363
- fmt .Fprintf (Err , msg , suffix )
364
- fmt .Fprintf (Out , " %s\n \n " , s .metadata .URL )
365
- }
366
-
367
364
func init () {
368
365
RootCmd .AddCommand (submitCmd )
369
366
}
0 commit comments