Skip to content

Commit

Permalink
Improve command import and clean it
Browse files Browse the repository at this point in the history
  • Loading branch information
JulienBreux committed Jul 27, 2023
1 parent 5eb5f39 commit 192fca7
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions internal/mksctl/command/imp/imp.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,14 @@ func prepareBody(f file) (string, *bytes.Buffer, error) {
writer := multipart.NewWriter(body)
contentType := writer.FormDataContentType()

part, err := writer.CreateFormFile("file", filepath.Base(f.path))
if err != nil {
return contentType, nil, err
}
defer writer.Close()

if _, err = io.Copy(part, f.file); err != nil {
if err := writer.WriteField("mainArtifact", strconv.FormatBool(f.mainArtifact)); err != nil {
return contentType, nil, err
}

if err := writer.WriteField("mainArtifact", strconv.FormatBool(f.mainArtifact)); err != nil {
writer, err := prepareBodyFile(f, writer)
if err != nil {
return contentType, nil, err
}

Expand All @@ -129,6 +127,19 @@ func prepareBody(f file) (string, *bytes.Buffer, error) {
return contentType, body, nil
}

func prepareBodyFile(f file, w *multipart.Writer) (*multipart.Writer, error) {
part, err := w.CreateFormFile("file", filepath.Base(f.path))
if err != nil {
return nil, err
}

if _, err = io.Copy(part, f.file); err != nil {
return nil, err
}

return w, nil
}

func argsToFiles(args []string) ([]file, error) {
files := []file{}

Expand Down

0 comments on commit 192fca7

Please sign in to comment.