Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Add zsh, fish and PowerShell completion support #8122

Merged
merged 3 commits into from
Feb 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 31 additions & 17 deletions commands/genautocomplete.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
package commands

import (
"io"
"os"

"github.com/spf13/cobra"
jww "github.com/spf13/jwalterweatherman"
)
Expand All @@ -22,10 +25,8 @@ var _ cmder = (*genautocompleteCmd)(nil)

type genautocompleteCmd struct {
autocompleteTarget string

// bash for now (zsh and others will come)
// bash, zsh or fish
autocompleteType string

*baseCmd
}

Expand All @@ -37,9 +38,6 @@ func newGenautocompleteCmd() *genautocompleteCmd {
Short: "Generate shell autocompletion script for Hugo",
Long: `Generates a shell autocompletion script for Hugo.

NOTE: The current version supports Bash only.
This should work for *nix systems with Bash installed.

By default, the file is written directly to /etc/bash_completion.d
for convenience, and the command may need superuser rights, e.g.:

Expand All @@ -48,32 +46,48 @@ for convenience, and the command may need superuser rights, e.g.:
Add ` + "`--completionfile=/path/to/file`" + ` flag to set alternative
file-path and name.

Add ` + "`--type={bash, zsh or fish}`" + ` flag to set alternative
shell type.

Logout and in again to reload the completion scripts,
or just source them in directly:

$ . /etc/bash_completion`,
$ . /etc/bash_completion or /path/to/file`,

RunE: func(cmd *cobra.Command, args []string) error {
if cc.autocompleteType != "bash" {
return newUserError("Only Bash is supported for now")
var err error
var target io.Writer

if cc.autocompleteTarget == "" {
target = os.Stdout
} else {
target, _ = os.OpenFile(cc.autocompleteTarget, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
}

switch cc.autocompleteType {
case "zsh":
err = cmd.Root().GenZshCompletion(target)
case "bash":
err = cmd.Root().GenBashCompletion(target)
case "fish":
err = cmd.Root().GenFishCompletion(target, true)
default:
return newUserError("Unsupported completion type")
}

err := cmd.Root().GenBashCompletionFile(cc.autocompleteTarget)
if err != nil {
return err
}

jww.FEEDBACK.Println("Bash completion file for Hugo saved to", cc.autocompleteTarget)

if cc.autocompleteTarget != "" {
jww.FEEDBACK.Println(cc.autocompleteType+" completion file for Hugo saved to", cc.autocompleteTarget)
}
return nil
},
})

cc.cmd.PersistentFlags().StringVarP(&cc.autocompleteTarget, "completionfile", "", "/etc/bash_completion.d/hugo.sh", "autocompletion file")
cc.cmd.PersistentFlags().StringVarP(&cc.autocompleteType, "type", "", "bash", "autocompletion type (currently only bash supported)")

// For bash-completion
cc.cmd.PersistentFlags().SetAnnotation("completionfile", cobra.BashCompFilenameExt, []string{})
cc.cmd.PersistentFlags().StringVarP(&cc.autocompleteTarget, "completionfile", "f", "", "autocompletion file, defaults to stdout")
cc.cmd.PersistentFlags().StringVarP(&cc.autocompleteType, "type", "t", "bash", "autocompletion type (zsh, bash or fish)")

return cc
}
22 changes: 11 additions & 11 deletions docs/content/en/commands/hugo_gen_autocomplete.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
date: 2020-09-13
date: 2021-01-06
title: "hugo gen autocomplete"
slug: hugo_gen_autocomplete
url: /commands/hugo_gen_autocomplete/
Expand All @@ -12,21 +12,21 @@ Generate shell autocompletion script for Hugo

Generates a shell autocompletion script for Hugo.

NOTE: The current version supports Bash only.
This should work for *nix systems with Bash installed.

By default, the file is written directly to /etc/bash_completion.d
By default, the file is written directly to `stdout`
for convenience, and the command may need superuser rights, e.g.:

$ sudo hugo gen autocomplete
$ sudo hugo gen autocomplete

Add `--completionfile=/path/to/file` flag to set alternative
file-path and name.

Add `--type={bash, zsh or fish}` flag to set alternative
shell type.

Logout and in again to reload the completion scripts,
or just source them in directly:

$ . /etc/bash_completion
$ . /path/to/file

```
hugo gen autocomplete [flags]
Expand All @@ -35,9 +35,9 @@ hugo gen autocomplete [flags]
### Options

```
--completionfile string autocompletion file (default "/etc/bash_completion.d/hugo.sh")
--completionfile string autocompletion file (defaults to stdout)
-h, --help help for autocomplete
--type string autocompletion type (currently only bash supported) (default "bash")
--type string autocompletion type (zsh, bash or fish) (default "bash")
```

### Options inherited from parent commands
Expand All @@ -60,6 +60,6 @@ hugo gen autocomplete [flags]

### SEE ALSO

* [hugo gen](/commands/hugo_gen/) - A collection of several useful generators.
- [hugo gen](/commands/hugo_gen/) - A collection of several useful generators.

###### Auto generated by spf13/cobra on 13-Sep-2020
###### Auto generated by spf13/cobra on 6-Jan-2021