Skip to content

Commit f021f71

Browse files
committed
Make dependencies explicit
1 parent a5e6814 commit f021f71

File tree

1 file changed

+53
-56
lines changed

1 file changed

+53
-56
lines changed

cmd/submit.go

Lines changed: 53 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,14 @@ var submitCmd = &cobra.Command{
5151
}
5252

5353
type submission struct {
54-
exercise workspace.Exercise
55-
metadata *workspace.ExerciseMetadata
5654
documents []workspace.Document
55+
metadata *workspace.ExerciseMetadata
5756
}
5857

5958
// submitContext is a context for submitting solutions to the API.
6059
type submitContext struct {
6160
usrCfg *viper.Viper
6261
flags *pflag.FlagSet
63-
args []string
6462
submission
6563
}
6664

@@ -74,7 +72,7 @@ func runSubmit(cfg config.Config, flags *pflag.FlagSet, args []string) error {
7472
return err
7573
}
7674

77-
if err := ctx.submitDocuments(); err != nil {
75+
if err := submitDocuments(cfg.UserViperConfig, ctx.metadata, ctx.documents); err != nil {
7876
return err
7977
}
8078

@@ -84,44 +82,43 @@ func runSubmit(cfg config.Config, flags *pflag.FlagSet, args []string) error {
8482

8583
// newSubmitContext creates a submitContext.
8684
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}
8886

89-
if err := ctx.sanitizeArgs(); err != nil {
87+
filepaths, err := ctx.sanitizeArgs(args)
88+
if err != nil {
9089
return nil, err
9190
}
9291

93-
exercise, err := ctx._exercise()
92+
exercise, err := ctx.exercise(filepaths)
9493
if err != nil {
9594
return nil, err
9695
}
97-
ctx.exercise = exercise
9896

99-
if err = ctx.migrateLegacyMetadata(); err != nil {
97+
if err = ctx.migrateLegacyMetadata(exercise); err != nil {
10098
return nil, err
10199
}
102100

103-
metadata, err := ctx._metadata()
101+
documents, err := ctx._documents(filepaths, exercise)
104102
if err != nil {
105103
return nil, err
106104
}
107-
ctx.metadata = metadata
108105

109-
documents, err := ctx._documents()
106+
metadata, err := ctx._metadata(exercise)
110107
if err != nil {
111108
return nil, err
112109
}
113-
ctx.documents = documents
114110

111+
ctx.submission = submission{documents: documents, metadata: metadata}
115112
return ctx, nil
116113
}
117114

118115
// 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 {
121118
var err error
122119
arg, err = filepath.Abs(arg)
123120
if err != nil {
124-
return err
121+
return nil, err
125122
}
126123

127124
info, err := os.Lstat(arg)
@@ -134,9 +131,9 @@ func (s *submitContext) sanitizeArgs() error {
134131
%s
135132
136133
`
137-
return fmt.Errorf(msg, arg)
134+
return nil, fmt.Errorf(msg, arg)
138135
}
139-
return err
136+
return nil, err
140137
}
141138
if info.IsDir() {
142139
msg := `
@@ -150,27 +147,27 @@ func (s *submitContext) sanitizeArgs() error {
150147
%s submit FILENAME
151148
152149
`
153-
return fmt.Errorf(msg, arg, BinaryName)
150+
return nil, fmt.Errorf(msg, arg, BinaryName)
154151
}
155152

156153
src, err := filepath.EvalSymlinks(arg)
157154
if err != nil {
158-
return err
155+
return nil, err
159156
}
160-
s.args[i] = src
157+
args[i] = src
161158
}
162-
return nil
159+
return args, nil
163160
}
164161

165-
func (s *submitContext) _exercise() (workspace.Exercise, error) {
162+
func (s *submitContext) exercise(filepaths []string) (workspace.Exercise, error) {
166163
ws, err := workspace.New(s.usrCfg.GetString("workspace"))
167164
if err != nil {
168165
return workspace.Exercise{}, err
169166
}
170167

171168
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)
174171
if err != nil {
175172
if workspace.IsMissingMetadata(err) {
176173
return workspace.Exercise{}, errors.New(msgMissingMetadata)
@@ -192,8 +189,8 @@ func (s *submitContext) _exercise() (workspace.Exercise, error) {
192189
return workspace.NewExerciseFromDir(exerciseDir), nil
193190
}
194191

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()
197194
if err != nil {
198195
return err
199196
}
@@ -203,13 +200,13 @@ func (s *submitContext) migrateLegacyMetadata() error {
203200
return nil
204201
}
205202

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())
208205
if err != nil {
209206
return nil, err
210207
}
211208

212-
if metadata.Exercise != s.exercise.Slug {
209+
if metadata.Exercise != exercise.Slug {
213210
// TODO: error msg should suggest running future doctor command
214211
msg := `
215212
@@ -220,7 +217,7 @@ func (s *submitContext) _metadata() (*workspace.ExerciseMetadata, error) {
220217
Please rename the directory '%[1]s' to '%[2]s' and try again.
221218
222219
`
223-
return nil, fmt.Errorf(msg, s.exercise.Slug, metadata.Exercise)
220+
return nil, fmt.Errorf(msg, exercise.Slug, metadata.Exercise)
224221
}
225222

226223
if !metadata.IsRequester {
@@ -238,9 +235,9 @@ func (s *submitContext) _metadata() (*workspace.ExerciseMetadata, error) {
238235
return metadata, nil
239236
}
240237

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 {
244241
// Don't submit empty files
245242
info, err := os.Stat(file)
246243
if err != nil {
@@ -267,7 +264,7 @@ func (s *submitContext) _documents() ([]workspace.Document, error) {
267264
fmt.Fprintf(Err, msg, file)
268265
continue
269266
}
270-
doc, err := workspace.NewDocument(s.exercise.Filepath(), file)
267+
doc, err := workspace.NewDocument(exercise.Filepath(), file)
271268
if err != nil {
272269
return nil, err
273270
}
@@ -284,19 +281,33 @@ func (s *submitContext) _documents() ([]workspace.Document, error) {
284281
return docs, nil
285282
}
286283

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 == "" {
290301
return errors.New("id is empty")
291302
}
292-
if len(s.documents) == 0 {
303+
if len(docs) == 0 {
293304
return errors.New("documents is empty")
294305
}
295306

296307
body := &bytes.Buffer{}
297308
writer := multipart.NewWriter(body)
298309

299-
for _, doc := range s.documents {
310+
for _, doc := range docs {
300311
file, err := os.Open(doc.Filepath())
301312
if err != nil {
302313
return err
@@ -316,11 +327,11 @@ func (s *submitContext) submitDocuments() error {
316327
return err
317328
}
318329

319-
client, err := api.NewClient(s.usrCfg.GetString("token"), s.usrCfg.GetString("apibaseurl"))
330+
client, err := api.NewClient(usrCfg.GetString("token"), usrCfg.GetString("apibaseurl"))
320331
if err != nil {
321332
return err
322333
}
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)
324335
req, err := client.NewRequest("PATCH", url, body)
325336
if err != nil {
326337
return err
@@ -350,20 +361,6 @@ func (s *submitContext) submitDocuments() error {
350361
return nil
351362
}
352363

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-
367364
func init() {
368365
RootCmd.AddCommand(submitCmd)
369366
}

0 commit comments

Comments
 (0)