improve github archive download error reporting

This commit is contained in:
Benoit Gagnon 2019-09-28 11:40:09 -04:00
parent 524c820a94
commit 2b485512c0

View file

@ -52,15 +52,22 @@ func downloadGitHubArchive(filepath string, url string) (string, error) {
return "", err
}
color.Cyan("GET %s %d", url, resp.StatusCode)
if resp.StatusCode != 200 {
return "", errors.New(fmt.Sprintf("unexpected status code %d", resp.StatusCode))
}
// GitHub conveniently uses the commit SHA1 at the ETag
// signature for the archive. This is needed when doing `jb update`
// to resolve a ref (ie. "master") to a commit SHA1 for the lock file
etagValue := resp.Header.Get(http.CanonicalHeaderKey("ETag"))
if etagValue == "" {
return "", errors.New("ETag header is missing from response")
}
commitShaPattern, _ := regexp.Compile("^\"([0-9a-f]{40})\"$")
m := commitShaPattern.FindStringSubmatch(etagValue)
if len(m) < 2 {
return "", errors.New(fmt.Sprintf("unexpected etag format: %s", etagValue))
return "", errors.New(fmt.Sprintf("etag value \"%s\" does not look like a SHA1", etagValue))
}
commitSha := m[1]
defer resp.Body.Close()