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 9feb22c commit 8a088a6
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 46 deletions.
5 changes: 0 additions & 5 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"gabe565.com/changelog-generator/internal/config"
"gabe565.com/changelog-generator/internal/git"
"gabe565.com/utils/cobrax"
"gabe565.com/utils/must"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -43,10 +42,6 @@ func New(opts ...cobrax.Option) *cobra.Command {
}

func run(cmd *cobra.Command, _ []string) error {
if shell := must.Must2(cmd.Flags().GetString(config.FlagCompletion)); shell != "" {
return completion(cmd, shell)
}

conf, err := config.Load(cmd)
if err != nil {
return err
Expand Down
25 changes: 0 additions & 25 deletions cmd/completion.go

This file was deleted.

2 changes: 1 addition & 1 deletion docs/changelog-generator.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ changelog-generator [flags]
### Options

```
--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)
--config string Config file (default ".changelog-generator.yaml")
-h, --help help for changelog-generator
-C, --repo string Path to the git repo root. Parent directories will be walked until .git is found. (default ".")
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/changelog-generator
go 1.23.3

require (
gabe565.com/utils v0.0.0-20241114234101-e128cd3269b5
gabe565.com/utils v0.0.0-20241116061915-abe2278ecd5c
github.com/go-git/go-git/v5 v5.12.0
github.com/knadh/koanf/parsers/yaml v0.1.0
github.com/knadh/koanf/providers/confmap v0.1.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk=
dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
gabe565.com/utils v0.0.0-20241114234101-e128cd3269b5 h1:uqtftvk1FMAsFPAT9ICLLntGsZHmrghgIGBClVCsVHk=
gabe565.com/utils v0.0.0-20241114234101-e128cd3269b5/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/Microsoft/go-winio v0.5.2/go.mod h1:WpS1mjBmmwHBEWmogvA2mj8546UReBk4v8QkMxJ6pZY=
github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow=
github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM=
Expand Down
13 changes: 5 additions & 8 deletions internal/config/flags.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package config

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

const (
FlagConfig = "config"
FlagRepo = "repo"
FlagCompletion = "completion"
FlagConfig = "config"
FlagRepo = "repo"
)

func RegisterFlags(cmd *cobra.Command) {
must.Must(cobrax.RegisterCompletionFlag(cmd))

cmd.Flags().String(FlagConfig, "", `Config file (default ".changelog-generator.yaml")`)
must.Must(cmd.RegisterFlagCompletionFunc(FlagConfig, func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return []string{"yaml"}, cobra.ShellCompDirectiveFilterFileExt
Expand All @@ -21,9 +23,4 @@ func RegisterFlags(cmd *cobra.Command) {
must.Must(cmd.RegisterFlagCompletionFunc(FlagRepo, func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return []string{}, cobra.ShellCompDirectiveFilterDirs
}))

cmd.Flags().String(FlagCompletion, "", "Output command-line completion code for the specified shell. (one of bash, zsh, fish, powershell)")
must.Must(cmd.RegisterFlagCompletionFunc(FlagCompletion, func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return []string{"bash", "zsh", "fish", "powershell"}, cobra.ShellCompDirectiveNoFileComp
}))
}
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/changelog-generator/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 8a088a6

Please sign in to comment.