Skip to content

Commit 9ed4216

Browse files
committed
feat(golang-rewrite): misc. version improvements
* Update `pluginListCommand` to print tools and versions to STDOUT * Improve output of `asdf current` command
1 parent 76bc18a commit 9ed4216

File tree

3 files changed

+106
-32
lines changed

3 files changed

+106
-32
lines changed

cli/cli.go

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,17 @@ func Execute(version string) {
6565
},
6666
{
6767
Name: "current",
68+
Flags: []cli.Flag{
69+
&cli.BoolFlag{
70+
Name: "no-header",
71+
Usage: "Whether or not to print a header line",
72+
},
73+
},
6874
Action: func(cCtx *cli.Context) error {
6975
tool := cCtx.Args().Get(0)
7076

71-
return currentCommand(logger, tool)
77+
noHeader := cCtx.Bool("no-header")
78+
return currentCommand(logger, tool, noHeader)
7279
},
7380
},
7481
{
@@ -286,7 +293,7 @@ func Execute(version string) {
286293
}
287294

288295
// This function is a whole mess and needs to be refactored
289-
func currentCommand(logger *log.Logger, tool string) error {
296+
func currentCommand(logger *log.Logger, tool string, noHeader bool) error {
290297
conf, err := config.LoadConfig()
291298
if err != nil {
292299
logger.Printf("error loading config: %s", err)
@@ -301,6 +308,9 @@ func currentCommand(logger *log.Logger, tool string) error {
301308

302309
// settings here to match legacy implementation
303310
w := tabwriter.NewWriter(os.Stdout, 16, 0, 1, ' ', 0)
311+
if !noHeader {
312+
writeHeader(w)
313+
}
304314

305315
if tool == "" {
306316
// show all
@@ -358,23 +368,38 @@ func getVersionInfo(conf config.Config, plugin plugins.Plugin, currentDir string
358368
return toolversion, found, installed
359369
}
360370

371+
func writeHeader(w *tabwriter.Writer) {
372+
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", "Name", "Version", "Source", "Installed")
373+
}
374+
361375
func formatCurrentVersionLine(w *tabwriter.Writer, plugin plugins.Plugin, toolversion resolve.ToolVersions, found bool, installed bool, err error) error {
362376
if err != nil {
363377
return err
364378
}
365379

366-
fmt.Fprintf(w, "%s\t%s\t%s\n", plugin.Name, formatVersions(toolversion.Versions), formatSource(toolversion, plugin, found, installed))
380+
// columns are: name, version, source, installed
381+
version := formatVersions(toolversion.Versions)
382+
source := formatSource(toolversion, found)
383+
installedStatus := formatInstalled(toolversion, plugin.Name, found, installed)
384+
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", plugin.Name, version, source, installedStatus)
367385
return nil
368386
}
369387

370-
func formatSource(toolversion resolve.ToolVersions, plugin plugins.Plugin, found bool, installed bool) string {
388+
func formatInstalled(toolversion resolve.ToolVersions, name string, found, installed bool) string {
389+
if !found {
390+
return ""
391+
}
392+
if !installed {
393+
return fmt.Sprintf("false - Run `asdf install %s %s`", name, toolversion.Versions[0])
394+
}
395+
return "true"
396+
}
397+
398+
func formatSource(toolversion resolve.ToolVersions, found bool) string {
371399
if found {
372-
if !installed {
373-
return fmt.Sprintf("Not installed. Run \"asdf install %s %s\"", plugin.Name, toolversion.Versions[0])
374-
}
375400
return filepath.Join(toolversion.Directory, toolversion.Source)
376401
}
377-
return fmt.Sprintf("No version is set. Run \"asdf <global|shell|local> %s <version>\"", plugin.Name)
402+
return "______"
378403
}
379404

380405
func formatVersions(versions []string) string {
@@ -693,13 +718,13 @@ func pluginListCommand(cCtx *cli.Context, logger *log.Logger) error {
693718
// logic
694719
for _, plugin := range plugins {
695720
if urls && refs {
696-
logger.Printf("%s\t\t%s\t%s\n", plugin.Name, plugin.URL, plugin.Ref)
721+
fmt.Printf("%s\t\t%s\t%s\n", plugin.Name, plugin.URL, plugin.Ref)
697722
} else if refs {
698-
logger.Printf("%s\t\t%s\n", plugin.Name, plugin.Ref)
723+
fmt.Printf("%s\t\t%s\n", plugin.Name, plugin.Ref)
699724
} else if urls {
700-
logger.Printf("%s\t\t%s\n", plugin.Name, plugin.URL)
725+
fmt.Printf("%s\t\t%s\n", plugin.Name, plugin.URL)
701726
} else {
702-
logger.Printf("%s\n", plugin.Name)
727+
fmt.Printf("%s\n", plugin.Name)
703728
}
704729
}
705730

@@ -1370,7 +1395,7 @@ func whereCommand(logger *log.Logger, tool, versionStr string) error {
13701395
versionStruct := toolversions.Version{Type: "version", Value: versions.Versions[0]}
13711396
if installs.IsInstalled(conf, plugin, versionStruct) {
13721397
installPath := installs.InstallPath(conf, plugin, versionStruct)
1373-
logger.Printf("%s", installPath)
1398+
fmt.Printf("%s", installPath)
13741399
return nil
13751400
}
13761401
}
@@ -1387,7 +1412,7 @@ func whereCommand(logger *log.Logger, tool, versionStr string) error {
13871412
}
13881413

13891414
installPath := installs.InstallPath(conf, plugin, version)
1390-
logger.Printf("%s", installPath)
1415+
fmt.Printf("%s", installPath)
13911416

13921417
return nil
13931418
}

docs/guide/upgrading-from-v0-14-to-v0-15.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ not an executable. The new rewrite removes all shell code from asdf, and it is
5050
now a binary rather than a shell function, so setting environment variables
5151
directly in the shell is no longer possible.
5252

53+
### `asdf current` has changed
54+
55+
Instead of three columns in the output, with the last being either the location
56+
the version is set or a suggested command that could be run to set or install a
57+
version. The third column has been split into two columns. The third column now
58+
only indicates the source of the version if it is set (typically either version
59+
file or environment variable) and the fourth is a boolean indicating whether
60+
the specified version is actually installed. If it is not installed, a
61+
suggested install command is shown.
62+
5363
### Plugin extension commands must now be prefixed with `cmd`
5464

5565
Previously plugin extension commands could be run like this:

test/current_command.bats

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,42 +20,62 @@ teardown() {
2020
@test "current should derive from the current .tool-versions" {
2121
cd "$PROJECT_DIR"
2222
echo 'dummy 1.1.0' >>"$PROJECT_DIR/.tool-versions"
23-
expected="dummy 1.1.0 $PROJECT_DIR/.tool-versions"
23+
expected="Name Version Source Installed
24+
dummy 1.1.0 $PROJECT_DIR/.tool-versions true"
2425

2526
run asdf current "dummy"
27+
28+
# shellcheck disable=SC2001
29+
condensed_output="$(sed -e 's/ [ ]*/ /g' <<<"$output")"
30+
2631
[ "$status" -eq 0 ]
27-
[ "$output" = "$expected" ]
32+
[ "$condensed_output" = "$expected" ]
2833
}
2934

3035
@test "current should handle long version name" {
3136
cd "$PROJECT_DIR"
3237
echo "dummy nightly-2000-01-01" >>"$PROJECT_DIR/.tool-versions"
33-
expected="dummy nightly-2000-01-01 $PROJECT_DIR/.tool-versions"
38+
expected="Name Version Source Installed
39+
dummy nightly-2000-01-01 $PROJECT_DIR/.tool-versions true"
3440

3541
run asdf current "dummy"
42+
43+
# shellcheck disable=SC2001
44+
condensed_output="$(sed -e 's/ [ ]*/ /g' <<<"$output")"
45+
3646
[ "$status" -eq 0 ]
37-
[ "$output" = "$expected" ]
47+
[ "$condensed_output" = "$expected" ]
3848
}
3949

4050
@test "current should handle multiple versions" {
4151
cd "$PROJECT_DIR"
4252
echo "dummy 1.2.0 1.1.0" >>"$PROJECT_DIR/.tool-versions"
43-
expected="dummy 1.2.0 1.1.0 $PROJECT_DIR/.tool-versions"
53+
expected="Name Version Source Installed
54+
dummy 1.2.0 1.1.0 $PROJECT_DIR/.tool-versions true"
4455

4556
run asdf current "dummy"
57+
58+
# shellcheck disable=SC2001
59+
condensed_output="$(sed -e 's/ [ ]*/ /g' <<<"$output")"
60+
4661
[ "$status" -eq 0 ]
47-
[ "$output" = "$expected" ]
62+
[ "$condensed_output" = "$expected" ]
4863
}
4964

5065
@test "current should derive from the legacy file if enabled" {
5166
cd "$PROJECT_DIR"
5267
echo 'legacy_version_file = yes' >"$HOME/.asdfrc"
5368
echo '1.2.0' >>"$PROJECT_DIR/.dummy-version"
54-
expected="dummy 1.2.0 $PROJECT_DIR/.dummy-version"
69+
expected="Name Version Source Installed
70+
dummy 1.2.0 $PROJECT_DIR/.dummy-version true"
5571

5672
run asdf current "dummy"
73+
74+
# shellcheck disable=SC2001
75+
condensed_output="$(sed -e 's/ [ ]*/ /g' <<<"$output")"
76+
5777
[ "$status" -eq 0 ]
58-
[ "$output" = "$expected" ]
78+
[ "$condensed_output" = "$expected" ]
5979
}
6080

6181
# TODO: Need to fix plugin error as well
@@ -69,21 +89,33 @@ teardown() {
6989

7090
@test "current should error when no version is set" {
7191
cd "$PROJECT_DIR"
72-
expected="dummy ______ No version is set. Run \"asdf <global|shell|local> dummy <version>\""
92+
expected="Name Version Source Installed
93+
dummy ______ ______ "
7394

7495
run asdf current "dummy"
96+
97+
# shellcheck disable=SC2001
98+
condensed_output="$(sed -e 's/ [ ]*/ /g' <<<"$output")"
99+
75100
[ "$status" -eq 126 ]
76-
[ "$output" = "$expected" ]
101+
[ "$condensed_output" = "$expected" ]
77102
}
78103

79104
@test "current should error when a version is set that isn't installed" {
80105
cd "$PROJECT_DIR"
81106
echo 'dummy 9.9.9' >>"$PROJECT_DIR/.tool-versions"
82-
expected="dummy 9.9.9 Not installed. Run \"asdf install dummy 9.9.9\""
107+
expected="Name Version Source Installed
108+
dummy 9.9.9 $PROJECT_DIR/.tool-versions false - Run \`asdf install dummy 9.9.9\`"
83109

110+
asdf uninstall dummy 9.9.9 || true
84111
run asdf current "dummy"
112+
113+
# shellcheck disable=SC2001
114+
condensed_output="$(sed -e 's/ [ ]*/ /g' -e 's/ $//g' <<<"$output")"
115+
85116
[ "$status" -eq 1 ]
86-
[ "$output" = "$expected" ]
117+
118+
[ "$condensed_output" = "$expected" ]
87119
}
88120

89121
@test "should output all plugins when no plugin passed" {
@@ -99,13 +131,17 @@ teardown() {
99131
cd "$PROJECT_DIR"
100132
echo 'dummy 1.1.0' >>"$PROJECT_DIR/.tool-versions"
101133
echo 'foobar 1.0.0' >>"$PROJECT_DIR/.tool-versions"
134+
expected="Name Version Source Installed
135+
baz ______ ______
136+
dummy 1.1.0 $PROJECT_DIR/.tool-versions true
137+
foobar 1.0.0 $PROJECT_DIR/.tool-versions true"
102138

103139
run asdf current
104-
expected="baz ______ No version is set. Run \"asdf <global|shell|local> baz <version>\"
105-
dummy 1.1.0 $PROJECT_DIR/.tool-versions
106-
foobar 1.0.0 $PROJECT_DIR/.tool-versions"
107140

108-
[ "$expected" = "$output" ]
141+
# shellcheck disable=SC2001
142+
condensed_output="$(sed -e 's/ [ ]*/ /g' -e 's/ $//g' <<<"$output")"
143+
144+
[ "$expected" = "$condensed_output" ]
109145
}
110146

111147
@test "should always match the tool name exactly" {
@@ -136,9 +172,12 @@ foobar 1.0.0 $PROJECT_DIR/.tool-versions"
136172
@test "current should handle comments" {
137173
cd "$PROJECT_DIR"
138174
echo "dummy 1.2.0 # this is a comment" >>"$PROJECT_DIR/.tool-versions"
139-
expected="dummy 1.2.0 $PROJECT_DIR/.tool-versions"
175+
expected="Name Version Source Installed
176+
dummy 1.2.0 $PROJECT_DIR/.tool-versions true"
140177

141178
run asdf current "dummy"
142179
[ "$status" -eq 0 ]
143-
[ "$output" = "$expected" ]
180+
# shellcheck disable=SC2001
181+
condensed_output="$(sed -e 's/ [ ]*/ /g' <<<"$output")"
182+
[ "$condensed_output" = "$expected" ]
144183
}

0 commit comments

Comments
 (0)