Skip to content

Commit

Permalink
remove duplicated code for downloading helm
Browse files Browse the repository at this point in the history
Signed-off-by: Amal Alkhamees <amalkms5@gmail.com>
  • Loading branch information
amalkh5 authored and alexellis committed Sep 6, 2020
1 parent 5ba2ecd commit 3c749d8
Showing 1 changed file with 20 additions and 24 deletions.
44 changes: 20 additions & 24 deletions pkg/helm/helm.go
Expand Up @@ -4,17 +4,15 @@
package helm

import (
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"path"
"strings"

"github.com/alexellis/arkade/pkg/archive"
"github.com/alexellis/arkade/pkg/env"
"github.com/alexellis/arkade/pkg/get"
execute "github.com/alexellis/go-execute/pkg/v1"
)

Expand Down Expand Up @@ -51,30 +49,28 @@ func GetHelmURL(arch, os, version string) string {
}

func DownloadHelm(userPath, clientArch, clientOS, subdir string) error {
helmURL := GetHelmURL(clientArch, clientOS, helmVersion)
fmt.Println(helmURL)
parsedURL, _ := url.Parse(helmURL)

res, err := http.DefaultClient.Get(parsedURL.String())
if err != nil {
return err
tools := get.MakeTools()
var tool *get.Tool
for _, t := range tools {
if t.Name == "helm" {
tool = &t
break
}
}

if res.StatusCode != http.StatusOK {
return fmt.Errorf("download of %s gave status: %d", parsedURL.String(), res.StatusCode)
if tool == nil {
return fmt.Errorf("unable to find tool definition")
}

dest := path.Join(path.Join(userPath, "bin"), subdir)
mkErr := os.MkdirAll(dest, 0700)
if mkErr != nil {
return mkErr
}
if _, err := os.Stat(fmt.Sprintf("%s", env.LocalBinary(tool.Name, ""))); errors.Is(err, os.ErrNotExist) {

outPath, finalName, err := get.Download(tool, clientArch, clientOS, tool.Version, get.DownloadArkadeDir)
if err != nil {
return err
}

defer res.Body.Close()
r := ioutil.NopCloser(res.Body)
untarErr := archive.Untar(r, dest)
if untarErr != nil {
return untarErr
fmt.Println("Downloaded to: ", outPath, finalName)
} else {
fmt.Printf("%s already exists, skipping download.\n", tool.Name)
}

return nil
Expand Down

0 comments on commit 3c749d8

Please sign in to comment.