Skip to content

Commit

Permalink
add update-path command #98
Browse files Browse the repository at this point in the history
Signed-off-by: Denis Vaumoron <dvaumoron@gmail.com>
  • Loading branch information
dvaumoron committed Apr 8, 2024
1 parent 1f8d2a9 commit 24d6d33
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 13 deletions.
60 changes: 57 additions & 3 deletions cmd/tenv/tenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ package main
import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/spf13/cobra"
"github.com/tofuutils/tenv/config"
Expand All @@ -30,12 +32,16 @@ import (
)

const (
versionName = "version"
rootVersionHelp = "Display tenv current version."
updatePathHelp = "Display PATH updated with tenv dir location first."

helpPrefix = "Subcommand to manage several versions of "
tfHelp = helpPrefix + "Terraform (https://www.terraform.io)."
tgHelp = helpPrefix + "Terragrunt (https://terragrunt.gruntwork.io/)."
tgHelp = helpPrefix + "Terragrunt (https://terragrunt.gruntwork.io)."
tofuHelp = helpPrefix + "OpenTofu (https://opentofu.org)."

pathEnvName = "PATH"
)

// can be overridden with ldflags.
Expand Down Expand Up @@ -75,6 +81,8 @@ func initRootCmd(conf *config.Config) *cobra.Command {
flags.BoolVarP(&conf.DisplayVerbose, "verbose", "v", false, "verbose output (and set log level to Trace)")

rootCmd.AddCommand(newVersionCmd())
rootCmd.AddCommand(newUpdatePathCmd())

tofuParams := subCmdParams{
deprecated: true, // direct use should display a deprecation message
needToken: true, remoteEnvName: config.TofuRemoteURLEnvName,
Expand Down Expand Up @@ -130,12 +138,58 @@ func initRootCmd(conf *config.Config) *cobra.Command {

func newVersionCmd() *cobra.Command {
return &cobra.Command{
Use: "version",
Use: versionName,
Short: rootVersionHelp,
Long: rootVersionHelp,
Args: cobra.NoArgs,
Run: func(_ *cobra.Command, _ []string) {
fmt.Println(config.TenvName, "version", version) //nolint
fmt.Println(config.TenvName, versionName, version) //nolint
},
}
}

func newUpdatePathCmd() *cobra.Command {
return &cobra.Command{
Use: "update-path",
Short: updatePathHelp,
Long: updatePathHelp,
Args: cobra.NoArgs,
RunE: func(_ *cobra.Command, _ []string) error {
execPath, err := os.Executable()
if err != nil {
return nil
}

pathEnv := os.Getenv(pathEnvName)
execDirPath := filepath.Dir(execPath)
gha, err := config.GetenvBool(false, config.GithubActionsEnvName)
if err != nil {
return err
}

if gha {
pathfilePath := os.Getenv("GITHUB_PATH")
if pathfilePath != "" {
pathfile, err := os.OpenFile(pathfilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer pathfile.Close()

_, err = pathfile.Write([]byte(execDirPath))
if err != nil {
return err
}
}
}

var pathBuilder strings.Builder
pathBuilder.WriteString(execDirPath)
pathBuilder.WriteRune(os.PathListSeparator)
pathBuilder.WriteString(pathEnv)
fmt.Println(pathBuilder.String()) //nolint

return nil
},
}
}
Expand Down
10 changes: 10 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const (
TerragruntName = "terragrunt"
TofuName = "tofu"

GithubActionsEnvName = "GITHUB_ACTIONS"

archEnvName = "ARCH"
autoInstallEnvName = "AUTO_INSTALL"
defaultConstraint = "DEFAULT_CONSTRAINT"
Expand Down Expand Up @@ -232,6 +234,14 @@ func (conf *Config) InitRemoteConf() error {
return nil
}

func GetenvBool(defaultValue bool, key string) (bool, error) {
if valueStr := os.Getenv(key); valueStr != "" {
return strconv.ParseBool(valueStr)
}

return defaultValue, nil
}

func getenvBoolFallback(defaultValue bool, keys ...string) (bool, error) {
if valueStr := getenvFallback(keys...); valueStr != "" {
return strconv.ParseBool(valueStr)
Expand Down
11 changes: 1 addition & 10 deletions versionmanager/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,8 @@ func exitWithErrorMsg(execName string, err error, pExitCode *int) {
*pExitCode = 1
}

func getenvBool(defaultValue bool, key string) (bool, error) {
if valueStr := os.Getenv(key); valueStr != "" {
return strconv.ParseBool(valueStr)
}

return defaultValue, nil
}

func initIO(cmd *exec.Cmd, execName string, pExitCode *int) (func(int), error) {
gha, err := getenvBool(false, "GITHUB_ACTIONS")
gha, err := config.GetenvBool(false, config.GithubActionsEnvName)
if err != nil {
return nil, err
}
Expand All @@ -127,7 +119,6 @@ func initIO(cmd *exec.Cmd, execName string, pExitCode *int) (func(int), error) {
}

outputPath := os.Getenv("GITHUB_OUTPUT")

outputFile, err := os.OpenFile(outputPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return nil, err
Expand Down

0 comments on commit 24d6d33

Please sign in to comment.