Skip to content

Commit

Permalink
Split out a method for doing a single upload attempt to prevent resou…
Browse files Browse the repository at this point in the history
…rce leaks.
  • Loading branch information
chrisgavin committed Jun 14, 2022
1 parent d089cfe commit 6bebcc2
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions internal/push/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,24 @@ func (pushService *pushService) uploadReleaseAsset(release *github.RepositoryRel
return asset, response, nil
}

func (pushService *pushService) uploadAsset(release *github.RepositoryRelease, assetPathStat os.FileInfo) (*github.Response, error) {
assetFile, err := os.Open(pushService.cacheDirectory.AssetPath(release.GetTagName(), assetPathStat.Name()))
if err != nil {
return nil, errors.Wrap(err, "Error opening release asset.")
}
defer assetFile.Close()
progressReader := &ioprogress.Reader{
Reader: assetFile,
Size: assetPathStat.Size(),
DrawFunc: ioprogress.DrawTerminalf(os.Stderr, ioprogress.DrawTextFormatBytes),
}
if err != nil {
return nil, errors.Wrap(err, "Error opening release asset.")
}
_, response, err := pushService.uploadReleaseAsset(release, assetPathStat, progressReader)
return response, err
}

func (pushService *pushService) createOrUpdateReleaseAsset(release *github.RepositoryRelease, existingAssets []*github.ReleaseAsset, assetPathStat os.FileInfo) error {
attempt := 0
for {
Expand All @@ -327,20 +345,7 @@ func (pushService *pushService) createOrUpdateReleaseAsset(release *github.Repos
}
}
log.Debugf("Uploading release asset %s...", assetPathStat.Name())
assetFile, err := os.Open(pushService.cacheDirectory.AssetPath(release.GetTagName(), assetPathStat.Name()))
if err != nil {
return errors.Wrap(err, "Error opening release asset.")
}
defer assetFile.Close()
progressReader := &ioprogress.Reader{
Reader: assetFile,
Size: assetPathStat.Size(),
DrawFunc: ioprogress.DrawTerminalf(os.Stderr, ioprogress.DrawTextFormatBytes),
}
if err != nil {
return errors.Wrap(err, "Error opening release asset.")
}
_, response, err := pushService.uploadReleaseAsset(release, assetPathStat, progressReader)
response, err := pushService.uploadAsset(release, assetPathStat)
if err == nil {
return nil
} else {
Expand Down

0 comments on commit 6bebcc2

Please sign in to comment.