Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement current subcommand #28

Merged
merged 4 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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)
ChrisTerBeke marked this conversation as resolved.
Show resolved Hide resolved
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))
}
4 changes: 2 additions & 2 deletions pkg/use/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func UseVersion(versionOrAlias string) {
helpers.ExitWithError("using", err)
}

useLocation := getUseLocation()
useLocation := GetUseLocation()

// inform the user that they need to update their PATH
path := os.Getenv("PATH")
Expand Down Expand Up @@ -96,7 +96,7 @@ func UseRequiredVersion() {
UseVersion(foundVersion)
}

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