Skip to content

Commit b33ab64

Browse files
committed
feat(golang-rewrite): create asdf current command
* Create asdf current command * Correct output of asdf exec command when no command is provided * Enable current_command.bats tests * Fix current_command.bats test code
1 parent 6c4df2a commit b33ab64

File tree

4 files changed

+127
-7
lines changed

4 files changed

+127
-7
lines changed

cmd/cmd.go

Lines changed: 117 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@ import (
77
"io"
88
"log"
99
"os"
10+
"path/filepath"
1011
"strings"
12+
"text/tabwriter"
1113

1214
"asdf/internal/config"
1315
"asdf/internal/exec"
1416
"asdf/internal/info"
1517
"asdf/internal/installs"
1618
"asdf/internal/plugins"
19+
"asdf/internal/resolve"
1720
"asdf/internal/shims"
1821
"asdf/internal/versions"
1922

@@ -45,6 +48,14 @@ func Execute(version string) {
4548
Usage: "The multiple runtime version manager",
4649
UsageText: usageText,
4750
Commands: []*cli.Command{
51+
{
52+
Name: "current",
53+
Action: func(cCtx *cli.Context) error {
54+
tool := cCtx.Args().Get(0)
55+
56+
return currentCommand(logger, tool)
57+
},
58+
},
4859
{
4960
Name: "exec",
5061
Action: func(cCtx *cli.Context) error {
@@ -169,10 +180,114 @@ func Execute(version string) {
169180
}
170181
}
171182

183+
// This function is a whole mess and needs to be refactored
184+
func currentCommand(logger *log.Logger, tool string) error {
185+
conf, err := config.LoadConfig()
186+
if err != nil {
187+
logger.Printf("error loading config: %s", err)
188+
return err
189+
}
190+
191+
currentDir, err := os.Getwd()
192+
if err != nil {
193+
logger.Printf("unable to get current directory: %s", err)
194+
return err
195+
}
196+
197+
// settings here to match legacy implementation
198+
w := tabwriter.NewWriter(os.Stdout, 16, 0, 1, ' ', 0)
199+
200+
if tool == "" {
201+
// show all
202+
allPlugins, err := plugins.List(conf, false, false)
203+
if err != nil {
204+
return err
205+
}
206+
207+
if len(allPlugins) < 1 {
208+
fmt.Println("No plugins installed")
209+
return nil
210+
}
211+
212+
for _, plugin := range allPlugins {
213+
toolversion, versionFound, versionInstalled := getVersionInfo(conf, plugin, currentDir)
214+
formatCurrentVersionLine(w, plugin, toolversion, versionFound, versionInstalled, err)
215+
}
216+
w.Flush()
217+
return nil
218+
}
219+
220+
// show single tool
221+
plugin := plugins.New(conf, tool)
222+
223+
err = plugin.Exists()
224+
_, ok := err.(plugins.PluginMissing)
225+
pluginExists := !ok
226+
227+
if pluginExists {
228+
toolversion, versionFound, versionInstalled := getVersionInfo(conf, plugin, currentDir)
229+
formatCurrentVersionLine(w, plugin, toolversion, versionFound, versionInstalled, err)
230+
w.Flush()
231+
if !versionFound {
232+
os.Exit(126)
233+
}
234+
235+
if !versionInstalled {
236+
os.Exit(1)
237+
}
238+
} else {
239+
fmt.Printf("No such plugin: %s\n", tool)
240+
return err
241+
}
242+
243+
return nil
244+
}
245+
246+
func getVersionInfo(conf config.Config, plugin plugins.Plugin, currentDir string) (resolve.ToolVersions, bool, bool) {
247+
toolversion, found, _ := resolve.Version(conf, plugin, currentDir)
248+
installed := false
249+
if found {
250+
firstVersion := toolversion.Versions[0]
251+
versionType, version := versions.ParseString(firstVersion)
252+
installed = installs.IsInstalled(conf, plugin, versionType, version)
253+
}
254+
return toolversion, found, installed
255+
}
256+
257+
func formatCurrentVersionLine(w *tabwriter.Writer, plugin plugins.Plugin, toolversion resolve.ToolVersions, found bool, installed bool, err error) error {
258+
if err != nil {
259+
return err
260+
}
261+
262+
fmt.Fprintf(w, "%s\t%s\t%s\n", plugin.Name, formatVersions(toolversion.Versions), formatSource(toolversion, plugin, found, installed))
263+
return nil
264+
}
265+
266+
func formatSource(toolversion resolve.ToolVersions, plugin plugins.Plugin, found bool, installed bool) string {
267+
if found {
268+
if !installed {
269+
return fmt.Sprintf("Not installed. Run \"asdf install %s %s\"", plugin.Name, toolversion.Versions[0])
270+
}
271+
return filepath.Join(toolversion.Directory, toolversion.Source)
272+
}
273+
return fmt.Sprintf("No version is set. Run \"asdf <global|shell|local> %s <version>\"", plugin.Name)
274+
}
275+
276+
func formatVersions(versions []string) string {
277+
switch len(versions) {
278+
case 0:
279+
return "______"
280+
case 1:
281+
return versions[0]
282+
default:
283+
return strings.Join(versions, " ")
284+
}
285+
}
286+
172287
func execCommand(logger *log.Logger, command string, args []string) error {
173288
if command == "" {
174-
logger.Printf("no command specified")
175-
return fmt.Errorf("no command specified")
289+
logger.Printf("usage: asdf exec <command>")
290+
return fmt.Errorf("usage: asdf exec <command>")
176291
}
177292

178293
conf, err := config.LoadConfig()

internal/plugins/plugins.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"fmt"
77
"io"
8+
"io/fs"
89
"os"
910
"path/filepath"
1011
"regexp"
@@ -175,6 +176,10 @@ func List(config config.Config, urls, refs bool) (plugins []Plugin, err error) {
175176
pluginsDir := DataDirectory(config.DataDir)
176177
files, err := os.ReadDir(pluginsDir)
177178
if err != nil {
179+
if _, ok := err.(*fs.PathError); ok {
180+
return []Plugin{}, nil
181+
}
182+
178183
return plugins, err
179184
}
180185

main_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ func TestBatsTests(t *testing.T) {
1919
// Run tests with the asdf binary in the temp directory
2020

2121
// Uncomment these as they are implemented
22-
//t.Run("current_command", func(t *testing.T) {
23-
// runBatsFile(t, dir, "current_command.bats")
24-
//})
22+
t.Run("current_command", func(t *testing.T) {
23+
runBatsFile(t, dir, "current_command.bats")
24+
})
2525

2626
//t.Run("help_command", func(t *testing.T) {
2727
// runBatsFile(t, dir, "help_command.bats")

test/current_command.bats

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ teardown() {
8888

8989
@test "should output all plugins when no plugin passed" {
9090

91-
install_dummy_plugin
91+
#install_dummy_plugin
9292
install_dummy_version "1.1.0"
9393

9494
install_mock_plugin "foobar"
@@ -109,7 +109,7 @@ foobar 1.0.0 $PROJECT_DIR/.tool-versions"
109109
}
110110

111111
@test "should always match the tool name exactly" {
112-
install_dummy_plugin
112+
#install_dummy_plugin
113113
install_dummy_version "1.1.0"
114114

115115
install_mock_plugin "y"

0 commit comments

Comments
 (0)