From 9972f9a6cb02d8738e0a1f5d30d3be6dcdc66346 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Thu, 11 Apr 2024 08:47:27 +0300 Subject: [PATCH 1/2] Fix (remove) check positional argument count validation --- cmd/check.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cmd/check.go b/cmd/check.go index d6303c7..81e730a 100644 --- a/cmd/check.go +++ b/cmd/check.go @@ -22,7 +22,6 @@ func newCheckCmd() *cobra.Command { check subcommand checks if the binary is the latest version and displays the name of the binary that needs to be updated. However, do not update`, - Args: cobra.NoArgs, ValidArgsFunction: cobra.NoFileCompletions, Run: func(cmd *cobra.Command, args []string) { OsExit(check(cmd, args)) From 6e16a8c6ae3a3a544d7d5c6082a1e0e6736705af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Thu, 11 Apr 2024 08:49:23 +0300 Subject: [PATCH 2/2] Complete executable names for check, remove, and update --- cmd/check.go | 2 +- cmd/check_test.go | 4 ++-- cmd/export_test.go | 4 ++-- cmd/list_test.go | 4 ++-- cmd/remove.go | 3 ++- cmd/update.go | 25 ++++++++++++++++++++++--- 6 files changed, 31 insertions(+), 11 deletions(-) diff --git a/cmd/check.go b/cmd/check.go index 81e730a..f2c6628 100644 --- a/cmd/check.go +++ b/cmd/check.go @@ -22,7 +22,7 @@ func newCheckCmd() *cobra.Command { check subcommand checks if the binary is the latest version and displays the name of the binary that needs to be updated. However, do not update`, - ValidArgsFunction: cobra.NoFileCompletions, + ValidArgsFunction: completePathBinaries, Run: func(cmd *cobra.Command, args []string) { OsExit(check(cmd, args)) }, diff --git a/cmd/check_test.go b/cmd/check_test.go index 8195017..5404eeb 100644 --- a/cmd/check_test.go +++ b/cmd/check_test.go @@ -218,7 +218,7 @@ func Test_check_gobin_is_empty(t *testing.T) { args: args{}, want: 1, stderr: []string{ - "gup:ERROR: can't get binary-paths installed by 'go install': open no_exist_dir: The system cannot find the file specified.", + "gup:ERROR: can't get package info: can't get binary-paths installed by 'go install': open no_exist_dir: The system cannot find the file specified.", "", }, }) @@ -235,7 +235,7 @@ func Test_check_gobin_is_empty(t *testing.T) { args: args{}, want: 1, stderr: []string{ - "gup:ERROR: can't get binary-paths installed by 'go install': open no_exist_dir: no such file or directory", + "gup:ERROR: can't get package info: can't get binary-paths installed by 'go install': open no_exist_dir: no such file or directory", "", }, }) diff --git a/cmd/export_test.go b/cmd/export_test.go index e75bb4e..b5a1d84 100644 --- a/cmd/export_test.go +++ b/cmd/export_test.go @@ -210,7 +210,7 @@ func Test_export(t *testing.T) { gobin: filepath.Join("testdata", "dummy"), want: 1, stderr: []string{ - "gup:ERROR: can't get binary-paths installed by 'go install': open " + filepath.Join("testdata", "dummy") + ": The system cannot find the file specified.", + "gup:ERROR: can't get package info: can't get binary-paths installed by 'go install': open " + filepath.Join("testdata", "dummy") + ": The system cannot find the file specified.", "", }, }) @@ -231,7 +231,7 @@ func Test_export(t *testing.T) { gobin: filepath.Join("testdata", "dummy"), want: 1, stderr: []string{ - "gup:ERROR: can't get binary-paths installed by 'go install': open testdata/dummy: no such file or directory", + "gup:ERROR: can't get package info: can't get binary-paths installed by 'go install': open testdata/dummy: no such file or directory", "", }, }) diff --git a/cmd/list_test.go b/cmd/list_test.go index 2d1eeaa..eba0e29 100644 --- a/cmd/list_test.go +++ b/cmd/list_test.go @@ -92,7 +92,7 @@ func Test_list_gobin_is_empty(t *testing.T) { args: args{}, want: 1, stderr: []string{ - "gup:ERROR: can't get binary-paths installed by 'go install': open no_exist_dir: The system cannot find the file specified.", + "gup:ERROR: can't get package info: can't get binary-paths installed by 'go install': open no_exist_dir: The system cannot find the file specified.", "", }, }) @@ -109,7 +109,7 @@ func Test_list_gobin_is_empty(t *testing.T) { args: args{}, want: 1, stderr: []string{ - "gup:ERROR: can't get binary-paths installed by 'go install': open no_exist_dir: no such file or directory", + "gup:ERROR: can't get package info: can't get binary-paths installed by 'go install': open no_exist_dir: no such file or directory", "", }, }) diff --git a/cmd/remove.go b/cmd/remove.go index 75cb82e..c342c53 100644 --- a/cmd/remove.go +++ b/cmd/remove.go @@ -21,7 +21,8 @@ func newRemoveCmd() *cobra.Command { Long: `Remove command in $GOPATH/bin or $GOBIN. If you want to specify multiple binaries at once, separate them with space. [e.g.] gup remove a_cmd b_cmd c_cmd`, - Args: cobra.MinimumNArgs(1), + Args: cobra.MinimumNArgs(1), + ValidArgsFunction: completePathBinaries, Run: func(cmd *cobra.Command, args []string) { OsExit(remove(cmd, args)) }, diff --git a/cmd/update.go b/cmd/update.go index a8dd2a3..99e0bbe 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "os/signal" + "path/filepath" "runtime" "strconv" "strings" @@ -30,15 +31,16 @@ under $GOPATH/bin and automatically updates commands to the latest version.`, Run: func(cmd *cobra.Command, args []string) { OsExit(gup(cmd, args)) }, + ValidArgsFunction: completePathBinaries, } cmd.Flags().BoolP("dry-run", "n", false, "perform the trial update with no changes") cmd.Flags().BoolP("notify", "N", false, "enable desktop notifications") cmd.Flags().StringSliceP("exclude", "e", []string{}, "specify binaries which should not be updated (delimiter: ',')") - if err := cmd.RegisterFlagCompletionFunc("exclude", cobra.NoFileCompletions); err != nil { + if err := cmd.RegisterFlagCompletionFunc("exclude", completePathBinaries); err != nil { panic(err) } cmd.Flags().StringSliceP("main", "m", []string{}, "specify binaries which update by @main or @master (delimiter: ',')") - if err := cmd.RegisterFlagCompletionFunc("main", cobra.NoFileCompletions); err != nil { + if err := cmd.RegisterFlagCompletionFunc("main", completePathBinaries); err != nil { panic(err) } // cmd.Flags().BoolP("main-all", "M", false, "update all binaries by @main or @master (delimiter: ',')") @@ -237,7 +239,7 @@ func pkgDigit(pkgs []goutil.Package) string { return strconv.Itoa(len(strconv.Itoa(len(pkgs)))) } -func getPackageInfo() ([]goutil.Package, error) { +func getBinaryPathList() ([]string, error) { goBin, err := goutil.GoBin() if err != nil { return nil, fmt.Errorf("%s: %w", "can't find installed binaries", err) @@ -248,6 +250,15 @@ func getPackageInfo() ([]goutil.Package, error) { return nil, fmt.Errorf("%s: %w", "can't get binary-paths installed by 'go install'", err) } + return binList, nil +} + +func getPackageInfo() ([]goutil.Package, error) { + binList, err := getBinaryPathList() + if err != nil { + return nil, fmt.Errorf("%s: %w", "can't get package info", err) + } + return goutil.GetPackageInformation(binList), nil } @@ -288,3 +299,11 @@ func extractUserSpecifyPkg(pkgs []goutil.Package, targets []string) []goutil.Pac } return result } + +func completePathBinaries(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + binList, _ := getBinaryPathList() + for i, b := range binList { + binList[i] = filepath.Base(b) + } + return binList, cobra.ShellCompDirectiveNoFileComp +}