From 013ff3785fc53e949d7fd7d0406e8900594cb19f Mon Sep 17 00:00:00 2001 From: Denis Vaumoron Date: Sun, 8 Sep 2024 18:21:25 +0200 Subject: [PATCH 1/4] handle chdir flag with WorkPath Signed-off-by: Denis Vaumoron --- config/config.go | 3 +++ versionmanager/proxy/agnostic.go | 5 ++++- versionmanager/proxy/proxy.go | 18 +++++++++++++++++- .../semantic/parser/iac/iacparser.go | 2 +- versionmanager/semantic/walker.go | 13 +++++-------- 5 files changed, 30 insertions(+), 11 deletions(-) diff --git a/config/config.go b/config/config.go index 2857c4ee..7bd10bb4 100644 --- a/config/config.go +++ b/config/config.go @@ -144,6 +144,7 @@ type Config struct { Tofu RemoteConfig TofuKeyPath string UserPath string + WorkPath string } func DefaultConfig() (Config, error) { @@ -162,6 +163,7 @@ func DefaultConfig() (Config, error) { Tg: makeDefaultRemoteConfig(defaultTerragruntGithubURL, baseGithubURL), Tofu: makeDefaultRemoteConfig(DefaultTofuGithubURL, baseGithubURL), UserPath: userPath, + WorkPath: ".", }, nil } @@ -217,6 +219,7 @@ func InitConfigFromEnv() (Config, error) { Tofu: makeRemoteConfig(TofuRemoteURLEnvName, tofuListURLEnvName, tofuInstallModeEnvName, tofuListModeEnvName, DefaultTofuGithubURL, baseGithubURL), TofuKeyPath: os.Getenv(tofuOpenTofuPGPKeyEnvName), UserPath: userPath, + WorkPath: ".", }, nil } diff --git a/versionmanager/proxy/agnostic.go b/versionmanager/proxy/agnostic.go index 21fd4b54..91a94aa1 100644 --- a/versionmanager/proxy/agnostic.go +++ b/versionmanager/proxy/agnostic.go @@ -35,8 +35,10 @@ import ( // Always call os.Exit. func ExecAgnostic(conf *config.Config, hclParser *hclparse.Parser, cmdArgs []string) { conf.InitDisplayer(true) - ctx := context.Background() manager := builder.BuildTofuManager(conf, hclParser) + + updateWorkPath(conf, cmdArgs) + detectedVersion, err := manager.ResolveWithVersionFiles() if err != nil { fmt.Println("Failed to resolve a version allowing to call tofu :", err) //nolint @@ -65,6 +67,7 @@ func ExecAgnostic(conf *config.Config, hclParser *hclparse.Parser, cmdArgs []str os.Exit(1) } + ctx := context.Background() detectedVersion, err = manager.Evaluate(ctx, detectedVersion, true) if err != nil { fmt.Println("Failed to evaluate the requested version in a specific version allowing to call", execName, ":", err) //nolint diff --git a/versionmanager/proxy/proxy.go b/versionmanager/proxy/proxy.go index 8657eed9..cde35918 100644 --- a/versionmanager/proxy/proxy.go +++ b/versionmanager/proxy/proxy.go @@ -25,6 +25,7 @@ import ( "os" "os/exec" "path/filepath" + "strings" "github.com/hashicorp/hcl/v2/hclparse" @@ -35,13 +36,18 @@ import ( "github.com/tofuutils/tenv/v3/versionmanager/lastuse" ) +const chdirFlagPrefix = "-chdir=" + var errDelimiter = errors.New("key and value should not contains delimiter") // Always call os.Exit. func Exec(conf *config.Config, builderFunc builder.BuilderFunc, hclParser *hclparse.Parser, execName string, cmdArgs []string) { conf.InitDisplayer(true) - ctx := context.Background() versionManager := builderFunc(conf, hclParser) + + updateWorkPath(conf, cmdArgs) + + ctx := context.Background() detectedVersion, err := versionManager.Detect(ctx, true) if err != nil { fmt.Println("Failed to detect a version allowing to call", execName, ":", err) //nolint @@ -65,3 +71,13 @@ func ExecPath(installPath string, version string, execName string, displayer log return filepath.Join(versionPath, execName) } + +func updateWorkPath(conf *config.Config, cmdArgs []string) { + for _, arg := range cmdArgs { + if cutted, ok := strings.CutPrefix(arg, chdirFlagPrefix); ok { + conf.WorkPath = cutted + + return + } + } +} diff --git a/versionmanager/semantic/parser/iac/iacparser.go b/versionmanager/semantic/parser/iac/iacparser.go index 0b396849..43ab3004 100644 --- a/versionmanager/semantic/parser/iac/iacparser.go +++ b/versionmanager/semantic/parser/iac/iacparser.go @@ -65,7 +65,7 @@ func GatherRequiredVersion(conf *config.Config, exts []ExtDescription) ([]string }() } - entries, err := os.ReadDir(".") + entries, err := os.ReadDir(conf.WorkPath) if err != nil { return nil, err } diff --git a/versionmanager/semantic/walker.go b/versionmanager/semantic/walker.go index c382f438..0362b104 100644 --- a/versionmanager/semantic/walker.go +++ b/versionmanager/semantic/walker.go @@ -19,7 +19,6 @@ package semantic import ( - "os" "path/filepath" "github.com/tofuutils/tenv/v3/config" @@ -27,17 +26,15 @@ import ( ) func RetrieveVersion(versionFiles []types.VersionFile, conf *config.Config) (string, error) { - for _, versionFile := range versionFiles { - if version, err := versionFile.Parser(versionFile.Name, conf); err != nil || version != "" { - return version, err - } - } - - previousPath, err := os.Getwd() + previousPath, err := filepath.Abs(conf.WorkPath) if err != nil { return "", err } + if version, err := retrieveVersionFromDir(versionFiles, previousPath, conf); err != nil || version != "" { + return version, err + } + userPathDone := false for currentPath := filepath.Dir(previousPath); currentPath != previousPath; previousPath, currentPath = currentPath, filepath.Dir(currentPath) { if version, err := retrieveVersionFromDir(versionFiles, currentPath, conf); err != nil || version != "" { From c3fcea4c28fc650f50e7ca46ab3749ff3a589044 Mon Sep 17 00:00:00 2001 From: Denis Vaumoron Date: Sun, 8 Sep 2024 18:30:03 +0200 Subject: [PATCH 2/4] fix spelling Signed-off-by: Denis Vaumoron --- versionmanager/proxy/proxy.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/versionmanager/proxy/proxy.go b/versionmanager/proxy/proxy.go index cde35918..49cff590 100644 --- a/versionmanager/proxy/proxy.go +++ b/versionmanager/proxy/proxy.go @@ -74,8 +74,8 @@ func ExecPath(installPath string, version string, execName string, displayer log func updateWorkPath(conf *config.Config, cmdArgs []string) { for _, arg := range cmdArgs { - if cutted, ok := strings.CutPrefix(arg, chdirFlagPrefix); ok { - conf.WorkPath = cutted + if chdirPath, ok := strings.CutPrefix(arg, chdirFlagPrefix); ok { + conf.WorkPath = chdirPath return } From 5ef60bce2120e85acb26ed27dcf5d28a79537998 Mon Sep 17 00:00:00 2001 From: Denis Vaumoron Date: Sun, 8 Sep 2024 18:36:50 +0200 Subject: [PATCH 3/4] update go version (fix linter error) Signed-off-by: Denis Vaumoron --- TENV_AS_LIB.md | 2 +- go.mod | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/TENV_AS_LIB.md b/TENV_AS_LIB.md index 6f0fe6a1..15b16791 100644 --- a/TENV_AS_LIB.md +++ b/TENV_AS_LIB.md @@ -4,7 +4,7 @@ ### Prerequisites -**tenv** requires [Go](https://go.dev) version [1.21](https://go.dev/doc/devel/release#go1.21.0) or above. +**tenv** requires [Go](https://go.dev) version [1.23](https://go.dev/doc/devel/release#go1.23.0) or above. ### Getting tenv module diff --git a/go.mod b/go.mod index 1578779c..8b44b9cc 100644 --- a/go.mod +++ b/go.mod @@ -2,8 +2,6 @@ module github.com/tofuutils/tenv/v3 go 1.23 -toolchain go1.23.1 - require ( github.com/BurntSushi/toml v1.4.0 github.com/ProtonMail/gopenpgp/v2 v2.7.5 From efb83d1ac48e81b442a91ea46b9f5d061cda74d6 Mon Sep 17 00:00:00 2001 From: Denis Vaumoron Date: Sun, 8 Sep 2024 18:38:53 +0200 Subject: [PATCH 4/4] fix lint Signed-off-by: Denis Vaumoron --- versionmanager/proxy/proxy.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/versionmanager/proxy/proxy.go b/versionmanager/proxy/proxy.go index 49cff590..e5d16ffa 100644 --- a/versionmanager/proxy/proxy.go +++ b/versionmanager/proxy/proxy.go @@ -74,7 +74,7 @@ func ExecPath(installPath string, version string, execName string, displayer log func updateWorkPath(conf *config.Config, cmdArgs []string) { for _, arg := range cmdArgs { - if chdirPath, ok := strings.CutPrefix(arg, chdirFlagPrefix); ok { + if chdirPath, ok := strings.CutPrefix(arg, chdirFlagPrefix); ok { //nolint conf.WorkPath = chdirPath return