-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #82 from asdf-vm/tb/shim-exec-env
feat(golang-rewrite): implement `asdf env` command
- Loading branch information
Showing
11 changed files
with
390 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Package execenv contains logic for generating execution environing using a plugin's | ||
// exec-env callback script if available. | ||
package execenv | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"asdf/internal/execute" | ||
"asdf/internal/plugins" | ||
) | ||
|
||
const execEnvCallbackName = "exec-env" | ||
|
||
// Generate runs exec-env callback if available and captures the environment | ||
// variables it sets. It then parses them and returns them as a map. | ||
func Generate(plugin plugins.Plugin, callbackEnv map[string]string) (env map[string]string, err error) { | ||
execEnvPath, err := plugin.CallbackPath(execEnvCallbackName) | ||
if err != nil { | ||
return callbackEnv, err | ||
} | ||
|
||
var stdout strings.Builder | ||
|
||
// This is done to support the legacy behavior. exec-env is the only asdf | ||
// callback that works by exporting environment variables. Because of this, | ||
// executing the callback isn't enough. We actually need to source it (.) so | ||
// the environment variables get set, and then run `env` so they get printed | ||
// to STDOUT. | ||
expression := execute.NewExpression(fmt.Sprintf(". \"%s\"; env", execEnvPath), []string{}) | ||
expression.Env = callbackEnv | ||
expression.Stdout = &stdout | ||
err = expression.Run() | ||
|
||
return envMap(stdout.String()), err | ||
} | ||
|
||
func envMap(env string) map[string]string { | ||
slice := map[string]string{} | ||
|
||
for _, envVar := range strings.Split(env, "\n") { | ||
varValue := strings.Split(envVar, "=") | ||
if len(varValue) == 2 { | ||
slice[varValue[0]] = varValue[1] | ||
} | ||
} | ||
|
||
return slice | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package execenv | ||
|
||
import ( | ||
"testing" | ||
|
||
"asdf/internal/config" | ||
"asdf/internal/plugins" | ||
"asdf/repotest" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
const ( | ||
testPluginName = "lua" | ||
testPluginName2 = "ruby" | ||
) | ||
|
||
func TestGenerate(t *testing.T) { | ||
testDataDir := t.TempDir() | ||
|
||
t.Run("returns map of environment variables", func(t *testing.T) { | ||
conf := config.Config{DataDir: testDataDir} | ||
_, err := repotest.InstallPlugin("dummy_plugin", testDataDir, testPluginName) | ||
assert.Nil(t, err) | ||
plugin := plugins.New(conf, testPluginName) | ||
assert.Nil(t, repotest.WritePluginCallback(plugin.Dir, "exec-env", "#!/usr/bin/env bash\nexport BAZ=bar")) | ||
env, err := Generate(plugin, map[string]string{"ASDF_INSTALL_VERSION": "test"}) | ||
assert.Nil(t, err) | ||
assert.Equal(t, "bar", env["BAZ"]) | ||
assert.Equal(t, "test", env["ASDF_INSTALL_VERSION"]) | ||
}) | ||
|
||
t.Run("returns error when plugin lacks exec-env callback", func(t *testing.T) { | ||
conf := config.Config{DataDir: testDataDir} | ||
_, err := repotest.InstallPlugin("dummy_plugin", testDataDir, testPluginName2) | ||
assert.Nil(t, err) | ||
plugin := plugins.New(conf, testPluginName2) | ||
env, err := Generate(plugin, map[string]string{}) | ||
assert.Equal(t, err.(plugins.NoCallbackError).Error(), "Plugin named ruby does not have a callback named exec-env") | ||
_, found := env["FOO"] | ||
assert.False(t, found) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.