Skip to content

Commit

Permalink
address comments and remove dependency on go-git
Browse files Browse the repository at this point in the history
Signed-off-by: liam.baker <[email protected]>
  • Loading branch information
liam.baker committed Jan 3, 2024
1 parent d327dfc commit 26a1b6e
Show file tree
Hide file tree
Showing 724 changed files with 66 additions and 125,180 deletions.
80 changes: 54 additions & 26 deletions cmd/system/aws_cli.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package system

import (
"bufio"
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
Expand All @@ -19,12 +22,21 @@ import (
"github.com/alexellis/arkade/pkg/env"
"github.com/alexellis/arkade/pkg/get"
execute "github.com/alexellis/go-execute/v2"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/storage/memory"
"github.com/spf13/cobra"
)

var runElevated bool

type ReferenceObject struct {
Type string `json:"tag,omitempty"`
}

type Reference struct {
Ref string `json:"ref,omitempty"`
Url string `json:"url,omitempty"`
Object ReferenceObject `json:"object,omitempty"`
}

func MakeInstallAWSCLI() *cobra.Command {
command := &cobra.Command{
Use: "aws-cli",
Expand All @@ -35,26 +47,23 @@ func MakeInstallAWSCLI() *cobra.Command {
SilenceUsage: true,
}

command.Flags().StringP("version", "v", "", "The version or leave blank to determine the latest available version")
command.Flags().StringP("version", "v", githubLatest, "The version or leave blank to determine the latest available version")
command.Flags().String("path", "/usr/local/bin", "Installation path, where a aws cli subfolder will be created")
command.Flags().Bool("progress", true, "Show download progress")
command.Flags().StringP("work-dir", "w", "", "Working directory that installer files should be copied to (current directory if not supplied)")
command.Flags().Bool("run-installer", false, "Whether or not arkade should run the downloaded installer")
command.Flags().Bool("run-installer", true, "Whether or not arkade should run the downloaded installer")

command.PreRunE = func(cmd *cobra.Command, args []string) error {
_, err := cmd.Flags().GetString("path")
fmt.Printf("Allow arkade to perform an elevated install (i.e. sudo)?\n\n")
fmt.Println("Enter a value: ")
reader := bufio.NewReader(os.Stdin)
input, err := reader.ReadString('\n')
if err != nil {
return err
}

_, err = cmd.Flags().GetBool("progress")
if err != nil {
return err
}

_, err = cmd.Flags().GetString("work-dir")
if err != nil {
return err
if strings.EqualFold(input, "yes") {
runElevated = true
}

return nil
Expand All @@ -80,7 +89,7 @@ func MakeInstallAWSCLI() *cobra.Command {
arch, _ = cmd.Flags().GetString("arch")
}

if version == "" {
if version == githubLatest {
v, err := getAWSCLIVersion("aws", "aws-cli")
if err != nil {
return err
Expand Down Expand Up @@ -150,8 +159,12 @@ func MakeInstallAWSCLI() *cobra.Command {
return err
}

extension := filepath.Ext(outPath)
if extension == ".zip" {
isArchive, err := awsCliTool.IsArchive(true)
if err != nil {
return err
}

if isArchive {
unpackPath := fmt.Sprintf("%s/awscli", workDir)
fmt.Printf("Unpacking AWS CLI to: %s\n", unpackPath)

Expand All @@ -166,7 +179,7 @@ func MakeInstallAWSCLI() *cobra.Command {
workDir = unpackPath
}

if runInstaller {
if runInstaller && runElevated {
if err := runBundledInstaller(osVer, workDir, filename, installPath); err != nil {
return err
}
Expand All @@ -185,27 +198,42 @@ func MakeInstallAWSCLI() *cobra.Command {
}

func getAWSCLIVersion(owner, repo string) (string, error) {
url := fmt.Sprintf("https://github.com/%s/%s", owner, repo)
url := fmt.Sprintf("https://api.github.com/repos/%s/%s/git/refs/tags", owner, repo)

client := http.Client{Timeout: time.Second * 10}
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}

remote := git.NewRemote(memory.NewStorage(), &config.RemoteConfig{
Name: "origin",
URLs: []string{url},
})
req, err := http.NewRequest(
http.MethodGet,
url,
nil,
)
if err != nil {
return "", err
}

var references []Reference
response, err := client.Do(req)
if err != nil {
return "", err
}

references, err := remote.List(&git.ListOptions{PeelingOption: git.AppendPeeled})
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
return "", err
}

if err := json.Unmarshal(body, &references); err != nil {
return "", err
}

tags := make([]*semver.Version, 0)
for _, reference := range references {
if reference.Name().IsTag() {
trimmed := strings.TrimPrefix(reference.Name().String(), "refs/tags/")
if reference.Object.Type == "tag" {
trimmed := strings.TrimPrefix(reference.Ref, "refs/tags/")
v, err := semver.NewVersion(trimmed)
if err != nil && errors.Is(err, semver.ErrInvalidSemVer) {
continue
Expand Down
22 changes: 2 additions & 20 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ require (
github.com/alexellis/go-execute/v2 v2.2.1
github.com/cheggaaa/pb/v3 v3.1.4
github.com/docker/go-units v0.5.0
github.com/go-git/go-git/v5 v5.9.0
github.com/google/go-containerregistry v0.16.1
github.com/morikuni/aec v1.0.0
github.com/olekukonko/tablewriter v0.0.5
Expand All @@ -22,44 +21,27 @@ require (
)

require (
dario.cat/mergo v1.0.0 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371 // indirect
github.com/VividCortex/ewma v1.2.0 // indirect
github.com/acomagu/bufpipe v1.0.4 // indirect
github.com/cloudflare/circl v1.3.3 // indirect
github.com/containerd/stargz-snapshotter/estargz v0.14.3 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/docker/cli v24.0.5+incompatible // indirect
github.com/docker/distribution v2.8.2+incompatible // indirect
github.com/docker/docker v24.0.5+incompatible // indirect
github.com/docker/docker-credential-helpers v0.8.0 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/fatih/color v1.15.0 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.5.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/klauspost/compress v1.17.0 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc5 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/skeema/knownhosts v1.2.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/vbatts/tar-split v0.11.5 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
golang.org/x/net v0.16.0 // indirect
golang.org/x/sync v0.4.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/tools v0.14.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
)
Loading

0 comments on commit 26a1b6e

Please sign in to comment.