Merge pull request #94 from asdf-vm/tb/plugin-improvements

feat(golang-rewrite): misc. plugin improvements
This commit is contained in:
Trevor Brown 2024-12-14 15:14:59 -05:00 committed by Trevor Brown
commit 33f50650e1
2 changed files with 40 additions and 6 deletions

View File

@ -324,7 +324,6 @@ func currentCommand(logger *log.Logger, tool string) error {
// show single tool // show single tool
plugin := plugins.New(conf, tool) plugin := plugins.New(conf, tool)
err = plugin.Exists() err = plugin.Exists()
_, ok := err.(plugins.PluginMissing) _, ok := err.(plugins.PluginMissing)
pluginExists := !ok pluginExists := !ok
@ -841,7 +840,7 @@ func pluginUpdateCommand(cCtx *cli.Context, logger *log.Logger, pluginName, ref
return err return err
} }
func pluginTestCommand(l *log.Logger, args []string, _, _ string) { func pluginTestCommand(l *log.Logger, args []string, toolVersion, _ string) {
conf, err := config.LoadConfig() conf, err := config.LoadConfig()
if err != nil { if err != nil {
l.Printf("error loading config: %s", err) 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") 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") 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) { func failTest(logger *log.Logger, msg string) {
@ -1079,11 +1090,16 @@ func listAllCommand(logger *log.Logger, conf config.Config, toolName, filter str
return nil 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 stdout strings.Builder
var stderr 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 { if err != nil {
fmt.Printf("Plugin %s's list-all callback script failed with output:\n", plugin.Name) fmt.Printf("Plugin %s's list-all callback script failed with output:\n", plugin.Name)
// Print to stderr // Print to stderr
@ -1131,7 +1147,11 @@ func listLocalCommand(logger *log.Logger, conf config.Config, pluginName, filter
} }
if pluginName != "" { 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) versions, _ := installs.Installed(conf, plugin)
if filter != "" { if filter != "" {
@ -1372,6 +1392,17 @@ func whereCommand(logger *log.Logger, tool, versionStr string) error {
return nil 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 { func reshimToolVersion(conf config.Config, tool, versionStr string, out io.Writer, errOut io.Writer) error {
version := toolversions.Parse(versionStr) version := toolversions.Parse(versionStr)
return shims.GenerateForVersion(conf, plugins.New(conf, tool), version, out, errOut) return shims.GenerateForVersion(conf, plugins.New(conf, tool), version, out, errOut)

View File

@ -11,6 +11,7 @@ import (
"strings" "strings"
"github.com/asdf-vm/asdf/internal/config" "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/hook"
"github.com/asdf-vm/asdf/internal/installs" "github.com/asdf-vm/asdf/internal/installs"
"github.com/asdf-vm/asdf/internal/plugins" "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), "ASDF_CONCURRENCY": asdfConcurrency(conf),
} }
env = execenv.MergeEnv(execenv.SliceToMap(os.Environ()), env)
err = os.MkdirAll(downloadDir, 0o777) err = os.MkdirAll(downloadDir, 0o777)
if err != nil { if err != nil {
return fmt.Errorf("unable to create download dir: %w", err) return fmt.Errorf("unable to create download dir: %w", err)