Skip to content

Commit 3491328

Browse files
committed
Make runSubmit more explicit
Remove submit type and move methods to submitCmdContext
1 parent 23afc48 commit 3491328

File tree

1 file changed

+37
-72
lines changed

1 file changed

+37
-72
lines changed

cmd/submit.go

Lines changed: 37 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -55,56 +55,44 @@ func runSubmit(cfg config.Config, flags *pflag.FlagSet, args []string) error {
5555
return err
5656
}
5757

58-
ctx, err := newSubmitCmdContext(cfg.UserViperConfig, flags, args)
59-
if err != nil {
60-
return err
61-
}
62-
63-
if err := ctx.submit(cfg.UserViperConfig); err != nil {
64-
return err
65-
}
66-
67-
ctx.printResult()
68-
return nil
69-
}
70-
71-
// submitCmdContext represents the context for the submit cmd.
72-
type submitCmdContext struct {
73-
usrCfg *viper.Viper
74-
flags *pflag.FlagSet
75-
submission
76-
}
77-
78-
// newSubmitCmdContext sets up a context to initiate a submission.
79-
func newSubmitCmdContext(usrCfg *viper.Viper, flags *pflag.FlagSet, args []string) (*submitCmdContext, error) {
80-
ctx := &submitCmdContext{usrCfg: usrCfg, flags: flags}
58+
ctx := &submitCmdContext{usrCfg: cfg.UserViperConfig, flags: flags}
8159

8260
filepaths, err := ctx.sanitizeArgs(args)
8361
if err != nil {
84-
return nil, err
62+
return err
8563
}
8664

8765
exercise, err := ctx.exercise(filepaths)
8866
if err != nil {
89-
return nil, err
67+
return err
9068
}
9169

9270
if err = ctx.migrateLegacyMetadata(exercise); err != nil {
93-
return nil, err
71+
return err
9472
}
9573

96-
documents, err := ctx._documents(filepaths, exercise)
74+
documents, err := ctx.documents(filepaths, exercise)
9775
if err != nil {
98-
return nil, err
76+
return err
9977
}
10078

101-
metadata, err := ctx._metadata(exercise)
79+
metadata, err := ctx.metadata(exercise)
10280
if err != nil {
103-
return nil, err
81+
return err
82+
}
83+
84+
if err := ctx.submit(metadata, documents); err != nil {
85+
return err
10486
}
10587

106-
ctx.submission = submission{documents: documents, metadata: metadata}
107-
return ctx, nil
88+
ctx.printResult(metadata)
89+
return nil
90+
}
91+
92+
// submitCmdContext represents the context for the submit cmd.
93+
type submitCmdContext struct {
94+
usrCfg *viper.Viper
95+
flags *pflag.FlagSet
10896
}
10997

11098
// sanitizeArgs validates args and swaps with evaluated symlink paths.
@@ -195,7 +183,7 @@ func (s *submitCmdContext) migrateLegacyMetadata(exercise workspace.Exercise) er
195183
return nil
196184
}
197185

198-
func (s *submitCmdContext) _metadata(exercise workspace.Exercise) (*workspace.ExerciseMetadata, error) {
186+
func (s *submitCmdContext) metadata(exercise workspace.Exercise) (*workspace.ExerciseMetadata, error) {
199187
metadata, err := workspace.NewExerciseMetadata(exercise.Filepath())
200188
if err != nil {
201189
return nil, err
@@ -230,7 +218,7 @@ func (s *submitCmdContext) _metadata(exercise workspace.Exercise) (*workspace.Ex
230218
return metadata, nil
231219
}
232220

233-
func (s *submitCmdContext) _documents(filepaths []string, exercise workspace.Exercise) ([]workspace.Document, error) {
221+
func (s *submitCmdContext) documents(filepaths []string, exercise workspace.Exercise) ([]workspace.Document, error) {
234222
docs := make([]workspace.Document, 0, len(filepaths))
235223
for _, file := range filepaths {
236224
// Don't submit empty files
@@ -276,36 +264,12 @@ func (s *submitCmdContext) _documents(filepaths []string, exercise workspace.Exe
276264
return docs, nil
277265
}
278266

279-
func (s *submitCmdContext) printResult() {
280-
msg := `
281-
282-
Your solution has been submitted successfully.
283-
%s
284-
`
285-
suffix := "View it at:\n\n "
286-
if s.metadata.AutoApprove && s.metadata.Team == "" {
287-
suffix = "You can complete the exercise and unlock the next core exercise at:\n"
288-
}
289-
fmt.Fprintf(Err, msg, suffix)
290-
fmt.Fprintf(Out, " %s\n\n", s.metadata.URL)
291-
}
292-
293-
// submission is a submission to the Excercism API.
294-
type submission struct {
295-
documents []workspace.Document
296-
metadata *workspace.ExerciseMetadata
297-
}
298-
299267
// submit submits the documents to the Exercism API.
300-
func (s *submission) submit(usrCfg *viper.Viper) error {
301-
if err := s.validate(); err != nil {
302-
return err
303-
}
304-
268+
func (s *submitCmdContext) submit(metadata *workspace.ExerciseMetadata, docs []workspace.Document) error {
305269
body := &bytes.Buffer{}
306270
writer := multipart.NewWriter(body)
307271

308-
for _, doc := range s.documents {
272+
for _, doc := range docs {
309273
file, err := os.Open(doc.Filepath())
310274
if err != nil {
311275
return err
@@ -325,11 +289,11 @@ func (s *submission) submit(usrCfg *viper.Viper) error {
325289
return err
326290
}
327291

328-
client, err := api.NewClient(usrCfg.GetString("token"), usrCfg.GetString("apibaseurl"))
292+
client, err := api.NewClient(s.usrCfg.GetString("token"), s.usrCfg.GetString("apibaseurl"))
329293
if err != nil {
330294
return err
331295
}
332-
url := fmt.Sprintf("%s/solutions/%s", usrCfg.GetString("apibaseurl"), s.metadata.ID)
296+
url := fmt.Sprintf("%s/solutions/%s", s.usrCfg.GetString("apibaseurl"), metadata.ID)
333297
req, err := client.NewRequest("PATCH", url, body)
334298
if err != nil {
335299
return err
@@ -359,17 +323,18 @@ func (s *submission) submit(usrCfg *viper.Viper) error {
359323
return nil
360324
}
361325

362-
func (s *submission) validate() error {
363-
if s == nil {
364-
return errors.New("submission is empty")
365-
}
366-
if s.metadata.ID == "" {
367-
return errors.New("id is empty")
368-
}
369-
if len(s.documents) == 0 {
370-
return errors.New("documents is empty")
326+
func (s *submitCmdContext) printResult(metadata *workspace.ExerciseMetadata) {
327+
msg := `
328+
329+
Your solution has been submitted successfully.
330+
%s
331+
`
332+
suffix := "View it at:\n\n "
333+
if metadata.AutoApprove && metadata.Team == "" {
334+
suffix = "You can complete the exercise and unlock the next core exercise at:\n"
371335
}
372-
return nil
336+
fmt.Fprintf(Err, msg, suffix)
337+
fmt.Fprintf(Out, " %s\n\n", metadata.URL)
373338
}
374339

375340
func init() {

0 commit comments

Comments
 (0)