Skip to content

Commit

Permalink
feat(cmd): Add --completion flag
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Mar 14, 2024
1 parent d9f88f5 commit a9c4e8f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
10 changes: 10 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ func New() *cobra.Command {
DisableAutoGenTag: true,
}

registerCompletionFlag(cmd)

cmd.PersistentFlags().String("config", "", `Config file (default ".changelog-generator.yaml")`)
_ = cmd.RegisterFlagCompletionFunc("config", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{"yaml"}, cobra.ShellCompDirectiveFilterFileExt
Expand All @@ -29,6 +31,14 @@ func New() *cobra.Command {
}

func run(cmd *cobra.Command, args []string) error {
completionFlag, err := cmd.Flags().GetString(CompletionFlag)
if err != nil {
return err
}
if completionFlag != "" {
return completion(cmd, args)
}

conf, err := config.Load(cmd)
if err != nil {
return err
Expand Down
51 changes: 51 additions & 0 deletions cmd/completion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

const CompletionFlag = "completion"

func registerCompletionFlag(cmd *cobra.Command) {
cmd.Flags().String(CompletionFlag, "", "Output command-line completion code for the specified shell. Can be 'bash', 'zsh', 'fish', or 'powershell'.")
err := cmd.RegisterFlagCompletionFunc(
CompletionFlag,
func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{"bash", "zsh", "fish", "powershell"}, cobra.ShellCompDirectiveNoFileComp
},
)
if err != nil {
panic(err)
}
}

func completion(cmd *cobra.Command, args []string) error {
completionFlag, err := cmd.Flags().GetString(CompletionFlag)
if err != nil {
panic(err)
}

switch completionFlag {
case "bash":
if err := cmd.Root().GenBashCompletion(cmd.OutOrStdout()); err != nil {
return err
}
case "zsh":
if err := cmd.Root().GenZshCompletion(cmd.OutOrStdout()); err != nil {
return err
}
case "fish":
if err := cmd.Root().GenFishCompletion(cmd.OutOrStdout(), true); err != nil {
return err
}
case "powershell":
if err := cmd.Root().GenPowerShellCompletionWithDesc(cmd.OutOrStdout()); err != nil {
return err
}
default:
return fmt.Errorf("%v: invalid shell", completionFlag)
}
return nil
}
5 changes: 3 additions & 2 deletions docs/changelog-generator.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ changelog-generator [flags]
### Options

```
--config string Config file (default ".changelog-generator.yaml")
-h, --help help for changelog-generator
--completion string Output command-line completion code for the specified shell. Can be 'bash', 'zsh', 'fish', or 'powershell'.
--config string Config file (default ".changelog-generator.yaml")
-h, --help help for changelog-generator
```

0 comments on commit a9c4e8f

Please sign in to comment.