Skip to content

Commit

Permalink
feat: add release asset downloading of tar/zip
Browse files Browse the repository at this point in the history
  • Loading branch information
dpastoor committed May 21, 2022
1 parent 4965c92 commit e37502d
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 9 deletions.
11 changes: 11 additions & 0 deletions cmd/helpers.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package cmd

import (
"encoding/json"
"errors"
"io"
"os"
"strings"

Expand Down Expand Up @@ -60,3 +62,12 @@ func setLogLevel(level string) {
log.Fatalf("Invalid log level: %s", logLevel)
}
}

func prettyEncode(data interface{}, out io.Writer) error {
enc := json.NewEncoder(out)
enc.SetIndent("", " ")
if err := enc.Encode(data); err != nil {
return err
}
return nil
}
73 changes: 64 additions & 9 deletions internal/gh/assets.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,42 @@
package gh

import (
"context"
"errors"
"fmt"
"io"
"net/http"
"os"
"strings"
"time"

"github.com/google/go-github/v44/github"
log "github.com/sirupsen/logrus"
)

type OsAssetSuffix int64
func getOsAssetSuffix(os string) osAssetSuffix {
switch os {
case "linux":
return linuxamd64
case "darwin":
return macos
case "windows":
return win
default:
return unknown
}
}

type osAssetSuffix int64

const (
unknown OsAssetSuffix = iota
unknown osAssetSuffix = iota
linuxamd64
macos
win
)

func (o OsAssetSuffix) String() string {
func (o osAssetSuffix) String() string {
switch o {
case linuxamd64:
return "linux-amd64.tar.gz"
Expand All @@ -28,17 +49,51 @@ func (o OsAssetSuffix) String() string {
}
}

func GetTarballForOs(assets []github.ReleaseAsset, suffix OsAssetSuffix) string {
// DownloadReleaseAsset downloads the release asset for a given platform to a temp
// file and returns the file handle.
// targetOs should be "windows", "darwin", "linux"
func DownloadReleaseAsset(client *github.Client, tag string, targetOs string) (*os.File, error) {
release, err := GetRelease(client, tag)
if err != nil {
return nil, err
}
asset := findAssetForOs(release.Assets, getOsAssetSuffix(targetOs))
if asset == nil {
return nil, errors.New("no release asset found")
}
// shouldn't need the redirect url given should follow redirects with the http client
log.Tracef("fetching information to download release asset from %s\n", asset.GetBrowserDownloadURL())
start := time.Now()
rc, _, err := client.Repositories.DownloadReleaseAsset(context.Background(), "quarto-dev", "quarto-cli", asset.GetID(), http.DefaultClient)
log.Tracef("done fetching release asset information in %s\n", time.Since(start))
if err != nil {
return nil, err
}
defer rc.Close()
tmpFile, err := os.CreateTemp("", fmt.Sprintf("*-%s", asset.GetName()))
if err != nil {
return tmpFile, err
}
log.Tracef("starting to copy release asset to %s\n", tmpFile.Name())
start = time.Now()
wb, err := io.Copy(tmpFile, rc)
log.Tracef("done copying release asset in %s\n", time.Since(start))
if err != nil {
return tmpFile, err
}
log.Debugf("downloaded %d bytes from release asset at url %s\n", wb, asset.GetBrowserDownloadURL())
return tmpFile, nil
}

func findAssetForOs(assets []*github.ReleaseAsset, suffix osAssetSuffix) *github.ReleaseAsset {
if suffix == unknown {
return ""
return nil
}
var dlUrl string
for _, asset := range assets {
name := asset.GetName()
if strings.HasSuffix(name, suffix.String()) {
dlUrl = asset.GetBrowserDownloadURL()
break
return asset
}
}
return dlUrl
return nil
}
14 changes: 14 additions & 0 deletions internal/gh/releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@ import (
log "github.com/sirupsen/logrus"
)

func GetRelease(client *github.Client, release string) (*github.RepositoryRelease, error) {
rel, resp, err := client.Repositories.GetReleaseByTag(
context.Background(),
"quarto-dev",
"quarto-cli",
release,
)
log.WithField("resp", resp).Trace("get-latest-release")
if err != nil {
return nil, err
}
return rel, err
}

func GetLatestRelease(client *github.Client) (*github.RepositoryRelease, error) {
rel, resp, err := client.Repositories.GetLatestRelease(
context.Background(),
Expand Down

0 comments on commit e37502d

Please sign in to comment.