Skip to content

Commit

Permalink
chore: Replace flag error handling with must library
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Nov 14, 2024
1 parent 836c5ad commit 3bce8e2
Showing 1 changed file with 19 additions and 23 deletions.
42 changes: 19 additions & 23 deletions internal/config/completions/completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package completions

import (
"bytes"
"errors"
"fmt"
"strconv"
"strings"
Expand All @@ -11,32 +10,29 @@ import (
"gabe565.com/pwgen/internal/config"
"gabe565.com/pwgen/internal/funcmap"
"gabe565.com/pwgen/internal/wordlist"
"gabe565.com/utils/must"
"github.com/spf13/cobra"
)

func Register(cmd *cobra.Command) {
if err := errors.Join(
cmd.RegisterFlagCompletionFunc(config.FlagConfig,
func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return []string{"toml"}, cobra.ShellCompDirectiveFilterFileExt
},
),
cmd.RegisterFlagCompletionFunc(config.FlagCount, cobra.NoFileCompletions),
cmd.RegisterFlagCompletionFunc(config.FlagWordlist,
func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
lists := wordlist.MetaValues()
values := make([]string, 0, len(lists))
for _, wl := range lists {
values = append(values, wl.String()+"\t"+strings.ReplaceAll(wl.Description(), "\n", " "))
}
return values, cobra.ShellCompDirectiveNoFileComp
},
),
cmd.RegisterFlagCompletionFunc(config.FlagTemplate, cobra.NoFileCompletions),
cmd.RegisterFlagCompletionFunc(config.FlagProfile, completeProfile),
); err != nil {
panic(err)
}
must.Must(cmd.RegisterFlagCompletionFunc(config.FlagConfig,
func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return []string{"toml"}, cobra.ShellCompDirectiveFilterFileExt
},
))
must.Must(cmd.RegisterFlagCompletionFunc(config.FlagCount, cobra.NoFileCompletions))
must.Must(cmd.RegisterFlagCompletionFunc(config.FlagWordlist,
func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
lists := wordlist.MetaValues()
values := make([]string, 0, len(lists))
for _, wl := range lists {
values = append(values, wl.String()+"\t"+strings.ReplaceAll(wl.Description(), "\n", " "))
}
return values, cobra.ShellCompDirectiveNoFileComp
},
))
must.Must(cmd.RegisterFlagCompletionFunc(config.FlagTemplate, cobra.NoFileCompletions))
must.Must(cmd.RegisterFlagCompletionFunc(config.FlagProfile, completeProfile))
}

func completeProfile(cmd *cobra.Command, _ []string, toComplete string) ([]string, cobra.ShellCompDirective) {
Expand Down

0 comments on commit 3bce8e2

Please sign in to comment.