Skip to content

Commit

Permalink
Enable auth when downloading github assets; use asset url instead of …
Browse files Browse the repository at this point in the history
…browser url (#24)
  • Loading branch information
shopeonarope authored Aug 10, 2023
1 parent 34945f2 commit 13c55d1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type GithubRelease struct {
// GithubAsset contains information about a specific GitHub asset
type GithubAsset struct {
Name string `json:"name"`
DownloadURL string `json:"browser_download_url"`
DownloadURL string `json:"url"`
Size int `json:"size"`
ContentType string `json:"content_type"`
}
Expand Down
26 changes: 13 additions & 13 deletions lib/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,31 @@ var testGithubRelease0 GithubRelease = GithubRelease{
Assets: []GithubAsset{
{
Name: "checksums.txt",
DownloadURL: "https://github.com/marwanhawari/ppath/releases/download/v0.0.3/checksums.txt",
DownloadURL: "https://api.github.com/repos/marwanhawari/ppath/releases/assets/52676090",
Size: 394,
ContentType: "text/plain; charset=utf-8",
},
{
Name: "ppath-v0.0.3-darwin-amd64.tar.gz",
DownloadURL: "https://github.com/marwanhawari/ppath/releases/download/v0.0.3/ppath-v0.0.3-darwin-amd64.tar.gz",
DownloadURL: "https://api.github.com/repos/marwanhawari/ppath/releases/assets/52676089",
Size: 626448,
ContentType: "application/gzip",
},
{
Name: "ppath-v0.0.3-darwin-arm64.tar.gz",
DownloadURL: "https://github.com/marwanhawari/ppath/releases/download/v0.0.3/ppath-v0.0.3-darwin-arm64.tar.gz",
DownloadURL: "https://api.github.com/repos/marwanhawari/ppath/releases/assets/52676091",
Size: 625832,
ContentType: "application/gzip",
},
{
Name: "ppath-v0.0.3-linux-amd64.tar.gz",
DownloadURL: "https://github.com/marwanhawari/ppath/releases/download/v0.0.3/ppath-v0.0.3-linux-amd64.tar.gz",
DownloadURL: "https://api.github.com/repos/marwanhawari/ppath/releases/assets/52676093",
Size: 567449,
ContentType: "application/gzip",
},
{
Name: "ppath-v0.0.3-linux-arm64.tar.gz",
DownloadURL: "https://github.com/marwanhawari/ppath/releases/download/v0.0.3/ppath-v0.0.3-linux-arm64.tar.gz",
DownloadURL: "https://api.github.com/repos/marwanhawari/ppath/releases/assets/52676092",
Size: 529321,
ContentType: "application/gzip",
},
Expand All @@ -45,25 +45,25 @@ var testGithubRelease1 GithubRelease = GithubRelease{
Assets: []GithubAsset{
{
Name: "ppath-v0.0.2-darwin-amd64.tar.gz",
DownloadURL: "https://github.com/marwanhawari/ppath/releases/download/v0.0.2/ppath-v0.0.2-darwin-amd64.tar.gz",
DownloadURL: "https://api.github.com/repos/marwanhawari/ppath/releases/assets/52647309",
Size: 1139147,
ContentType: "application/x-gtar",
},
{
Name: "ppath-v0.0.2-darwin-arm64.tar.gz",
DownloadURL: "https://github.com/marwanhawari/ppath/releases/download/v0.0.2/ppath-v0.0.2-darwin-arm64.tar.gz",
DownloadURL: "https://api.github.com/repos/marwanhawari/ppath/releases/assets/52647324",
Size: 1100483,
ContentType: "application/x-gtar",
},
{
Name: "ppath-v0.0.2-linux-amd64.tar.gz",
DownloadURL: "https://github.com/marwanhawari/ppath/releases/download/v0.0.2/ppath-v0.0.2-linux-amd64.tar.gz",
DownloadURL: "https://api.github.com/repos/marwanhawari/ppath/releases/assets/52647307",
Size: 1092421,
ContentType: "application/x-gtar",
},
{
Name: "ppath-v0.0.2-linux-arm64.tar.gz",
DownloadURL: "https://github.com/marwanhawari/ppath/releases/download/v0.0.2/ppath-v0.0.2-linux-arm64.tar.gz",
DownloadURL: "https://api.github.com/repos/marwanhawari/ppath/releases/assets/52647308",
Size: 1012554,
ContentType: "application/x-gtar",
},
Expand All @@ -74,25 +74,25 @@ var testGithubRelease2 GithubRelease = GithubRelease{
Assets: []GithubAsset{
{
Name: "ppath-v0.0.1-darwin-amd64.tar.gz",
DownloadURL: "https://github.com/marwanhawari/ppath/releases/download/v0.0.1/ppath-v0.0.1-darwin-amd64.tar.gz",
DownloadURL: "https://api.github.com/repos/marwanhawari/ppath/releases/assets/51111591",
Size: 1139366,
ContentType: "application/x-gtar",
},
{
Name: "ppath-v0.0.1-darwin-arm64.tar.gz",
DownloadURL: "https://github.com/marwanhawari/ppath/releases/download/v0.0.1/ppath-v0.0.1-darwin-arm64.tar.gz",
DownloadURL: "https://api.github.com/repos/marwanhawari/ppath/releases/assets/51111587",
Size: 1101013,
ContentType: "application/x-gtar",
},
{
Name: "ppath-v0.0.1-linux-amd64.tar.gz",
DownloadURL: "https://github.com/marwanhawari/ppath/releases/download/v0.0.1/ppath-v0.0.1-linux-amd64.tar.gz",
DownloadURL: "https://api.github.com/repos/marwanhawari/ppath/releases/assets/51111593",
Size: 1093728,
ContentType: "application/x-gtar",
},
{
Name: "ppath-v0.0.1-linux-arm64.tar.gz",
DownloadURL: "https://github.com/marwanhawari/ppath/releases/download/v0.0.1/ppath-v0.0.1-linux-arm64.tar.gz",
DownloadURL: "https://api.github.com/repos/marwanhawari/ppath/releases/assets/51111599",
Size: 1014337,
ContentType: "application/x-gtar",
},
Expand Down
16 changes: 15 additions & 1 deletion lib/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,21 @@ func PathExists(path string) (bool, error) {
func DownloadFile(downloadPath string, url string) error {
sp := constants.LoadingSpinner
sp.Start()
resp, err := http.Get(url)
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}

if strings.Contains(url, "api.github.com") {
req.Header.Add("Accept", "application/octet-stream")
githubToken := os.Getenv("GITHUB_TOKEN")
if githubToken != "" {
req.Header.Add("Authorization", fmt.Sprintf("token %v", githubToken))
}
}

resp, err := client.Do(req)
sp.Stop()

if err != nil {
Expand Down

0 comments on commit 13c55d1

Please sign in to comment.