Skip to content

Commit

Permalink
Merge pull request #140 from scop/feat/completion-improvements
Browse files Browse the repository at this point in the history
Various completion improvements
  • Loading branch information
nao1215 authored Apr 8, 2024
2 parents 9f70b68 + f5ddf3f commit 3742bde
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 13 deletions.
10 changes: 6 additions & 4 deletions cmd/bug_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import (

func newBugReportCmd() *cobra.Command {
return &cobra.Command{
Use: "bug-report",
Short: "Submit a bug report at GitHub",
Long: "bug-report opens the default browser to start a bug report which will include useful system information.",
Example: " gup bug-report",
Use: "bug-report",
Short: "Submit a bug report at GitHub",
Long: "bug-report opens the default browser to start a bug report which will include useful system information.",
Example: " gup bug-report",
Args: cobra.NoArgs,
ValidArgsFunction: cobra.NoFileCompletions,
Run: func(cmd *cobra.Command, args []string) {
OsExit(bugReport(cmd, args, openBrowser))
},
Expand Down
5 changes: 5 additions & 0 deletions cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,17 @@ 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))
},
}

cmd.Flags().IntP("jobs", "j", runtime.NumCPU(), "Specify the number of CPU cores to use")
if err := cmd.RegisterFlagCompletionFunc("jobs", completeNCPUs); err != nil {
panic(err)
}

return cmd
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ exports the file to $XDG_CONFIG_HOME/.config/gup/gup.conf (e.g. $HOME/.config/gu
After you have placed gup.conf in the same path hierarchy on
another system, you execute import subcommand. gup start the
installation according to the contents of gup.conf.`,
Args: cobra.NoArgs,
ValidArgsFunction: cobra.NoFileCompletions,
Run: func(cmd *cobra.Command, args []string) {
OsExit(export(cmd, args))
},
Expand Down
8 changes: 8 additions & 0 deletions cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ binaries across multiple systems. After you create gup.conf by
import subcommand in another environment, you save conf-file in
$XDG_CONFIG_HOME/.config/gup/gup.conf (e.g. $HOME/.config/gup/gup.conf.)
Finally, you execute the export subcommand in this state.`,
Args: cobra.NoArgs,
ValidArgsFunction: cobra.NoFileCompletions,
Run: func(cmd *cobra.Command, args []string) {
OsExit(runImport(cmd, args))
},
Expand All @@ -29,7 +31,13 @@ Finally, you execute the export subcommand in this state.`,
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().StringP("input", "i", config.FilePath(), "specify gup.conf file path to import")
if err := cmd.MarkFlagFilename("input", "conf"); err != nil {
panic(err)
}
cmd.Flags().IntP("jobs", "j", runtime.NumCPU(), "Specify the number of CPU cores to use")
if err := cmd.RegisterFlagCompletionFunc("jobs", completeNCPUs); err != nil {
panic(err)
}

return cmd
}
Expand Down
8 changes: 5 additions & 3 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import (

func newListCmd() *cobra.Command {
return &cobra.Command{
Use: "list",
Short: "List up command name with package path and version under $GOPATH/bin or $GOBIN",
Long: `List up command name with package path and version under $GOPATH/bin or $GOBIN`,
Use: "list",
Short: "List up command name with package path and version under $GOPATH/bin or $GOBIN",
Long: `List up command name with package path and version under $GOPATH/bin or $GOBIN`,
Args: cobra.NoArgs,
ValidArgsFunction: cobra.NoFileCompletions,
Run: func(cmd *cobra.Command, args []string) {
OsExit(list(cmd, args))
},
Expand Down
10 changes: 6 additions & 4 deletions cmd/man.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ import (

func newManCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "man",
Short: "Generate man-pages under /usr/share/man/man1 (need root privilege)",
Long: `Generate man-pages under /usr/share/man/man1 (need root privilege)`,
Example: " sudo gup man",
Use: "man",
Short: "Generate man-pages under /usr/share/man/man1 (need root privilege)",
Long: `Generate man-pages under /usr/share/man/man1 (need root privilege)`,
Example: " sudo gup man",
Args: cobra.NoArgs,
ValidArgsFunction: cobra.NoFileCompletions,
Run: func(cmd *cobra.Command, args []string) {
OsExit(man(cmd, args))
},
Expand Down
1 change: 1 addition & 0 deletions cmd/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ 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),
Run: func(cmd *cobra.Command, args []string) {
OsExit(remove(cmd, args))
},
Expand Down
11 changes: 11 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package cmd

import (
"os"
"runtime"
"strconv"

"github.com/nao1215/gup/internal/assets"
"github.com/nao1215/gup/internal/completion"
Expand Down Expand Up @@ -55,3 +57,12 @@ func Execute() error {
rootCmd := newRootCmd()
return rootCmd.Execute()
}

func completeNCPUs(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
n := runtime.NumCPU()
ret := make([]string, 0, n)
for i := 1; i <= n; i++ {
ret = append(ret, strconv.FormatInt(int64(i), 10))
}
return ret, cobra.ShellCompDirectiveNoFileComp
}
9 changes: 9 additions & 0 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,18 @@ under $GOPATH/bin and automatically updates commands to the latest version.`,
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 {
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 {
panic(err)
}
// cmd.Flags().BoolP("main-all", "M", false, "update all binaries by @main or @master (delimiter: ',')")
cmd.Flags().IntP("jobs", "j", runtime.NumCPU(), "Specify the number of CPU cores to use")
if err := cmd.RegisterFlagCompletionFunc("jobs", completeNCPUs); err != nil {
panic(err)
}

return cmd
}
Expand Down
6 changes: 4 additions & 2 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import (

func newVersionCmd() *cobra.Command {
return &cobra.Command{
Use: "version",
Short: "Show " + cmdinfo.Name + " command version information",
Use: "version",
Short: "Show " + cmdinfo.Name + " command version information",
Args: cobra.NoArgs,
ValidArgsFunction: cobra.NoFileCompletions,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(cmdinfo.GetVersion())
},
Expand Down

0 comments on commit 3742bde

Please sign in to comment.