Skip to content

Commit

Permalink
Merge pull request #76 from asdf-vm/tb/shim-versions-command
Browse files Browse the repository at this point in the history
feat(golang-rewrite): create shimversions command
  • Loading branch information
Stratus3D authored Oct 31, 2024
2 parents a03537d + cad7bb9 commit 2c4d5e6
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 18 deletions.
24 changes: 24 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down
31 changes: 17 additions & 14 deletions internal/shims/shims.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion test/shim_versions_command.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 2c4d5e6

Please sign in to comment.