Skip to content

Instantly share code, notes, and snippets.

@lclarkmichalek
Created December 23, 2016 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lclarkmichalek/f1bb6a3f474c2caf118d2c97608a080f to your computer and use it in GitHub Desktop.
Save lclarkmichalek/f1bb6a3f474c2caf118d2c97608a080f to your computer and use it in GitHub Desktop.
url := fmt.Sprintf("%v/dist/v1/%v/artifact/%v/%v", baseURL, repoName, version, runtime.GOOS)
debug("GET", url)
resp, err := client.Get(url)
if err != nil {
return errors.Wrap(err, "could not request latest version download")
}
defer resp.Body.Close()
debug("GET", resp.StatusCode, url)
if !(200 <= resp.StatusCode && resp.StatusCode < 300) {
data, _ := ioutil.ReadAll(resp.Body)
return errors.Errorf("invalid status code %v: %v", resp.StatusCode, string(data))
}
decompressed, err := gzip.NewReader(resp.Body)
if err != nil {
return errors.Wrap(err, "could not decode gzip header")
}
tarArchive := tar.NewReader(decompressed)
binaryName := fmt.Sprintf("%v-%v.%v-amd64/aton", repoName, version, runtime.GOOS)
debug("looking for", binaryName, "in archive")
for {
hdr, err := tarArchive.Next()
if err != nil && err == io.EOF {
break
} else if err != nil {
return errors.Wrap(err, "could not decode response")
}
debug("found", hdr.Name)
if hdr.Name != binaryName {
continue
}
_, err = io.Copy(target, tarArchive)
if err != nil {
return errors.Wrap(err, "could not copy response into file")
}
return nil
}
return errors.Errorf("could not find %v in archive", binaryName)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment