From cad7bb9a563b2b4f086fe1dd6a5ac1aaecd8b135 Mon Sep 17 00:00:00 2001 From: Trevor Brown Date: Thu, 31 Oct 2024 09:20:00 -0400 Subject: [PATCH] feat(golang-rewrite): create shimversions command * Enable `shim_versions_command.bats` tests * Create `asdf shimversions` command --- cmd/cmd.go | 24 ++++++++++++++++++++++++ internal/shims/shims.go | 31 +++++++++++++++++-------------- main_test.go | 6 +++--- test/shim_versions_command.bats | 2 +- 4 files changed, 45 insertions(+), 18 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index eaf18112..d8dc1830 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -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() diff --git a/internal/shims/shims.go b/internal/shims/shims.go index 3504d159..a59c6ccc 100644 --- a/internal/shims/shims.go +++ b/internal/shims/shims.go @@ -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 } diff --git a/main_test.go b/main_test.go index 3c0454ca..424b4aba 100644 --- a/main_test.go +++ b/main_test.go @@ -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") diff --git a/test/shim_versions_command.bats b/test/shim_versions_command.bats index 2371231b..857517de 100644 --- a/test/shim_versions_command.bats +++ b/test/shim_versions_command.bats @@ -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"