Skip to content

Commit 9de1350

Browse files
committed
Fall back to source install only when binary URL is 404
When the install from a binary package fails it falls back to installing from source. Tthe only case where the fallback should occur is when the server responds with a 404 that no binary package exists. Otherwise it's falling back on various IO or permissions errors. Fixes #30
1 parent 9c6bbb8 commit 9de1350

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
77
### Fixed
88

99
- Fix errors with renames failing across disks by falling back to a copy/delete. [#31](https://github.com/andrewkroh/gvm/pull/31)
10-
10+
- Fall back to a source code based install only when binary package URL returns
11+
with HTTP 404. [#30](https://github.com/andrewkroh/gvm/issues/30) [#32](https://github.com/andrewkroh/gvm/pull/32)
12+
1113
## [0.2.3]
1214

1315
### Changed

common/common.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,26 @@ import (
1515

1616
var log = logrus.WithField("package", "common")
1717

18+
// ErrNotFound is returned when the download fails due to HTTP 404 Not Found.
19+
var ErrNotFound = errors.New("not found")
20+
1821
func DownloadFile(url, destinationDir string) (string, error) {
19-
log.WithField("url", url).Debug("downloading file")
22+
log.WithField("url", url).Debug("Downloading file")
2023
var name string
2124
var err error
2225
var retry bool
2326
for a := 1; a <= 3; a++ {
2427
name, err, retry = downloadFile(url, destinationDir)
2528
if err != nil && retry {
26-
log.WithError(err).Debugf("Attempt %d failed", a)
29+
log.WithError(err).Debugf("Download attempt %d failed", a)
2730
continue
2831
}
2932
break
3033
}
3134
return name, err
3235
}
3336

34-
func downloadFile(url, destinationDir string) (string, error, bool) {
37+
func downloadFile(url, destinationDir string) (path string, err error, retryable bool) {
3538
client := http.Client{
3639
Timeout: time.Duration(3 * time.Minute),
3740
}
@@ -42,7 +45,10 @@ func downloadFile(url, destinationDir string) (string, error, bool) {
4245
defer resp.Body.Close()
4346

4447
if resp.StatusCode != http.StatusOK {
45-
return "", errors.Errorf("download failed with http status %v", resp.StatusCode), resp.StatusCode != http.StatusNotFound
48+
if resp.StatusCode == http.StatusNotFound {
49+
return "", ErrNotFound, false
50+
}
51+
return "", errors.Errorf("download failed with http status %v", resp.StatusCode), true
4652
}
4753

4854
name := filepath.Join(destinationDir, filepath.Base(url))
@@ -55,7 +61,7 @@ func downloadFile(url, destinationDir string) (string, error, bool) {
5561
if err != nil {
5662
return "", errors.Wrap(err, "failed to write file to disk"), true
5763
}
58-
log.WithFields(logrus.Fields{"file": name, "size_bytes": numBytes}).Debug("download complete")
64+
log.WithFields(logrus.Fields{"file": name, "size_bytes": numBytes}).Debug("Download complete")
5965

6066
return name, nil, false
6167
}

gvm.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package gvm
22

33
import (
4+
"errors"
45
"fmt"
56
"io/ioutil"
67
"os"
@@ -9,6 +10,8 @@ import (
910
"strings"
1011

1112
"github.com/sirupsen/logrus"
13+
14+
"github.com/andrewkroh/gvm/common"
1215
)
1316

1417
type Manager struct {
@@ -214,9 +217,10 @@ func (m *Manager) Install(version *GoVersion) (string, error) {
214217
if err == nil {
215218
return dir, nil
216219
}
217-
m.Logger.WithFields(logrus.Fields{"version": version, "error": err}).
218-
Warn("Failed to install Go from binary package. Trying to " +
219-
"install from source.")
220+
// Only continue to installing from source if the server confirms 404.
221+
if !errors.Is(err, common.ErrNotFound) {
222+
return "", err
223+
}
220224
}
221225

222226
return m.installSrc(version)

0 commit comments

Comments
 (0)