From f45dfc2f37a9fe88f5cb4f12a50c61e04f800b43 Mon Sep 17 00:00:00 2001 From: Ben Mezger Date: Wed, 6 Jan 2021 10:19:11 -0300 Subject: [PATCH 1/3] Feat: Add zsh, fish and powershell completion support See issue #4296 --- commands/genautocomplete.go | 30 ++++++++++++------- .../en/commands/hugo_gen_autocomplete.md | 14 ++++----- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/commands/genautocomplete.go b/commands/genautocomplete.go index e8d9890cfbc..b72e5a93054 100644 --- a/commands/genautocomplete.go +++ b/commands/genautocomplete.go @@ -23,7 +23,7 @@ var _ cmder = (*genautocompleteCmd)(nil) type genautocompleteCmd struct { autocompleteTarget string - // bash for now (zsh and others will come) + // bash or zsh autocompleteType string *baseCmd @@ -37,9 +37,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.: @@ -48,29 +45,40 @@ 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, fish or powershell}`" + ` 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 + switch cc.autocompleteType { + case "zsh": + err = cmd.Root().GenZshCompletionFile(cc.autocompleteTarget) + case "bash": + err = cmd.Root().GenBashCompletionFile(cc.autocompleteTarget) + case "fish": + err = cmd.Root().GenFishCompletionFile(cc.autocompleteTarget, true) + case "powershell": + err = cmd.Root().GenFishCompletionFile(cc.autocompleteTarget, 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) - + 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)") + cc.cmd.PersistentFlags().StringVarP(&cc.autocompleteType, "type", "", "bash", "autocompletion type (zsh, bash, fish or powershell)") // For bash-completion cc.cmd.PersistentFlags().SetAnnotation("completionfile", cobra.BashCompFilenameExt, []string{}) diff --git a/docs/content/en/commands/hugo_gen_autocomplete.md b/docs/content/en/commands/hugo_gen_autocomplete.md index 377a5f203a3..fe391630962 100644 --- a/docs/content/en/commands/hugo_gen_autocomplete.md +++ b/docs/content/en/commands/hugo_gen_autocomplete.md @@ -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/ @@ -12,9 +12,6 @@ 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 for convenience, and the command may need superuser rights, e.g.: @@ -23,10 +20,13 @@ 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, fish or powershell}` 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 ``` hugo gen autocomplete [flags] @@ -37,7 +37,7 @@ hugo gen autocomplete [flags] ``` --completionfile string autocompletion file (default "/etc/bash_completion.d/hugo.sh") -h, --help help for autocomplete - --type string autocompletion type (currently only bash supported) (default "bash") + --type string autocompletion type (zsh, bash, fish or powershell) (default "bash") ``` ### Options inherited from parent commands @@ -62,4 +62,4 @@ hugo gen autocomplete [flags] * [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 From e014523b3cf9e4cce09cfd55b20618ba48c6697a Mon Sep 17 00:00:00 2001 From: Ben Mezger Date: Tue, 2 Feb 2021 20:02:53 -0300 Subject: [PATCH 2/3] Refactor: Remove powershell support --- commands/genautocomplete.go | 8 ++------ docs/content/en/commands/hugo_gen_autocomplete.md | 4 ++-- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/commands/genautocomplete.go b/commands/genautocomplete.go index b72e5a93054..044a052e69f 100644 --- a/commands/genautocomplete.go +++ b/commands/genautocomplete.go @@ -22,10 +22,8 @@ var _ cmder = (*genautocompleteCmd)(nil) type genautocompleteCmd struct { autocompleteTarget string - - // bash or zsh + // bash, zsh or fish autocompleteType string - *baseCmd } @@ -45,7 +43,7 @@ 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, fish or powershell}`" + ` flag to set alternative +Add ` + "`--type={bash, zsh or fish}`" + ` flag to set alternative shell type. Logout and in again to reload the completion scripts, @@ -62,8 +60,6 @@ or just source them in directly: err = cmd.Root().GenBashCompletionFile(cc.autocompleteTarget) case "fish": err = cmd.Root().GenFishCompletionFile(cc.autocompleteTarget, true) - case "powershell": - err = cmd.Root().GenFishCompletionFile(cc.autocompleteTarget, true) default: return newUserError("Unsupported completion type") } diff --git a/docs/content/en/commands/hugo_gen_autocomplete.md b/docs/content/en/commands/hugo_gen_autocomplete.md index fe391630962..033e4fa8203 100644 --- a/docs/content/en/commands/hugo_gen_autocomplete.md +++ b/docs/content/en/commands/hugo_gen_autocomplete.md @@ -20,7 +20,7 @@ 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, fish or powershell}` flag to set alternative +Add `--type={bash, zsh or fish}` flag to set alternative shell type. Logout and in again to reload the completion scripts, @@ -37,7 +37,7 @@ hugo gen autocomplete [flags] ``` --completionfile string autocompletion file (default "/etc/bash_completion.d/hugo.sh") -h, --help help for autocomplete - --type string autocompletion type (zsh, bash, fish or powershell) (default "bash") + --type string autocompletion type (zsh, bash or fish) (default "bash") ``` ### Options inherited from parent commands From e97f661033146429c674324fcd9a878dc26fd6ce Mon Sep 17 00:00:00 2001 From: Ben Mezger Date: Tue, 2 Feb 2021 20:03:31 -0300 Subject: [PATCH 3/3] Refactor: Write to stdout by default --- commands/genautocomplete.go | 28 +++++++++++++------ .../en/commands/hugo_gen_autocomplete.md | 10 +++---- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/commands/genautocomplete.go b/commands/genautocomplete.go index 044a052e69f..3f3bf9c8784 100644 --- a/commands/genautocomplete.go +++ b/commands/genautocomplete.go @@ -14,6 +14,9 @@ package commands import ( + "io" + "os" + "github.com/spf13/cobra" jww "github.com/spf13/jwalterweatherman" ) @@ -53,13 +56,21 @@ or just source them in directly: RunE: func(cmd *cobra.Command, args []string) error { 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().GenZshCompletionFile(cc.autocompleteTarget) + err = cmd.Root().GenZshCompletion(target) case "bash": - err = cmd.Root().GenBashCompletionFile(cc.autocompleteTarget) + err = cmd.Root().GenBashCompletion(target) case "fish": - err = cmd.Root().GenFishCompletionFile(cc.autocompleteTarget, true) + err = cmd.Root().GenFishCompletion(target, true) default: return newUserError("Unsupported completion type") } @@ -68,16 +79,15 @@ or just source them in directly: return err } - jww.FEEDBACK.Println(cc.autocompleteType+" 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 (zsh, bash, fish or powershell)") - - // 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 } diff --git a/docs/content/en/commands/hugo_gen_autocomplete.md b/docs/content/en/commands/hugo_gen_autocomplete.md index 033e4fa8203..9fbfac9554e 100644 --- a/docs/content/en/commands/hugo_gen_autocomplete.md +++ b/docs/content/en/commands/hugo_gen_autocomplete.md @@ -12,10 +12,10 @@ Generate shell autocompletion script for Hugo Generates a shell autocompletion script for Hugo. -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. @@ -26,7 +26,7 @@ shell type. Logout and in again to reload the completion scripts, or just source them in directly: - $ . /etc/bash_completion or /path/to/file + $ . /path/to/file ``` hugo gen autocomplete [flags] @@ -35,7 +35,7 @@ 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 (zsh, bash or fish) (default "bash") ``` @@ -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 6-Jan-2021