Skip to content

Commit

Permalink
feat(extensions): add github release downloader
Browse files Browse the repository at this point in the history
This specifically adds a downloader for github releases it uses either the

SHUTTLE_EXTENSIONS_GITHUB_ACCESS_TOKEN or GITHUB_ACCESS_TOKEN for access to the releases

The downloader expects a full url from the registry as seen in a previous pr

Signed-off-by: Kasper J. Hermansen <[email protected]>
  • Loading branch information
kjuulh committed Jan 25, 2024
1 parent 428cffd commit f6eb664
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
68 changes: 68 additions & 0 deletions internal/extensions/downloader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package extensions

import (
"context"
"fmt"
"io"
"net/http"
"os"
"time"
)

type Downloader interface {
Download(ctx context.Context, dest string) error
}

func NewDownloader(downloadLink *registryExtensionDownloadLink) (Downloader, error) {
switch downloadLink.Provider {
case "github-release":
return newGitHubReleaseDownloader(downloadLink), nil
default:
return nil, fmt.Errorf("invalid provider type: %s", downloadLink.Provider)
}
}

type gitHubReleaseDownloader struct {
link *registryExtensionDownloadLink
}

func newGitHubReleaseDownloader(downloadLink *registryExtensionDownloadLink) Downloader {
return &gitHubReleaseDownloader{
link: downloadLink,
}
}

func (d *gitHubReleaseDownloader) Download(ctx context.Context, dest string) error {
client := http.DefaultClient

client.Timeout = time.Second * 60

req, err := http.NewRequestWithContext(ctx, http.MethodGet, d.link.Url, nil)
if err != nil {
return err
}

if accessToken := os.Getenv("SHUTTLE_EXTENSIONS_GITHUB_ACCESS_TOKEN"); accessToken != "" {
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", accessToken))
} else if accessToken := os.Getenv("GITHUB_ACCESS_TOKEN"); accessToken != "" {
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", accessToken))
}

resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()

extensionBinary, err := os.Create(dest)
if err != nil {
return err
}
defer extensionBinary.Close()

if _, err := io.Copy(extensionBinary, resp.Body); err != nil {
return err
}

return nil
}
11 changes: 9 additions & 2 deletions internal/extensions/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,16 @@ func (e *Extension) Ensure(ctx context.Context) error {
return fmt.Errorf("failed to find a valid extension matching your os and architecture")
}

//TODO: Initiate download
downloader, err := NewDownloader(downloadLink)
if err != nil {
return err
}

if err := downloader.Download(ctx, binaryPath); err != nil {
return err
}

panic("not implemented yet")
return nil
}

func (e *Extension) getExtensionBinaryName() string {
Expand Down
1 change: 1 addition & 0 deletions internal/extensions/registry_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type registryExtensionDownloadLink struct {
Os string `json:"os"`
Url string `json:"url"`
Checksum string `json:"checksum"`
Provider string `json:"provider"`
}

type registryExtension struct {
Expand Down

0 comments on commit f6eb664

Please sign in to comment.