Skip to content

Commit

Permalink
chore: Move shell completion flag to cobrax library
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Nov 16, 2024
1 parent 3dacf24 commit 4815bad
Show file tree
Hide file tree
Showing 9 changed files with 14 additions and 107 deletions.
6 changes: 1 addition & 5 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func New(opts ...cobrax.Option) *cobra.Command {

config.InitLog(slog.LevelInfo)
conf := config.New()
conf.RegisterFlags(cmd.Flags())
conf.RegisterFlags(cmd)
must.Must(config.RegisterCompletion(cmd))
cmd.SetContext(config.NewContext(context.Background(), conf))

Expand All @@ -59,10 +59,6 @@ func run(cmd *cobra.Command, args []string) error {
panic("command missing config")
}

if conf.Completion != "" {
return completion(cmd, conf.Completion)
}

slog.Info("cli-of-life", "version", cobrax.GetVersion(cmd), "commit", cobrax.GetCommit(cmd))

if len(args) == 1 {
Expand Down
26 changes: 0 additions & 26 deletions cmd/completion.go

This file was deleted.

47 changes: 0 additions & 47 deletions cmd/completion_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion docs/cli-of-life.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ cli-of-life [file | url] [flags]

```
--cache-limit int Maximum number of entries to keep cached. Higher values will use more memory, but less CPU. (default 10000000)
--completion string Output command-line completion code for the specified shell (one of: bash, zsh, fish, powershell)
--completion string Generate the autocompletion script for the specified shell (one of bash, zsh, fish, powershell)
-h, --help help for cli-of-life
--play Play on startup
--rule-string string Rule string to use. This will be ignored if a pattern file is loaded. (default "B3/S23")
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module gabe565.com/cli-of-life
go 1.23.3

require (
gabe565.com/utils v0.0.0-20241113080130-5627e2628c07
gabe565.com/utils v0.0.0-20241116061915-abe2278ecd5c
github.com/PuerkitoBio/goquery v1.10.0
github.com/charmbracelet/bubbles v0.20.0
github.com/charmbracelet/bubbletea v1.2.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
gabe565.com/utils v0.0.0-20241113080130-5627e2628c07 h1:1TGslYPWSlhS6i55gimuXFKPsGNdYY/58tQaXfmsfmM=
gabe565.com/utils v0.0.0-20241113080130-5627e2628c07/go.mod h1:1WioSVukwGZYG4Q0LJBnRhgYyVljmW2Izl+RW36ALUc=
gabe565.com/utils v0.0.0-20241116061915-abe2278ecd5c h1:1rmGsS/Sbm85ZELLkfOtXJ99v3YHdmqvkhMDLteLUug=
gabe565.com/utils v0.0.0-20241116061915-abe2278ecd5c/go.mod h1:1WioSVukwGZYG4Q0LJBnRhgYyVljmW2Izl+RW36ALUc=
github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ=
github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE=
github.com/PuerkitoBio/goquery v1.10.0 h1:6fiXdLuUvYs2OJSvNRqlNPoBm6YABE226xrbavY5Wv4=
Expand Down
16 changes: 0 additions & 16 deletions internal/config/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,6 @@ import (
"github.com/spf13/cobra"
)

const (
ShellBash = "bash"
ShellZsh = "zsh"
ShellFish = "fish"
ShellPowerShell = "powershell"
)

func shells() []string {
return []string{ShellBash, ShellZsh, ShellFish, ShellPowerShell}
}

func RegisterCompletion(cmd *cobra.Command) error {
return errors.Join(
cmd.RegisterFlagCompletionFunc(RuleStringFlag,
Expand All @@ -27,10 +16,5 @@ func RegisterCompletion(cmd *cobra.Command) error {
),
cmd.RegisterFlagCompletionFunc(PlayFlag, cobra.NoFileCompletions),
cmd.RegisterFlagCompletionFunc(CacheLimitFlag, cobra.NoFileCompletions),
cmd.RegisterFlagCompletionFunc(CompletionFlag,
func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return shells(), cobra.ShellCompDirectiveNoFileComp
},
),
)
}
10 changes: 5 additions & 5 deletions internal/config/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,29 @@ package config
import (
"bytes"
"log/slog"
"strings"

"gabe565.com/utils/cobrax"
"gabe565.com/utils/must"
"github.com/spf13/pflag"
"github.com/spf13/cobra"
)

const (
RuleStringFlag = "rule-string"
PlayFlag = "play"
CacheLimitFlag = "cache-limit"
CompletionFlag = "completion"

// Deprecated: Pass file as positional argument instead
FileFlag = "file"
// Deprecated: Pass URL as positional argument instead
URLFlag = "url"
)

func (c *Config) RegisterFlags(fs *pflag.FlagSet) {
func (c *Config) RegisterFlags(cmd *cobra.Command) {
must.Must(cobrax.RegisterCompletionFlag(cmd))
fs := cmd.Flags()
fs.StringVar(&c.RuleString, RuleStringFlag, c.RuleString, "Rule string to use. This will be ignored if a pattern file is loaded.")
fs.BoolVar(&c.Play, PlayFlag, c.Play, "Play on startup")
fs.IntVar(&c.CacheLimit, CacheLimitFlag, c.CacheLimit, "Maximum number of entries to keep cached. Higher values will use more memory, but less CPU.")
fs.StringVar(&c.Completion, CompletionFlag, c.Completion, "Output command-line completion code for the specified shell (one of: "+strings.Join(shells(), ", ")+")")

fs.StringVarP(&c.Pattern, FileFlag, "f", c.Pattern, "Load a pattern file")
fs.StringVar(&c.Pattern, URLFlag, c.Pattern, "Load a pattern URL")
Expand Down
8 changes: 4 additions & 4 deletions internal/generate/completions/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"gabe565.com/cli-of-life/cmd"
"gabe565.com/utils/cobrax"
flag "github.com/spf13/pflag"
)

Expand Down Expand Up @@ -39,13 +40,12 @@ func main() {
var buf bytes.Buffer
rootCmd.SetOut(&buf)

for _, shell := range []string{"bash", "zsh", "fish"} {
rootCmd.SetArgs([]string{"--completion=" + shell})
if err := rootCmd.Execute(); err != nil {
for _, shell := range []cobrax.Shell{cobrax.Bash, cobrax.Zsh, cobrax.Fish} {
if err := cobrax.GenCompletion(rootCmd, shell); err != nil {
panic(err)
}

f, err := os.Create(filepath.Join("completions", name+"."+shell))
f, err := os.Create(filepath.Join("completions", name+"."+string(shell)))
if err != nil {
panic(err)
}
Expand Down

0 comments on commit 4815bad

Please sign in to comment.