-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(extensions): add github release downloader
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
Showing
3 changed files
with
78 additions
and
2 deletions.
There are no files selected for viewing
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
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 | ||
} |
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
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