@@ -50,18 +50,6 @@ var submitCmd = &cobra.Command{
50
50
},
51
51
}
52
52
53
- type submission struct {
54
- documents []workspace.Document
55
- metadata * workspace.ExerciseMetadata
56
- }
57
-
58
- // submitContext is a context for submitting solutions to the API.
59
- type submitContext struct {
60
- usrCfg * viper.Viper
61
- flags * pflag.FlagSet
62
- submission
63
- }
64
-
65
53
func runSubmit (cfg config.Config , flags * pflag.FlagSet , args []string ) error {
66
54
if err := validateUserConfig (cfg .UserViperConfig ); err != nil {
67
55
return err
@@ -72,14 +60,21 @@ func runSubmit(cfg config.Config, flags *pflag.FlagSet, args []string) error {
72
60
return err
73
61
}
74
62
75
- if err := submitDocuments (cfg .UserViperConfig , ctx . metadata , ctx . documents ); err != nil {
63
+ if err := ctx . submit (cfg .UserViperConfig ); err != nil {
76
64
return err
77
65
}
78
66
79
67
ctx .printResult ()
80
68
return nil
81
69
}
82
70
71
+ // submitContext is a context for submitting solutions to the API.
72
+ type submitContext struct {
73
+ usrCfg * viper.Viper
74
+ flags * pflag.FlagSet
75
+ submission
76
+ }
77
+
83
78
// newSubmitContext creates a submitContext.
84
79
func newSubmitContext (usrCfg * viper.Viper , flags * pflag.FlagSet , args []string ) (* submitContext , error ) {
85
80
ctx := & submitContext {usrCfg : usrCfg , flags : flags }
@@ -281,33 +276,21 @@ func (s *submitContext) _documents(filepaths []string, exercise workspace.Exerci
281
276
return docs , nil
282
277
}
283
278
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 )
279
+ type submission struct {
280
+ documents []workspace.Document
281
+ metadata * workspace.ExerciseMetadata
296
282
}
297
283
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 == "" {
301
- return errors .New ("id is empty" )
302
- }
303
- if len (docs ) == 0 {
304
- return errors .New ("documents is empty" )
284
+ // submit submits the documents to the Exercism API.
285
+ func (s submission ) submit (usrCfg * viper.Viper ) error {
286
+ if err := s .validate (); err != nil {
287
+ return err
305
288
}
306
289
307
290
body := & bytes.Buffer {}
308
291
writer := multipart .NewWriter (body )
309
292
310
- for _ , doc := range docs {
293
+ for _ , doc := range s . documents {
311
294
file , err := os .Open (doc .Filepath ())
312
295
if err != nil {
313
296
return err
@@ -331,7 +314,7 @@ func submitDocuments(usrCfg *viper.Viper, metadata *workspace.ExerciseMetadata,
331
314
if err != nil {
332
315
return err
333
316
}
334
- url := fmt .Sprintf ("%s/solutions/%s" , usrCfg .GetString ("apibaseurl" ), metadata .ID )
317
+ url := fmt .Sprintf ("%s/solutions/%s" , usrCfg .GetString ("apibaseurl" ), s . metadata .ID )
335
318
req , err := client .NewRequest ("PATCH" , url , body )
336
319
if err != nil {
337
320
return err
@@ -361,6 +344,30 @@ func submitDocuments(usrCfg *viper.Viper, metadata *workspace.ExerciseMetadata,
361
344
return nil
362
345
}
363
346
347
+ func (s submission ) printResult () {
348
+ msg := `
349
+
350
+ Your solution has been submitted successfully.
351
+ %s
352
+ `
353
+ suffix := "View it at:\n \n "
354
+ if s .metadata .AutoApprove && s .metadata .Team == "" {
355
+ suffix = "You can complete the exercise and unlock the next core exercise at:\n "
356
+ }
357
+ fmt .Fprintf (Err , msg , suffix )
358
+ fmt .Fprintf (Out , " %s\n \n " , s .metadata .URL )
359
+ }
360
+
361
+ func (s submission ) validate () error {
362
+ if s .metadata .ID == "" {
363
+ return errors .New ("id is empty" )
364
+ }
365
+ if len (s .documents ) == 0 {
366
+ return errors .New ("documents is empty" )
367
+ }
368
+ return nil
369
+ }
370
+
364
371
func init () {
365
372
RootCmd .AddCommand (submitCmd )
366
373
}
0 commit comments