-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bd5fcb6
commit d77c7da
Showing
4 changed files
with
38 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |