Skip to content

Commit

Permalink
Merge related methods
Browse files Browse the repository at this point in the history
Previously, `submitRequest` depended directly on state of writer from `writeFormFiles`. These methods are related enough that they can be merged, avoiding potential maintenance problems and resulting in a more simple API.
  • Loading branch information
jdsutherland committed Jan 2, 2019
1 parent 9e94e35 commit 4a392b8
Showing 1 changed file with 5 additions and 22 deletions.
27 changes: 5 additions & 22 deletions cmd/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,7 @@ func runSubmit(cfg config.Config, flags *pflag.FlagSet, args []string) error {
return err
}

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

if err := ctx.writeFormFiles(writer, exercise.Documents); err != nil {
return err
}

if err := ctx.submitRequest(metadata.ID, writer, body); err != nil {
if err := ctx.submitRequest(metadata.ID, exercise.Documents); err != nil {
return err
}

Expand Down Expand Up @@ -275,14 +268,14 @@ func (ctx *submitContext) documents(exerciseDir string) ([]workspace.Document, e
return docs, nil
}

func (ctx *submitContext) writeFormFiles(writer *multipart.Writer, docs []workspace.Document) error {
if writer == nil {
return errors.New("writer is empty")
}
func (ctx *submitContext) submitRequest(id string, docs []workspace.Document) error {
if len(docs) == 0 {
return errors.New("docs is empty")
}

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

for _, doc := range docs {
file, err := os.Open(doc.Filepath())
if err != nil {
Expand All @@ -302,16 +295,6 @@ func (ctx *submitContext) writeFormFiles(writer *multipart.Writer, docs []worksp
if err := writer.Close(); err != nil {
return err
}
return nil
}

func (ctx *submitContext) submitRequest(id string, writer *multipart.Writer, body *bytes.Buffer) error {
if writer == nil {
return errors.New("writer is empty")
}
if body.Len() == 0 {
return errors.New("body is empty")
}

client, err := api.NewClient(ctx.usrCfg.GetString("token"), ctx.usrCfg.GetString("apibaseurl"))
if err != nil {
Expand Down

0 comments on commit 4a392b8

Please sign in to comment.