Split out a method for doing a single upload attempt to prevent resource leaks.

This commit is contained in:
Chris Gavin 2022-06-14 14:54:49 +01:00
Родитель d089cfe8ca
Коммит 6bebcc22ba
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 07F950B80C27E4DA
1 изменённых файлов: 19 добавлений и 14 удалений

Просмотреть файл

@ -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 {
@ -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 {