Skip to content

Commit

Permalink
Refactor install dir func
Browse files Browse the repository at this point in the history
  • Loading branch information
paultyng committed Sep 3, 2020
1 parent 5d00c69 commit adf42b8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
27 changes: 15 additions & 12 deletions tfinstall/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,26 @@ import (
"golang.org/x/crypto/openpgp"
)

func ensureInstallDir(installDir string) (string, error) {
if installDir == "" {
return ioutil.TempDir("", "tfexec")
}

if _, err := os.Stat(installDir); err != nil {
return "", fmt.Errorf("could not access directory %s for installing Terraform: %w", installDir, err)
}

return installDir, nil
}

func downloadWithVerification(ctx context.Context, tfVersion string, installDir string) (string, error) {
osName := runtime.GOOS
archName := runtime.GOARCH

// setup: ensure we have a place to put our downloaded terraform binary
var tfDir string
var err error
if installDir == "" {
tfDir, err = ioutil.TempDir("", "tfexec")
if err != nil {
return "", fmt.Errorf("failed to create temp dir: %s", err)
}
} else {
if _, err := os.Stat(installDir); err != nil {
return "", fmt.Errorf("could not access directory %s for installing Terraform: %s", installDir, err)
}
tfDir = installDir
tfDir, err := ensureInstallDir(installDir)
if err != nil {
return "", err
}

httpGetter := &getter.HttpGetter{
Expand Down
6 changes: 6 additions & 0 deletions tfinstall/exact_version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@ import (

// downloads terraform 0.12.26 from the live releases site
func TestFindExactVersion(t *testing.T) {
ctx := context.Background()

tmpDir, err := ioutil.TempDir("", "tfinstall-test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)

<<<<<<< HEAD
tfpath, err := Find(context.Background(), ExactVersion("0.12.26", tmpDir))
=======
tfpath, err := Find(ctx, ExactVersion("0.12.26", tmpDir))
>>>>>>> 2a86fd3... Refactor install dir func
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit adf42b8

Please sign in to comment.