Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Argument validation and completion improvements #144

Merged
merged 2 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +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`,
Args: cobra.NoArgs,
ValidArgsFunction: cobra.NoFileCompletions,
ValidArgsFunction: completePathBinaries,
Run: func(cmd *cobra.Command, args []string) {
OsExit(check(cmd, args))
},
Expand Down
4 changes: 2 additions & 2 deletions cmd/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
"",
},
})
Expand All @@ -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",
"",
},
})
Expand Down
4 changes: 2 additions & 2 deletions cmd/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
"",
},
})
Expand All @@ -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",
"",
},
})
Expand Down
4 changes: 2 additions & 2 deletions cmd/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
"",
},
})
Expand All @@ -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",
"",
},
})
Expand Down
3 changes: 2 additions & 1 deletion cmd/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
},
Expand Down
25 changes: 22 additions & 3 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"os/signal"
"path/filepath"
"runtime"
"strconv"
"strings"
Expand All @@ -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: ',')")
Expand Down Expand Up @@ -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)
Expand All @@ -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
}

Expand Down Expand Up @@ -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
}
Loading