Skip to content

Commit 65848da

Browse files
committed
Move methods to submit type
This makes submit testable in isolation.
1 parent f021f71 commit 65848da

File tree

1 file changed

+41
-34
lines changed

1 file changed

+41
-34
lines changed

cmd/submit.go

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,6 @@ var submitCmd = &cobra.Command{
5050
},
5151
}
5252

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-
6553
func runSubmit(cfg config.Config, flags *pflag.FlagSet, args []string) error {
6654
if err := validateUserConfig(cfg.UserViperConfig); err != nil {
6755
return err
@@ -72,14 +60,21 @@ func runSubmit(cfg config.Config, flags *pflag.FlagSet, args []string) error {
7260
return err
7361
}
7462

75-
if err := submitDocuments(cfg.UserViperConfig, ctx.metadata, ctx.documents); err != nil {
63+
if err := ctx.submit(cfg.UserViperConfig); err != nil {
7664
return err
7765
}
7866

7967
ctx.printResult()
8068
return nil
8169
}
8270

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+
8378
// newSubmitContext creates a submitContext.
8479
func newSubmitContext(usrCfg *viper.Viper, flags *pflag.FlagSet, args []string) (*submitContext, error) {
8580
ctx := &submitContext{usrCfg: usrCfg, flags: flags}
@@ -281,33 +276,21 @@ func (s *submitContext) _documents(filepaths []string, exercise workspace.Exerci
281276
return docs, nil
282277
}
283278

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
296282
}
297283

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
305288
}
306289

307290
body := &bytes.Buffer{}
308291
writer := multipart.NewWriter(body)
309292

310-
for _, doc := range docs {
293+
for _, doc := range s.documents {
311294
file, err := os.Open(doc.Filepath())
312295
if err != nil {
313296
return err
@@ -331,7 +314,7 @@ func submitDocuments(usrCfg *viper.Viper, metadata *workspace.ExerciseMetadata,
331314
if err != nil {
332315
return err
333316
}
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)
335318
req, err := client.NewRequest("PATCH", url, body)
336319
if err != nil {
337320
return err
@@ -361,6 +344,30 @@ func submitDocuments(usrCfg *viper.Viper, metadata *workspace.ExerciseMetadata,
361344
return nil
362345
}
363346

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+
364371
func init() {
365372
RootCmd.AddCommand(submitCmd)
366373
}

0 commit comments

Comments
 (0)