Skip to content

Commit

Permalink
chore: prevent downloading already existing downloaded releases
Browse files Browse the repository at this point in the history
  • Loading branch information
bschaatsbergen committed Feb 11, 2024
1 parent 2f0f52a commit d41b1d7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
7 changes: 4 additions & 3 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var (
fmt.Println("See 'tfversion install -h' for help and examples")
os.Exit(1)
}
installA(args[0])
execCmd(args[0])
},
}
)
Expand All @@ -35,9 +35,10 @@ func init() {
rootCmd.AddCommand(installCmd)
}

func installA(version string) {
// Check if the Terraform release is already downloaded
func execCmd(version string) {
isAlreadyDownloaded := download.IsAlreadyDownloaded(version)
//TODO: Print a message to the user that the version is already downloaded

if !isAlreadyDownloaded {
// Download the Terraform release
zipFile, err := download.Download(version, runtime.GOOS, runtime.GOARCH)
Expand Down
15 changes: 8 additions & 7 deletions pkg/download/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ import (
"github.com/bschaatsbergen/tfversion/pkg/install"
)

// IsAlreadyDownloaded checks if the given Terraform version is already downloaded and unzipped.
func IsAlreadyDownloaded(version string) bool {
downloadLocation := getDownloadLocation()
filePath := filepath.Join(downloadLocation, version, install.TerraformBinaryName)
_, err := os.Stat(filePath)
return !os.IsNotExist(err)
}

func getDownloadLocation() string {
user, err := os.UserHomeDir()
if err != nil {
Expand All @@ -28,13 +36,6 @@ func ensureDownloadDirectoryExists(downloadLocation string) {
}
}

func IsAlreadyDownloaded(version string) bool {
downloadLocation := getDownloadLocation()
filePath := filepath.Join(downloadLocation, version, install.TerraformBinaryName)
_, err := os.Stat(filePath)
return !os.IsNotExist(err)
}

// Download downloads the Terraform release zip file for the given version, OS and architecture.
func Download(version, goos, goarch string) (string, error) {
downloadLocation := getDownloadLocation()
Expand Down

0 comments on commit d41b1d7

Please sign in to comment.