Skip to content

Commit

Permalink
feat: can download private files
Browse files Browse the repository at this point in the history
Signed-off-by: Kasper J. Hermansen <[email protected]>
  • Loading branch information
kjuulh committed May 17, 2024
1 parent 2a802db commit 9eabf2b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
28 changes: 26 additions & 2 deletions internal/extensions/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"log"
"net/http"
"os"
"os/exec"
"strings"
"time"
)

Expand Down Expand Up @@ -44,11 +46,18 @@ func (d *gitHubReleaseDownloader) Download(ctx context.Context, dest string) err
}

if accessToken := os.Getenv("SHUTTLE_EXTENSIONS_GITHUB_ACCESS_TOKEN"); accessToken != "" {
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", accessToken))
log.Println("using extensions github access token")
req.Header.Add("Authorization", fmt.Sprintf("token %s", accessToken))
} else if accessToken := os.Getenv("GITHUB_ACCESS_TOKEN"); accessToken != "" {
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", accessToken))
log.Println("using github access token")
req.Header.Add("Authorization", fmt.Sprintf("token %s", accessToken))
} else if accessToken, ok := getToken(); ok {
log.Println("using gh token")
req.Header.Add("Authorization", fmt.Sprintf("token %s", accessToken))
}

req.Header.Add("Accept", "application/octet-stream")

resp, err := client.Do(req)
if err != nil {
return err
Expand All @@ -72,3 +81,18 @@ func (d *gitHubReleaseDownloader) Download(ctx context.Context, dest string) err

return nil
}

func getToken() (string, bool) {
tokenRaw, err := exec.Command("gh", "auth", "token").Output()
if err != nil {
return "", false
}

token := string(tokenRaw)

if token != "" {
return strings.TrimSpace(token), false
}

return "", false
}
3 changes: 2 additions & 1 deletion internal/extensions/github_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (gc *githubClient) UpsertFile(ctx context.Context, shuttleExtensionsFile *s
downloadLink := registryExtensionDownloadLink{
Architecture: arch,
Os: os,
Url: releaseAsset.DownloadUrl,
Url: releaseAsset.Url,
Provider: "github-release",
}

Expand Down Expand Up @@ -192,6 +192,7 @@ func (gc *githubClient) GetRelease(ctx context.Context, shuttleExtensionsFile *s

type githubReleaseAsset struct {
DownloadUrl string `json:"browser_download_url"`
Url string `json:"url"`
}

func (gra *githubReleaseAsset) ParseDownloadLink(name string) (arch string, os string, err error) {
Expand Down

0 comments on commit 9eabf2b

Please sign in to comment.