Skip to content

Commit

Permalink
Merge branch 'main' into feature/auto-install-missing-version
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisTerBeke committed Mar 8, 2024
2 parents 61f55fb + 3df93ca commit d9ced68
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
5 changes: 5 additions & 0 deletions README.md
Expand Up @@ -122,6 +122,11 @@ tfversion list --pre-release
tfversion uninstall 1.7.4
```

### Display the currently activated version of Terraform
```sh
tfversion current
```

## Contributing

Contributions are highly appreciated and always welcome.
Expand Down
26 changes: 26 additions & 0 deletions cmd/current.go
@@ -0,0 +1,26 @@
package cmd

import (
"github.com/spf13/cobra"
"github.com/tfversion/tfversion/pkg/current"
)

const (
currentExample = "# Print the current active version of Terraform\n" +
"tfversion current"
)

var (
currentCmd = &cobra.Command{
Use: "current",
Short: "Print the current active version of Terraform",
Example: currentExample,
Run: func(cmd *cobra.Command, args []string) {
current.CheckCurrentVersion()
},
}
)

func init() {
rootCmd.AddCommand(currentCmd)
}
29 changes: 29 additions & 0 deletions pkg/current/current.go
@@ -0,0 +1,29 @@
package current

import (
"fmt"
"os"
"path/filepath"

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

// CheckCurrentVersion prints the current active version of Terraform.
func CheckCurrentVersion() {

symlinkPath := filepath.Join(use.GetUseLocation(), download.TerraformBinaryName)
_, err := os.Lstat(symlinkPath)
if err != nil {
helpers.ExitWithError("no current terraform version found", err)
}

realPath, err := filepath.EvalSymlinks(symlinkPath)
if err != nil {
helpers.ExitWithError("resolving symlink", err)
}
_, currentVersion := filepath.Split(filepath.Dir(realPath))

fmt.Printf("Current active Terraform version: %s\n", helpers.ColoredVersion(currentVersion))
}
2 changes: 1 addition & 1 deletion pkg/use/use.go
Expand Up @@ -99,7 +99,7 @@ func UseRequiredVersion(autoInstall bool) {
UseVersion(foundVersion, autoInstall)
}

func getUseLocation() string {
func GetUseLocation() string {
user, err := os.UserHomeDir()
if err != nil {
helpers.ExitWithError("user home directory", err)
Expand Down

0 comments on commit d9ced68

Please sign in to comment.