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

Proxies handle chdir flag #247

Merged
merged 4 commits into from
Sep 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
2 changes: 1 addition & 1 deletion TENV_AS_LIB.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ type Config struct {
Tofu RemoteConfig
TofuKeyPath string
UserPath string
WorkPath string
}

func DefaultConfig() (Config, error) {
Expand All @@ -162,6 +163,7 @@ func DefaultConfig() (Config, error) {
Tg: makeDefaultRemoteConfig(defaultTerragruntGithubURL, baseGithubURL),
Tofu: makeDefaultRemoteConfig(DefaultTofuGithubURL, baseGithubURL),
UserPath: userPath,
WorkPath: ".",
}, nil
}

Expand Down Expand Up @@ -217,6 +219,7 @@ func InitConfigFromEnv() (Config, error) {
Tofu: makeRemoteConfig(TofuRemoteURLEnvName, tofuListURLEnvName, tofuInstallModeEnvName, tofuListModeEnvName, DefaultTofuGithubURL, baseGithubURL),
TofuKeyPath: os.Getenv(tofuOpenTofuPGPKeyEnvName),
UserPath: userPath,
WorkPath: ".",
}, nil
}

Expand Down
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion versionmanager/proxy/agnostic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
18 changes: 17 additions & 1 deletion versionmanager/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/hashicorp/hcl/v2/hclparse"

Expand All @@ -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
Expand All @@ -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 chdirPath, ok := strings.CutPrefix(arg, chdirFlagPrefix); ok { //nolint
conf.WorkPath = chdirPath

return
}
}
}
2 changes: 1 addition & 1 deletion versionmanager/semantic/parser/iac/iacparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
13 changes: 5 additions & 8 deletions versionmanager/semantic/walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,22 @@
package semantic

import (
"os"
"path/filepath"

"github.com/tofuutils/tenv/v3/config"
"github.com/tofuutils/tenv/v3/versionmanager/semantic/types"
)

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 != "" {
Expand Down
Loading