Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 3 additions & 12 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -1222,22 +1222,13 @@ func listAllCommand(logger *log.Logger, conf config.Config, toolName, filter str
return err
}

var stdout strings.Builder
var stderr strings.Builder

err = plugin.RunCallback("list-all", []string{}, map[string]string{}, &stdout, &stderr)
versions, err := plugin.GetAvailableVersions()
if err != nil {
fmt.Printf("Plugin %s's list-all callback script failed with output:\n", plugin.Name)
// Print to stderr
os.Stderr.WriteString(stderr.String())
os.Stderr.WriteString(stdout.String())

logger.Printf("%v", err)
cli.OsExiter(1)
return err
}

versions := strings.Split(stdout.String(), " ")

if filter != "" {
versions = filterByExactMatch(versions, filter)
}
Expand All @@ -1249,7 +1240,7 @@ func listAllCommand(logger *log.Logger, conf config.Config, toolName, filter str
}

for _, version := range versions {
fmt.Printf("%s\n", version)
logger.Printf("%s\n", version)
}

return nil
Expand Down
13 changes: 12 additions & 1 deletion internal/cli/set/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"os"
"path/filepath"
"slices"
"strings"

"github.com/asdf-vm/asdf/internal/config"
Expand Down Expand Up @@ -35,11 +36,15 @@ func Main(_ io.Writer, stderr io.Writer, args []string, home bool, parent bool,
}

resolvedVersions := []string{}
plugin := plugins.New(conf, args[0])
pluginAvailableVersions, err := plugin.GetAvailableVersions()
if err != nil {
return printError(stderr, fmt.Sprintf("error getting available plugin versions: %s", err))
}

for _, version := range args[1:] {
parsedVersion := toolversions.ParseFromCliArg(version)
if parsedVersion.Type == "latest" {
plugin := plugins.New(conf, args[0])
resolvedVersion, err := versions.Latest(plugin, parsedVersion.Value)
if err != nil {
return fmt.Errorf("unable to resolve latest version for %s", plugin.Name)
Expand All @@ -50,6 +55,12 @@ func Main(_ io.Writer, stderr io.Writer, args []string, home bool, parent bool,
resolvedVersions = append(resolvedVersions, version)
}

for _, version := range resolvedVersions {
if !slices.Contains(pluginAvailableVersions, version) {
return printError(stderr, fmt.Sprintf("version %s is not available for plugin %s\n", version, plugin.Name))
}
}

tv := toolversions.ToolVersions{Name: args[0], Versions: resolvedVersions}

if home {
Expand Down
1 change: 0 additions & 1 deletion internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ func (r Repo) Update(ref string) (string, string, string, error) {
// If no ref is provided we take the default branch of the remote
if strings.TrimSpace(ref) == "" {
ref, err = r.remoteDefaultBranch()

if err != nil {
return "", "", "", err
}
Expand Down
18 changes: 18 additions & 0 deletions internal/plugins/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,24 @@ func (p Plugin) Update(conf config.Config, ref string, out, errout io.Writer) (s
return newRef, nil
}

// GetAvailableVersions retrieves all available versions of the plugin
func (p Plugin) GetAvailableVersions() ([]string, error) {
var stdout, stderr strings.Builder

err := p.RunCallback("list-all", []string{}, map[string]string{}, &stdout, &stderr)
if err != nil {
msg := fmt.Sprintf(
"Plugin %s's list-all callback script failed with error:\n%v\nStderr:\n%v\nStdout:\n%v\n",
p.Name, err, stderr.String(), stdout.String(),
)

return nil, errors.New(msg)
}

versions := strings.Split(stdout.String(), " ")
return versions, nil
}

// List takes config and flags for what to return and builds a list of plugins
// representing the currently installed plugins on the system.
func List(config config.Config, urls, refs bool) (plugins []Plugin, err error) {
Expand Down
1 change: 0 additions & 1 deletion internal/shims/shims.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ func FindExecutable(conf config.Config, shimName, currentDirectory string) (path
}

versions, found, err := resolve.Version(conf, plugin, currentDirectory)

if err != nil {
return "", plugins.Plugin{}, "", false, nil
}
Expand Down
4 changes: 2 additions & 2 deletions internal/versions/versions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func TestInstallOneVersion(t *testing.T) {
stdout, stderr := buildOutputs()

installScript := filepath.Join(conf.DataDir, "plugins", plugin.Name, "bin", "install")
f, err := os.OpenFile(installScript, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0777)
f, err := os.OpenFile(installScript, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0o777)
assert.Nil(t, err)
_, err = f.WriteString("\nexit 1")
assert.Nil(t, err)
Expand Down Expand Up @@ -347,7 +347,7 @@ func TestLatest(t *testing.T) {

// Replace latest-stable script so it returns a dev version that would be otherwise filtered out
latestScript := filepath.Join(pluginDir, "bin", "latest-stable")
err = os.WriteFile(latestScript, []byte("#!/usr/bin/env bash\necho 1.2.3-dev"), 0777)
err = os.WriteFile(latestScript, []byte("#!/usr/bin/env bash\necho 1.2.3-dev"), 0o777)
assert.Nil(t, err)

version, err := Latest(plugin, "")
Expand Down
Loading