fall back to git if github archive download fails for any reason

This commit is contained in:
Benoit Gagnon 2019-09-28 11:41:08 -04:00
parent 2b485512c0
commit 741e7f316f

View file

@ -167,30 +167,31 @@ func (p *GitPackage) Install(ctx context.Context, name, dir, version string) (st
defer os.RemoveAll(tmpDir) defer os.RemoveAll(tmpDir)
// Optimization for GitHub sources: download a tarball archive of the requested // Optimization for GitHub sources: download a tarball archive of the requested
// version instead of cloning the entire repository. Resolves the version to a // version instead of cloning the entire repository. The SHA1 is discovered through
// commit SHA using the GitHub API. // the ETag header included in the response.
isGitHubRemote, err := regexp.MatchString(`^(https|ssh)://github\.com/.+$`, p.Source.Remote) isGitHubRemote, err := regexp.MatchString(`^(https|ssh)://github\.com/.+$`, p.Source.Remote)
if isGitHubRemote { if isGitHubRemote {
archiveUrl := fmt.Sprintf("%s/archive/%s.tar.gz", p.Source.Remote, version) archiveUrl := fmt.Sprintf("%s/archive/%s.tar.gzz", p.Source.Remote, version)
archiveFilepath := fmt.Sprintf("%s.tar.gz", tmpDir) archiveFilepath := fmt.Sprintf("%s.tar.gz", tmpDir)
defer os.Remove(archiveFilepath) defer os.Remove(archiveFilepath)
commitSha, err := downloadGitHubArchive(archiveFilepath, archiveUrl) commitSha, err := downloadGitHubArchive(archiveFilepath, archiveUrl)
if err != nil { if err == nil {
return "", err r, err := os.Open(archiveFilepath)
defer r.Close()
if err == nil {
err = gzipUntar(tmpDir, r, p.Source.Subdir)
}
} }
r, err := os.Open(archiveFilepath) if err == nil {
if err != nil { return commitSha, nil
return "", err
} }
err = gzipUntar(tmpDir, r, p.Source.Subdir) // The repository may be private or the archive download may not work
if err != nil { // for other reasons. In any case, fall back to the slower git-based installation.
return "", err color.Yellow("archive install failed: %s", err)
} color.Yellow("retrying with git...")
return commitSha, nil
} }
cmd := exec.CommandContext(ctx, "git", "init") cmd := exec.CommandContext(ctx, "git", "init")