Skip to content

Commit

Permalink
feat(golang-rewrite): correct PATH code for env and exec commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Stratus3D committed Nov 27, 2024
1 parent a09d424 commit 007a710
Showing 1 changed file with 18 additions and 49 deletions.
67 changes: 18 additions & 49 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"asdf/internal/help"
"asdf/internal/info"
"asdf/internal/installs"
"asdf/internal/paths"
"asdf/internal/plugins"
"asdf/internal/resolve"
"asdf/internal/shims"
Expand Down Expand Up @@ -380,36 +379,23 @@ func envCommand(logger *log.Logger, shimmedCommand string, args []string) error
if err != nil {
return err
}
callbackEnv := map[string]string{
env := map[string]string{
"ASDF_INSTALL_TYPE": parsedVersion.Type,
"ASDF_INSTALL_VERSION": parsedVersion.Value,
"ASDF_INSTALL_PATH": installs.InstallPath(conf, plugin, parsedVersion),
"PATH": setPath(conf, execPaths),
"PATH": setPath(execPaths),
}

var env map[string]string
var fname string

if parsedVersion.Type == "system" {
env = execenv.CurrentEnv()
newPath := paths.RemoveFromPath(env["PATH"], shims.Directory(conf))
env["PATH"] = newPath
var found bool
fname, found = shims.SystemExecutableOnPath(conf, command)
if !found {
fmt.Println("not found")
return err
}
} else {
env, err = execenv.Generate(plugin, callbackEnv)
if parsedVersion.Type != "system" {
env, err = execenv.Generate(plugin, env)
if _, ok := err.(plugins.NoCallbackError); !ok && err != nil {
return err
}
}

fname, err = shims.ExecutableOnPath(env["PATH"], command)
if err != nil {
return err
}
fname, err := shims.ExecutableOnPath(env["PATH"], command)
if err != nil {
return err
}

err = exec.Exec(fname, realArgs, execute.MapToSlice(env))
Expand All @@ -419,9 +405,8 @@ func envCommand(logger *log.Logger, shimmedCommand string, args []string) error
return err
}

func setPath(conf config.Config, pathes []string) string {
currentPath := os.Getenv("PATH")
return strings.Join(pathes, ":") + ":" + paths.RemoveFromPath(currentPath, shims.Directory(conf))
func setPath(paths []string) string {
return strings.Join(paths, ":") + ":" + os.Getenv("PATH")
}

func execCommand(logger *log.Logger, command string, args []string) error {
Expand All @@ -436,7 +421,7 @@ func execCommand(logger *log.Logger, command string, args []string) error {
return err
}

_, plugin, version, err := getExecutable(logger, conf, command)
executable, plugin, version, err := getExecutable(logger, conf, command)
if err != nil {
return err
}
Expand All @@ -452,39 +437,23 @@ func execCommand(logger *log.Logger, command string, args []string) error {
if err != nil {
return err
}
callbackEnv := map[string]string{
env := map[string]string{
"ASDF_INSTALL_TYPE": parsedVersion.Type,
"ASDF_INSTALL_VERSION": parsedVersion.Value,
"ASDF_INSTALL_PATH": installs.InstallPath(conf, plugin, parsedVersion),
"PATH": setPath(conf, execPaths),
"PATH": setPath(execPaths),
}

var env map[string]string
var fname string

if parsedVersion.Type == "system" {
env = execenv.CurrentEnv()
newPath := paths.RemoveFromPath(env["PATH"], shims.Directory(conf))
env["PATH"] = newPath
var found bool
fname, found = shims.SystemExecutableOnPath(conf, command)
if !found {
fmt.Println("not found")
return err
}
} else {
env, err = execenv.Generate(plugin, callbackEnv)
if parsedVersion.Type != "system" {
env, err = execenv.Generate(plugin, env)
if _, ok := err.(plugins.NoCallbackError); !ok && err != nil {
return err
}

fname, err = shims.ExecutableOnPath(env["PATH"], command)
if err != nil {
return err
}
}

return exec.Exec(fname, args, execute.MapToSlice(env))
env = execenv.MergeEnv(execenv.SliceToMap(os.Environ()), env)

return exec.Exec(executable, args, execute.MapToSlice(env))
}

func getExecutable(logger *log.Logger, conf config.Config, command string) (executable string, plugin plugins.Plugin, version string, err error) {
Expand Down

0 comments on commit 007a710

Please sign in to comment.