Skip to content

Commit

Permalink
CLI-6 File: Download: Fix file descriptors proper close
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewLab-lokalise committed Jul 28, 2020
1 parent 8875e33 commit 67d8c7b
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions cmd/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,32 +314,39 @@ func init() {
fs.DurationVar(&uploadPollingTimeout, "poll-timeout", 30*time.Second, "Specify custom file upload polling maximum duration. Default: 30s")
}

//noinspection GoUnhandledErrorResult
func downloadAndUnzip(srcUrl, destPath, unzipPath string) error {
fileName := path.Base(srcUrl)
zipFile, err := os.Create(path.Join(destPath, fileName))
if err != nil {
return err
}
defer zipFile.Close()

resp, err := http.Get(srcUrl)
if err != nil {
return err
}
defer resp.Body.Close()

_, err = io.Copy(zipFile, resp.Body)
if err != nil {
return err
}

err = resp.Body.Close()
if err != nil {
return err
}

fmt.Println("Unzipping to", unzipPath+"...")
err = unzip(zipFile.Name(), unzipPath)
if err != nil {
return err
}

err = zipFile.Close()
if err != nil {
return err
}

if !downloadKeepZip {
_ = os.Remove(zipFile.Name())
}
Expand Down

0 comments on commit 67d8c7b

Please sign in to comment.