From 81f732bd4b4b740a1927fbccf5dd20b83039a055 Mon Sep 17 00:00:00 2001 From: Trevor Brown Date: Sat, 14 Dec 2024 13:42:46 -0500 Subject: [PATCH] feat(golang-rewrite): misc. plugin improvements * Correct the environment `bin/install` runs in * Improve output of `asdf list all` command when plugin not found * Update `asdf plugin test` command to install a tool version in the test --- cli/cli.go | 43 ++++++++++++++++++++++++++++++----- internal/versions/versions.go | 3 +++ 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/cli/cli.go b/cli/cli.go index b411153e..e25caee4 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -324,7 +324,6 @@ func currentCommand(logger *log.Logger, tool string) error { // show single tool plugin := plugins.New(conf, tool) - err = plugin.Exists() _, ok := err.(plugins.PluginMissing) pluginExists := !ok @@ -841,7 +840,7 @@ func pluginUpdateCommand(cCtx *cli.Context, logger *log.Logger, pluginName, ref return err } -func pluginTestCommand(l *log.Logger, args []string, _, _ string) { +func pluginTestCommand(l *log.Logger, args []string, toolVersion, _ string) { conf, err := config.LoadConfig() if err != nil { l.Printf("error loading config: %s", err) @@ -922,9 +921,21 @@ func pluginTestCommand(l *log.Logger, args []string, _, _ string) { failTest(l, "Unable to list available versions") } - if len(strings.Split(output.String(), " ")) < 1 { + allVersions := strings.Fields(output.String()) + if len(allVersions) < 1 { failTest(l, "list-all did not return any version") } + + // grab first version returned by list-all callback if no version provided as + // a CLI argument + if toolVersion == "" { + toolVersion = allVersions[0] + } + + err = versions.InstallOneVersion(conf, plugin, toolVersion, false, os.Stdout, os.Stderr) + if err != nil { + failTest(l, "install exited with an error") + } } func failTest(logger *log.Logger, msg string) { @@ -1079,11 +1090,16 @@ func listAllCommand(logger *log.Logger, conf config.Config, toolName, filter str return nil } - plugin := plugins.New(conf, toolName) + plugin, err := loadPlugin(logger, conf, toolName) + if err != nil { + os.Exit(1) + return err + } + var stdout strings.Builder var stderr strings.Builder - err := plugin.RunCallback("list-all", []string{}, map[string]string{}, &stdout, &stderr) + err = plugin.RunCallback("list-all", []string{}, map[string]string{}, &stdout, &stderr) if err != nil { fmt.Printf("Plugin %s's list-all callback script failed with output:\n", plugin.Name) // Print to stderr @@ -1131,7 +1147,11 @@ func listLocalCommand(logger *log.Logger, conf config.Config, pluginName, filter } if pluginName != "" { - plugin := plugins.New(conf, pluginName) + plugin, err := loadPlugin(logger, conf, pluginName) + if err != nil { + os.Exit(1) + return err + } versions, _ := installs.Installed(conf, plugin) if filter != "" { @@ -1372,6 +1392,17 @@ func whereCommand(logger *log.Logger, tool, versionStr string) error { return nil } +func loadPlugin(logger *log.Logger, conf config.Config, pluginName string) (plugins.Plugin, error) { + plugin := plugins.New(conf, pluginName) + err := plugin.Exists() + if err != nil { + logger.Printf("No such plugin: %s", pluginName) + return plugin, err + } + + return plugin, err +} + func reshimToolVersion(conf config.Config, tool, versionStr string, out io.Writer, errOut io.Writer) error { version := toolversions.Parse(versionStr) return shims.GenerateForVersion(conf, plugins.New(conf, tool), version, out, errOut) diff --git a/internal/versions/versions.go b/internal/versions/versions.go index 0a8d3de7..04544ed5 100644 --- a/internal/versions/versions.go +++ b/internal/versions/versions.go @@ -11,6 +11,7 @@ import ( "strings" "github.com/asdf-vm/asdf/internal/config" + "github.com/asdf-vm/asdf/internal/execenv" "github.com/asdf-vm/asdf/internal/hook" "github.com/asdf-vm/asdf/internal/installs" "github.com/asdf-vm/asdf/internal/plugins" @@ -153,6 +154,8 @@ func InstallOneVersion(conf config.Config, plugin plugins.Plugin, versionStr str "ASDF_CONCURRENCY": asdfConcurrency(conf), } + env = execenv.MergeEnv(execenv.SliceToMap(os.Environ()), env) + err = os.MkdirAll(downloadDir, 0o777) if err != nil { return fmt.Errorf("unable to create download dir: %w", err)