Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: download releases from S3 repository #13

Merged
merged 9 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions cmd/install.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

package cmd

import (
Expand All @@ -16,8 +15,8 @@ import (
var installCmd = &cobra.Command{
Use: "install [version]",
Short: "install an Armory CLI version, if version is omitted latest will be used and it will be linked to as default",
Run: execInstallCmd,
Args: cobra.MaximumNArgs(1),
Run: execInstallCmd,
Args: cobra.MaximumNArgs(1),
}

func execInstallCmd(cmd *cobra.Command, args []string) {
Expand All @@ -26,7 +25,11 @@ func execInstallCmd(cmd *cobra.Command, args []string) {
useVersionAsDefault, _ := cmd.Flags().GetBool("default")
var version string
if len(args) == 0 {
version = utils.GetLatestVersion()
var err error
version, err = utils.GetLatestVersion()
if err != nil {
log.Fatalf(err.Error())
}
qrnik marked this conversation as resolved.
Show resolved Hide resolved
useVersionAsDefault = true
} else {
version = args[0]
Expand All @@ -46,7 +49,7 @@ func execInstallCmd(cmd *cobra.Command, args []string) {
}

path := filepath.Join(dir, "armory")
err = downloadRelease(path, utils.GetBinDownloadUrlForVersion(version))
err = downloadRelease(path, utils.GetBinDownloadUrlForVersion(version, goos, goarch))
if err != nil {
log.Fatalf("Failed to download release, err: %s", err.Error())
}
Expand Down Expand Up @@ -84,4 +87,4 @@ func downloadRelease(filepath string, url string) error {
// Write the body to file
_, err = io.Copy(out, resp.Body)
return err
}
}
11 changes: 7 additions & 4 deletions cmd/listall.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ import (
var listallCmd = &cobra.Command{
Use: "listall",
Short: "list available versions",
Run: execListAllCmd,
Run: execListAllCmd,
}

func execListAllCmd(cmd *cobra.Command, args []string) {
allReleases := utils.GetAllReleases()
for _, release := range allReleases {
log.Infof(*release.TagName)
versions, err := utils.GetAllVersions()
if err != nil {
log.Fatalf(err.Error())
}
for _, version := range versions {
log.Infof(version)
}
}

Expand Down
7 changes: 4 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ var RootCmd = &cobra.Command{
Short: "Armory Version Manager",
}

func Execute() {
func Execute() {
//goland:noinspection GoBoolExpressions because incorrectly detects as "always false"
if runtime.GOOS == "windows" {
log.Fatalf("avm only supports OS X and GNU+Linux")
}
Expand Down Expand Up @@ -52,7 +53,7 @@ func configureLogging(cmd *cobra.Command, args []string) error {
}
log.SetLevel(lvl)
log.SetFormatter(&easy.Formatter{
LogFormat: "%msg%\n",
LogFormat: "%msg%\n",
})
return nil
}
}
29 changes: 16 additions & 13 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
module github.com/armory/avm

go 1.17
go 1.20

require (
github.com/google/go-github/v39 v39.1.0
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.2.1
github.com/Masterminds/semver/v3 v3.2.1
github.com/hashicorp/go-retryablehttp v0.7.4
github.com/jarcoal/httpmock v1.3.1
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.7.0
github.com/stretchr/testify v1.8.4
github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816
)

require (
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.0.1 // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816 // indirect
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
604 changes: 28 additions & 576 deletions go.sum

Large diffs are not rendered by default.

68 changes: 0 additions & 68 deletions pkg/utils/github.go

This file was deleted.

12 changes: 12 additions & 0 deletions pkg/utils/resources/all-versions-1.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<ListBucketResult
xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<IsTruncated>true</IsTruncated>
<NextContinuationToken>l0g4nr0y</NextContinuationToken>
<Contents>
<Key>cli/v1.10.0/armory-darwin-amd64</Key>
</Contents>
<Contents>
<Key>cli/v1.1.11/armory-darwin-amd64</Key>
</Contents>
</ListBucketResult>
9 changes: 9 additions & 0 deletions pkg/utils/resources/all-versions-2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<ListBucketResult
xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<IsTruncated>true</IsTruncated>
<NextContinuationToken>k3nd411r0y</NextContinuationToken>
<Contents>
<Key>cli/v2.0.0/armory-darwin-amd64</Key>
</Contents>
</ListBucketResult>
8 changes: 8 additions & 0 deletions pkg/utils/resources/all-versions-3.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<ListBucketResult
xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<IsTruncated>false</IsTruncated>
<Contents>
<Key>cli/v3.0.0/armory-darwin-amd64</Key>
</Contents>
</ListBucketResult>
14 changes: 14 additions & 0 deletions pkg/utils/resources/get-latest-version.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<ListBucketResult
xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<IsTruncated>false</IsTruncated>
<Contents>
<Key>cli/v1.10.0/armory-darwin-amd64</Key>
</Contents>
<Contents>
<Key>cli/v1.1.11/armory-darwin-amd64</Key>
</Contents>
<Contents>
<Key>cli/v2.0.0/armory-darwin-amd64</Key>
</Contents>
</ListBucketResult>
96 changes: 96 additions & 0 deletions pkg/utils/versions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package utils

import (
"encoding/xml"
"fmt"
"github.com/Masterminds/semver/v3"
"github.com/hashicorp/go-retryablehttp"
"net/url"
"sort"
"strings"
)

const (
repositoryUrl = "https://armory-cli-releases.s3.amazonaws.com/"

cliPrefix = "cli"
)

var client = retryablehttp.NewClient()

// GetAllVersions returns versions available in S3 repository, sorted as SemVers in descending order.
func GetAllVersions() ([]string, error) {
var versions []*semver.Version
more := true
params := url.Values{}
params.Set("list-type", "2")
params.Set("prefix", cliPrefix)
for more {
u := buildUrl(params)
httpRes, err := client.Get(u)
if err != nil {
return []string{}, err
qrnik marked this conversation as resolved.
Show resolved Hide resolved
}

var res listBucketResult
if err = xml.NewDecoder(httpRes.Body).Decode(&res); err != nil {
qrnik marked this conversation as resolved.
Show resolved Hide resolved
return []string{}, err
}
more = res.IsTruncated
if res.NextContinuationToken != "" {
params.Set("continuation-token", res.NextContinuationToken)
}

for _, o := range res.Contents {
v, err := o.version()
if err != nil {
return []string{}, err
}
versions = append(versions, v)
}
}

sort.Sort(sort.Reverse(semver.Collection(versions)))

var versionStrings []string
for _, v := range versions {
versionStrings = append(versionStrings, v.Original())
}
return versionStrings, nil
}

func GetLatestVersion() (string, error) {
all, err := GetAllVersions()
if err != nil {
return "", err
}
return all[0], err
}

func GetBinDownloadUrlForVersion(version, goos, goarch string) string {
return fmt.Sprintf("%s%s/%s/armory-%s-%s", repositoryUrl, cliPrefix, version, goos, goarch)
}

func buildUrl(params url.Values) string {
u, err := url.Parse(repositoryUrl)
if err != nil {
panic(err)
qrnik marked this conversation as resolved.
Show resolved Hide resolved
}
u.RawQuery = params.Encode()
return u.String()
}

type listBucketResult struct {
IsTruncated bool
Contents []object
NextContinuationToken string
}

type object struct {
Key string // asset key has format "cli/{version}/armory-{os}-{arch}"
}

func (o object) version() (*semver.Version, error) {
v := strings.Split(o.Key, "/")[1]
return semver.NewVersion(v)
}
Loading
Loading