Skip to content

Commit

Permalink
Core tool downloader fixes (#1028)
Browse files Browse the repository at this point in the history
  • Loading branch information
ofalvai authored Dec 5, 2024
1 parent df96588 commit 91e698e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 24 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/bitrise-io/stepman v0.0.0-20240828074035-6ae1a5f5efde
github.com/go-git/go-git/v5 v5.12.0
github.com/gofrs/uuid v4.3.1+incompatible
github.com/hashicorp/go-retryablehttp v0.7.7
github.com/hashicorp/go-version v1.4.0
github.com/heimdalr/dag v1.4.0
github.com/ryanuber/go-glob v1.0.0
Expand All @@ -39,7 +40,6 @@ require (
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-retryablehttp v0.7.7 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
Expand Down
31 changes: 8 additions & 23 deletions tools/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package tools
import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"runtime"
Expand All @@ -17,10 +16,10 @@ import (
"github.com/bitrise-io/go-utils/pathutil"
stepman "github.com/bitrise-io/stepman/cli"
stepmanModels "github.com/bitrise-io/stepman/models"
"github.com/hashicorp/go-retryablehttp"
"golang.org/x/sys/unix"
)

// UnameGOOS ...
func UnameGOOS() (string, error) {
switch runtime.GOOS {
case "darwin":
Expand All @@ -31,7 +30,6 @@ func UnameGOOS() (string, error) {
return "", fmt.Errorf("Unsupported platform (%s)", runtime.GOOS)
}

// UnameGOARCH ...
func UnameGOARCH() (string, error) {
switch runtime.GOARCH {
case "amd64":
Expand All @@ -42,7 +40,6 @@ func UnameGOARCH() (string, error) {
return "", fmt.Errorf("Unsupported architecture (%s)", runtime.GOARCH)
}

// InstallToolFromGitHub ...
func InstallToolFromGitHub(toolname, githubUser, toolVersion string) error {
unameGOOS, err := UnameGOOS()
if err != nil {
Expand All @@ -57,7 +54,6 @@ func InstallToolFromGitHub(toolname, githubUser, toolVersion string) error {
return InstallFromURL(toolname, downloadURL)
}

// DownloadFile ...
func DownloadFile(downloadURL, targetDirPath string) error {
outFile, err := os.Create(targetDirPath)
defer func() {
Expand All @@ -66,19 +62,22 @@ func DownloadFile(downloadURL, targetDirPath string) error {
}
}()
if err != nil {
return fmt.Errorf("failed to create (%s), error: %s", targetDirPath, err)
return fmt.Errorf("create %s: %s", targetDirPath, err)
}

resp, err := http.Get(downloadURL)
resp, err := retryablehttp.Get(downloadURL)
if err != nil {
return fmt.Errorf("failed to download from (%s), error: %s", downloadURL, err)
return fmt.Errorf("download %s: %s", downloadURL, err)
}
if resp.StatusCode >= 400 {
return fmt.Errorf("download %s: %s", downloadURL, resp.Status)
}

defer func() {
if err := resp.Body.Close(); err != nil {
log.Warnf("failed to close (%s) body", downloadURL)
}
}()

_, err = io.Copy(outFile, resp.Body)
if err != nil {
return fmt.Errorf("failed to download from (%s), error: %s", downloadURL, err)
Expand All @@ -87,7 +86,6 @@ func DownloadFile(downloadURL, targetDirPath string) error {
return nil
}

// InstallFromURL ...
func InstallFromURL(toolBinName, downloadURL string) error {
if len(toolBinName) < 1 {
return fmt.Errorf("no Tool (bin) Name provided! URL was: %s", downloadURL)
Expand Down Expand Up @@ -128,13 +126,11 @@ func InstallFromURL(toolBinName, downloadURL string) error {
// ------------------
// --- Stepman

// StepmanSetup ...
func StepmanSetup(collection string) error {
log := log.NewLogger(log.GetGlobalLoggerOpts())
return stepman.Setup(collection, "", log)
}

// StepmanStepInfo ...
func StepmanStepInfo(collection, stepID, stepVersion string) (stepmanModels.StepInfoModel, error) {
log := log.NewLogger(log.GetGlobalLoggerOpts())
return stepman.QueryStepInfo(collection, stepID, stepVersion, log)
Expand All @@ -143,31 +139,26 @@ func StepmanStepInfo(collection, stepID, stepVersion string) (stepmanModels.Step
//
// Share

// StepmanShare ...
func StepmanShare() error {
args := []string{"share", "--toolmode"}
return command.RunCommand("stepman", args...)
}

// StepmanShareAudit ...
func StepmanShareAudit() error {
args := []string{"share", "audit", "--toolmode"}
return command.RunCommand("stepman", args...)
}

// StepmanShareCreate ...
func StepmanShareCreate(tag, git, stepID string) error {
args := []string{"share", "create", "--tag", tag, "--git", git, "--stepid", stepID, "--toolmode"}
return command.RunCommand("stepman", args...)
}

// StepmanShareFinish ...
func StepmanShareFinish() error {
args := []string{"share", "finish", "--toolmode"}
return command.RunCommand("stepman", args...)
}

// StepmanShareStart ...
func StepmanShareStart(collection string) error {
args := []string{"share", "start", "--collection", collection, "--toolmode"}
return command.RunCommand("stepman", args...)
Expand All @@ -176,17 +167,14 @@ func StepmanShareStart(collection string) error {
// ------------------
// --- Envman

// EnvmanInit ...
func EnvmanInit(envStorePth string, clear bool) error {
return envman.InitEnvStore(envStorePth, clear)
}

// EnvmanAdd ...
func EnvmanAdd(envStorePth, key, value string, expand, skipIfEmpty, sensitive bool) error {
return envman.AddEnv(envStorePth, key, value, expand, false, skipIfEmpty, sensitive)
}

// EnvmanAddEnvs ...
func EnvmanAddEnvs(envstorePth string, envsList []envmanModels.EnvironmentItemModel) error {
for _, env := range envsList {
key, value, err := env.GetKeyValuePair()
Expand Down Expand Up @@ -221,12 +209,10 @@ func EnvmanAddEnvs(envstorePth string, envsList []envmanModels.EnvironmentItemMo
return nil
}

// EnvmanReadEnvList ...
func EnvmanReadEnvList(envStorePth string) (envmanModels.EnvsJSONListModel, error) {
return envman.ReadEnvsJSONList(envStorePth, true, false, &envmanEnv.DefaultEnvironmentSource{})
}

// EnvmanClear ...
func EnvmanClear(envStorePth string) error {
return envman.ClearEnvs(envStorePth)
}
Expand All @@ -253,7 +239,6 @@ func GetSecretKeysAndValues(secrets []envmanModels.EnvironmentItemModel) ([]stri
return secretKeys, secretValues
}

// MoveFile ...
func MoveFile(oldpath, newpath string) error {
err := os.Rename(oldpath, newpath)
if err == nil {
Expand Down

0 comments on commit 91e698e

Please sign in to comment.