Skip to content

Commit 3dd0dd3

Browse files
authored
fix: correct exit status when sub-command does not exist (#1991)
Fixes #1928
1 parent 8990b6b commit 3dd0dd3

File tree

3 files changed

+64
-35
lines changed

3 files changed

+64
-35
lines changed

cmd/asdf/main_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ func TestBatsTests(t *testing.T) {
106106
t.Run("which_command", func(t *testing.T) {
107107
runBatsFile(t, dir, "which_command.bats")
108108
})
109+
110+
t.Run("non_existent_command", func(t *testing.T) {
111+
runBatsFile(t, dir, "non_existent_command.bats")
112+
})
109113
}
110114

111115
func runBatsFile(t *testing.T, dir, filename string) {

internal/cli/cli.go

+36-35
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ func Execute(version string) {
181181
Name: "plugin",
182182
Action: func(_ *cli.Context) error {
183183
logger.Println("Unknown command: `asdf plugin`")
184-
os.Exit(1)
184+
cli.OsExiter(1)
185185
return nil
186186
},
187187
Subcommands: []*cli.Command{
@@ -335,14 +335,15 @@ func Execute(version string) {
335335
},
336336
},
337337
},
338-
Action: func(_ *cli.Context) error {
339-
return helpCommand(logger, version, "", "")
338+
CommandNotFound: func(_ *cli.Context, s string) {
339+
logger.Printf("invalid command provided: %s\n\n", s)
340+
helpCommand(logger, version, "", "")
341+
cli.OsExiter(1)
340342
},
341343
}
342344

343-
err := app.Run(os.Args)
344-
if err != nil {
345-
os.Exit(1)
345+
if err := app.Run(os.Args); err != nil {
346+
cli.OsExiter(1)
346347
}
347348
}
348349

@@ -415,7 +416,7 @@ func currentCommand(logger *log.Logger, tool string, noHeader bool) error {
415416
}
416417

417418
if !versionInstalled {
418-
os.Exit(1)
419+
cli.OsExiter(1)
419420
}
420421
} else {
421422
fmt.Printf("No such plugin: %s\n", tool)
@@ -589,7 +590,7 @@ func execCommand(logger *log.Logger, command string, args []string) error {
589590

590591
err = hook.RunWithOutput(conf, fmt.Sprintf("pre_%s_%s", plugin.Name, filepath.Base(executable)), args, os.Stdout, os.Stderr)
591592
if err != nil {
592-
os.Exit(1)
593+
cli.OsExiter(1)
593594
return err
594595
}
595596

@@ -653,7 +654,7 @@ func getExecutable(logger *log.Logger, conf config.Config, command string) (exec
653654

654655
if _, ok := err.(shims.NoExecutableForPluginError); ok {
655656
logger.Printf("No executable %s found for current version. Please select a different version or install %s manually for the current version", command, command)
656-
os.Exit(1)
657+
cli.OsExiter(1)
657658
return "", plugin, version, err
658659
}
659660
shimPath := shims.Path(conf, command)
@@ -725,7 +726,7 @@ func pluginAddCommand(_ *cli.Context, conf config.Config, logger *log.Logger, pl
725726
return nil
726727
}
727728

728-
os.Exit(1)
729+
cli.OsExiter(1)
729730
return nil
730731
}
731732

@@ -736,7 +737,7 @@ func pluginAddCommand(_ *cli.Context, conf config.Config, logger *log.Logger, pl
736737
func pluginRemoveCommand(_ *cli.Context, logger *log.Logger, pluginName string) error {
737738
if pluginName == "" {
738739
logger.Print("No plugin given")
739-
os.Exit(1)
740+
cli.OsExiter(1)
740741
return nil
741742
}
742743

@@ -757,7 +758,7 @@ func pluginRemoveCommand(_ *cli.Context, logger *log.Logger, pluginName string)
757758
err2 := shims.RemoveAll(conf)
758759
if err2 != nil {
759760
logger.Printf("%s", err2)
760-
os.Exit(1)
761+
cli.OsExiter(1)
761762
return err2
762763
}
763764

@@ -818,7 +819,7 @@ func pluginListAllCommand(logger *log.Logger) error {
818819
}
819820
if disableRepo {
820821
logger.Printf("Short-name plugin repository is disabled")
821-
os.Exit(1)
822+
cli.OsExiter(1)
822823
return nil
823824
}
824825

@@ -881,26 +882,26 @@ func helpCommand(logger *log.Logger, asdfVersion, tool, version string) error {
881882
if version != "" {
882883
err := help.PrintToolVersion(conf, tool, version)
883884
if err != nil {
884-
os.Exit(1)
885+
cli.OsExiter(1)
885886
}
886887
return err
887888
}
888889

889890
err := help.PrintTool(conf, tool)
890891
if err != nil {
891-
os.Exit(1)
892+
cli.OsExiter(1)
892893
}
893894
return err
894895
}
895896

896897
allPlugins, err := plugins.List(conf, false, false)
897898
if err != nil {
898-
os.Exit(1)
899+
cli.OsExiter(1)
899900
}
900901

901902
err = help.Print(asdfVersion, allPlugins)
902903
if err != nil {
903-
os.Exit(1)
904+
cli.OsExiter(1)
904905
}
905906

906907
return err
@@ -943,7 +944,7 @@ func pluginTestCommand(l *log.Logger, args []string, toolVersion, ref string) {
943944
conf, err := config.LoadConfig()
944945
if err != nil {
945946
l.Printf("error loading config: %s", err)
946-
os.Exit(1)
947+
cli.OsExiter(1)
947948
return
948949
}
949950

@@ -1039,7 +1040,7 @@ func pluginTestCommand(l *log.Logger, args []string, toolVersion, ref string) {
10391040

10401041
func failTest(logger *log.Logger, msg string) {
10411042
logger.Printf("FAILED: %s", msg)
1042-
os.Exit(1)
1043+
cli.OsExiter(1)
10431044
}
10441045

10451046
func formatUpdateResult(logger *log.Logger, pluginName, updatedToRef string, err error) {
@@ -1098,7 +1099,7 @@ func installCommand(logger *log.Logger, toolName, version string, keepDownload b
10981099

10991100
if _, ok := err.(versions.NoVersionSetError); ok {
11001101
logger.Printf("No versions specified for %s in config files or environment", toolName)
1101-
os.Exit(1)
1102+
cli.OsExiter(1)
11021103
}
11031104

11041105
logger.Printf("error installing version: %v", err)
@@ -1156,7 +1157,7 @@ func latestCommand(logger *log.Logger, all bool, toolName, pattern string) (err
11561157
if !all {
11571158
err = latestForPlugin(conf, toolName, pattern, false)
11581159
if err != nil {
1159-
os.Exit(1)
1160+
cli.OsExiter(1)
11601161
}
11611162

11621163
return err
@@ -1178,7 +1179,7 @@ func latestCommand(logger *log.Logger, all bool, toolName, pattern string) (err
11781179
}
11791180

11801181
if err != nil {
1181-
os.Exit(1)
1182+
cli.OsExiter(1)
11821183
return maybeErr
11831184
}
11841185
return nil
@@ -1203,13 +1204,13 @@ func listCommand(logger *log.Logger, first, second, third string) (err error) {
12031204
func listAllCommand(logger *log.Logger, conf config.Config, toolName, filter string) error {
12041205
if toolName == "" {
12051206
logger.Print("No plugin given")
1206-
os.Exit(1)
1207+
cli.OsExiter(1)
12071208
return nil
12081209
}
12091210

12101211
plugin, err := loadPlugin(logger, conf, toolName)
12111212
if err != nil {
1212-
os.Exit(1)
1213+
cli.OsExiter(1)
12131214
return err
12141215
}
12151216

@@ -1223,7 +1224,7 @@ func listAllCommand(logger *log.Logger, conf config.Config, toolName, filter str
12231224
os.Stderr.WriteString(stderr.String())
12241225
os.Stderr.WriteString(stdout.String())
12251226

1226-
os.Exit(1)
1227+
cli.OsExiter(1)
12271228
return err
12281229
}
12291230

@@ -1235,7 +1236,7 @@ func listAllCommand(logger *log.Logger, conf config.Config, toolName, filter str
12351236

12361237
if len(versions) == 0 {
12371238
logger.Printf("No compatible versions available (%s %s)", plugin.Name, filter)
1238-
os.Exit(1)
1239+
cli.OsExiter(1)
12391240
return nil
12401241
}
12411242

@@ -1266,7 +1267,7 @@ func listLocalCommand(logger *log.Logger, conf config.Config, pluginName, filter
12661267
if pluginName != "" {
12671268
plugin, err := loadPlugin(logger, conf, pluginName)
12681269
if err != nil {
1269-
os.Exit(1)
1270+
cli.OsExiter(1)
12701271
return err
12711272
}
12721273
versions, _ := installs.Installed(conf, plugin)
@@ -1277,13 +1278,13 @@ func listLocalCommand(logger *log.Logger, conf config.Config, pluginName, filter
12771278

12781279
if len(versions) == 0 {
12791280
logger.Printf("No compatible versions installed (%s %s)", plugin.Name, filter)
1280-
os.Exit(1)
1281+
cli.OsExiter(1)
12811282
return nil
12821283
}
12831284

12841285
currentVersions, _, err := resolve.Version(conf, plugin, currentDir)
12851286
if err != nil {
1286-
os.Exit(1)
1287+
cli.OsExiter(1)
12871288
return err
12881289
}
12891290

@@ -1310,7 +1311,7 @@ func listLocalCommand(logger *log.Logger, conf config.Config, pluginName, filter
13101311
if len(versions) > 0 {
13111312
currentVersions, _, err := resolve.Version(conf, plugin, currentDir)
13121313
if err != nil {
1313-
os.Exit(1)
1314+
cli.OsExiter(1)
13141315
return err
13151316
}
13161317
for _, version := range versions {
@@ -1341,7 +1342,7 @@ func reshimCommand(logger *log.Logger, tool, version string) (err error) {
13411342
plugin = plugins.New(conf, tool)
13421343
if err := plugin.Exists(); err != nil {
13431344
logger.Printf("No such plugin: %s", plugin.Name)
1344-
os.Exit(1)
1345+
cli.OsExiter(1)
13451346
return err
13461347
}
13471348
}
@@ -1425,22 +1426,22 @@ func whichCommand(logger *log.Logger, command string) error {
14251426
func uninstallCommand(logger *log.Logger, tool, version string) error {
14261427
if tool == "" || version == "" {
14271428
logger.Print("No plugin given")
1428-
os.Exit(1)
1429+
cli.OsExiter(1)
14291430
return nil
14301431
}
14311432

14321433
conf, err := config.LoadConfig()
14331434
if err != nil {
14341435
logger.Printf("error loading config: %s", err)
1435-
os.Exit(1)
1436+
cli.OsExiter(1)
14361437
return err
14371438
}
14381439

14391440
plugin := plugins.New(conf, tool)
14401441
err = versions.Uninstall(conf, plugin, version, os.Stdout, os.Stderr)
14411442
if err != nil {
14421443
logger.Printf("%s", err)
1443-
os.Exit(1)
1444+
cli.OsExiter(1)
14441445
return err
14451446
}
14461447

@@ -1449,7 +1450,7 @@ func uninstallCommand(logger *log.Logger, tool, version string) error {
14491450
err = shims.RemoveAll(conf)
14501451
if err != nil {
14511452
logger.Printf("%s", err)
1452-
os.Exit(1)
1453+
cli.OsExiter(1)
14531454
return err
14541455
}
14551456

test/non_existent_command.bats

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env bats
2+
3+
load test_helpers
4+
5+
setup() {
6+
setup_asdf_dir
7+
8+
PROJECT_DIR="$HOME/project"
9+
mkdir -p "$PROJECT_DIR"
10+
}
11+
12+
@test "should show help when no valid command is provided" {
13+
cd "$PROJECT_DIR"
14+
15+
run asdf non-existent-command
16+
17+
[ "$status" -eq 1 ]
18+
[[ $output == 'invalid command provided:'* ]]
19+
[[ $output == *$'version: v'* ]]
20+
[[ $output == *$'MANAGE PLUGINS\n'* ]]
21+
[[ $output == *$'MANAGE TOOLS\n'* ]]
22+
[[ $output == *$'UTILS\n'* ]]
23+
[[ $output == *$'"Late but latest"\n-- Rajinikanth' ]]
24+
}

0 commit comments

Comments
 (0)