Skip to content

Commit

Permalink
feat: --role shell completions (#282)
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Alexandro Becker <[email protected]>
  • Loading branch information
caarlos0 committed Jun 3, 2024
1 parent 59edaf7 commit bf71c2a
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,9 @@ func initFlags() {
return results, cobra.ShellCompDirectiveDefault
})
}
_ = rootCmd.RegisterFlagCompletionFunc("role", func(_ *cobra.Command, _ []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return roleNames(toComplete), cobra.ShellCompDirectiveDefault
})

if config.Format && config.FormatAs == "" {
config.FormatAs = "markdown"
Expand Down Expand Up @@ -551,14 +554,20 @@ func listConversations() error {
return nil
}

func listRoles() error {
func roleNames(prefix string) []string {
roles := make([]string, 0, len(config.Roles))
for role := range config.Roles {
if prefix != "" && !strings.HasPrefix(role, prefix) {
continue
}
roles = append(roles, role)
}
slices.Sort(roles)
return roles
}

for _, role := range roles {
func listRoles() error {
for _, role := range roleNames("") {
s := role
if role == config.Role {
s = role + stdoutStyles().Timeago.Render(" (default)")
Expand Down

0 comments on commit bf71c2a

Please sign in to comment.