Created
December 23, 2016 12:48
-
-
Save lclarkmichalek/f1bb6a3f474c2caf118d2c97608a080f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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