Skip to content

Commit

Permalink
Restructure in paths
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisTerBeke committed May 24, 2024
1 parent bd5fcb6 commit d77c7da
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 31 deletions.
2 changes: 1 addition & 1 deletion pkg/current/current.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ import (

// CheckCurrentVersion prints the current active version of Terraform.
func CheckCurrentVersion() {
version := paths.GetUseVersion()
version := paths.GetActiveVersion()
fmt.Printf("Current active Terraform version: %s\n", helpers.ColoredVersion(version))
}
14 changes: 0 additions & 14 deletions pkg/paths/installed.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,6 @@ func GetInstallLocation() string {
return downloadLocation
}

// GetInstalledVersions returns a list of all installed Terraform versions.
func GetInstalledVersions() []string {
installLocation := GetInstallLocation()
installedVersions, err := os.ReadDir(installLocation)
if err != nil {
helpers.ExitWithError("listing versions directory", err)
}
var versionNames []string
for _, v := range installedVersions {
versionNames = append(versionNames, v.Name())
}
return versionNames
}

// IsInstalled checks if the given Terraform version is already installed.
func IsInstalled(version string) bool {
binaryPath := GetBinaryLocation(version)
Expand Down
16 changes: 0 additions & 16 deletions pkg/paths/use.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package paths

import (
"os"
"path/filepath"

"github.com/tfversion/tfversion/pkg/helpers"
Expand All @@ -21,18 +20,3 @@ func GetUseLocation() string {
func GetActiveBinaryLocation() string {
return filepath.Join(GetUseLocation(), TerraformBinaryName)
}

// GetUseVersion returns the currently active Terraform version.
func GetUseVersion() string {
binaryPath := GetActiveBinaryLocation()
_, err := os.Lstat(binaryPath)
if err != nil {
helpers.ExitWithError("no current terraform version found", err)
}
realPath, err := filepath.EvalSymlinks(binaryPath)
if err != nil {
helpers.ExitWithError("resolving symlink", err)
}
_, version := filepath.Split(filepath.Dir(realPath))
return version
}
37 changes: 37 additions & 0 deletions pkg/paths/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package paths

import (
"os"
"path/filepath"

"github.com/tfversion/tfversion/pkg/helpers"
)

// GetInstalledVersions returns a list of all installed Terraform versions.
func GetInstalledVersions() []string {
installLocation := GetInstallLocation()
installedVersions, err := os.ReadDir(installLocation)
if err != nil {
helpers.ExitWithError("listing versions directory", err)
}
var versionNames []string
for _, v := range installedVersions {
versionNames = append(versionNames, v.Name())
}
return versionNames
}

// GetActiveVersion returns the currently active Terraform version.
func GetActiveVersion() string {
binaryPath := GetActiveBinaryLocation()
_, err := os.Lstat(binaryPath)
if err != nil {
helpers.ExitWithError("no current terraform version found", err)
}
realPath, err := filepath.EvalSymlinks(binaryPath)
if err != nil {
helpers.ExitWithError("resolving symlink", err)
}
_, version := filepath.Split(filepath.Dir(realPath))
return version
}

0 comments on commit d77c7da

Please sign in to comment.