Skip to content
This repository has been archived by the owner on Dec 18, 2024. It is now read-only.

Commit

Permalink
feat(golang-rewrite): misc. plugin improvements
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Stratus3D committed Dec 18, 2024
1 parent b7193e4 commit 3af0291
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
43 changes: 37 additions & 6 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 != "" {
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions internal/versions/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 3af0291

Please sign in to comment.