Skip to content

Commit

Permalink
chore(cmd): Handle flag completion registration errors and remove `co…
Browse files Browse the repository at this point in the history
…bra.FixedCompletion`
  • Loading branch information
gabe565 committed Mar 25, 2024
1 parent f4a8ca9 commit 496a75f
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions cmd/gonesutil/ls/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,32 @@ func New() *cobra.Command {
Aliases: []string{"list"},
RunE: run,

ValidArgsFunction: cobra.FixedCompletions([]string{"nes"}, cobra.ShellCompDirectiveFilterFileExt),
ValidArgsFunction: func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return []string{"nes"}, cobra.ShellCompDirectiveFilterFileExt
},
}
cmd.Flags().StringP("output", "o", "table", "Output format. One of: (table, json, yaml)")
_ = cmd.RegisterFlagCompletionFunc(
"output",
cobra.FixedCompletions([]string{"table", "json", "yaml"}, cobra.ShellCompDirectiveNoFileComp),
)
if err := cmd.RegisterFlagCompletionFunc("output",
func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return []string{"table", "json", "yaml"}, cobra.ShellCompDirectiveNoFileComp
},
); err != nil {
panic(err)
}

cmd.Flags().StringToStringP("filter", "f", map[string]string{}, "Filter by a field")
_ = cmd.RegisterFlagCompletionFunc("filter", completeFilter)
if err := cmd.RegisterFlagCompletionFunc("filter", completeFilter); err != nil {
panic(err)
}

cmd.Flags().StringP("sort", "s", PathField, "Sort by a field")
_ = cmd.RegisterFlagCompletionFunc(
"sort",
cobra.FixedCompletions([]string{PathField, NameField, MapperField, BatteryField, MirrorField}, cobra.ShellCompDirectiveNoFileComp),
)
if err := cmd.RegisterFlagCompletionFunc("sort",
func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return []string{PathField, NameField, MapperField, BatteryField, MirrorField}, cobra.ShellCompDirectiveNoFileComp
},
); err != nil {
panic(err)
}

cmd.Flags().BoolP("reverse", "r", false, "Reverse the output")
return cmd
Expand Down

0 comments on commit 496a75f

Please sign in to comment.