Merge pull request #76 from asdf-vm/tb/shim-versions-command

feat(golang-rewrite): create shimversions command
This commit is contained in:
Trevor Brown 2024-10-31 09:27:29 -04:00 committed by Trevor Brown
commit 9d9fc698f4
4 changed files with 45 additions and 18 deletions

View File

@ -184,6 +184,13 @@ func Execute(version string) {
return reshimCommand(logger, args.Get(0), args.Get(1))
},
},
{
Name: "shimversions",
Action: func(cCtx *cli.Context) error {
args := cCtx.Args()
return shimVersionsCommand(logger, args.Get(0))
},
},
{
Name: "uninstall",
Action: func(cCtx *cli.Context) error {
@ -793,6 +800,23 @@ func reshimCommand(logger *log.Logger, tool, version string) (err error) {
return reshimToolVersion(conf, tool, version, os.Stdout, os.Stderr)
}
func shimVersionsCommand(logger *log.Logger, shimName string) error {
conf, err := config.LoadConfig()
if err != nil {
logger.Printf("error loading config: %s", err)
return err
}
shimPath := shims.Path(conf, shimName)
toolVersions, err := shims.GetToolsAndVersionsFromShimFile(shimPath)
for _, toolVersion := range toolVersions {
for _, version := range toolVersion.Versions {
fmt.Printf("%s %s", toolVersion.Name, version)
}
}
return err
}
// This function is a whole mess and needs to be refactored
func whichCommand(logger *log.Logger, command string) error {
conf, err := config.LoadConfig()

View File

@ -63,7 +63,7 @@ func FindExecutable(conf config.Config, shimName, currentDirectory string) (stri
return "", false, UnknownCommandError{shim: shimName}
}
toolVersions, err := getToolsAndVersionsFromShimFile(shimPath)
toolVersions, err := GetToolsAndVersionsFromShimFile(shimPath)
if err != nil {
return "", false, err
}
@ -154,6 +154,21 @@ func GetExecutablePath(conf config.Config, plugin plugins.Plugin, shimName, vers
return "", fmt.Errorf("executable not found")
}
// GetToolsAndVersionsFromShimFile takes a file path and parses out the tools
// and versions present in it and returns them as a slice containing info in
// ToolVersions structs.
func GetToolsAndVersionsFromShimFile(shimPath string) (versions []toolversions.ToolVersions, err error) {
contents, err := os.ReadFile(shimPath)
if err != nil {
return versions, err
}
versions = parse(string(contents))
versions = toolversions.Unique(versions)
return versions, err
}
func getCustomExecutablePath(conf config.Config, plugin plugins.Plugin, shimName, version string) (string, error) {
var stdOut strings.Builder
var stdErr strings.Builder
@ -169,18 +184,6 @@ func getCustomExecutablePath(conf config.Config, plugin plugins.Plugin, shimName
return filepath.Join(installPath, stdOut.String()), err
}
func getToolsAndVersionsFromShimFile(shimPath string) (versions []toolversions.ToolVersions, err error) {
contents, err := os.ReadFile(shimPath)
if err != nil {
return versions, err
}
versions = parse(string(contents))
versions = toolversions.Unique(versions)
return versions, err
}
// RemoveAll removes all shim scripts
func RemoveAll(conf config.Config) error {
shimDir := filepath.Join(conf.DataDir, shimDirName)
@ -270,7 +273,7 @@ func Write(conf config.Config, plugin plugins.Plugin, version, executablePath st
versions := []toolversions.ToolVersions{{Name: plugin.Name, Versions: []string{version}}}
if _, err := os.Stat(shimPath); err == nil {
oldVersions, err := getToolsAndVersionsFromShimFile(shimPath)
oldVersions, err := GetToolsAndVersionsFromShimFile(shimPath)
if err != nil {
return err
}

View File

@ -83,9 +83,9 @@ func TestBatsTests(t *testing.T) {
// runBatsFile(t, dir, "shim_exec.bats")
//})
//t.Run("shim_versions_command", func(t *testing.T) {
// runBatsFile(t, dir, "shim_versions_command.bats")
//})
t.Run("shim_versions_command", func(t *testing.T) {
runBatsFile(t, dir, "shim_versions_command.bats")
})
t.Run("uninstall_command", func(t *testing.T) {
runBatsFile(t, dir, "uninstall_command.bats")

View File

@ -22,7 +22,7 @@ teardown() {
run asdf install dummy 1.0
run asdf reshim dummy
run asdf shim-versions dummy
run asdf shimversions dummy
[ "$status" -eq 0 ]
echo "$output" | grep "dummy 3.0"