Feat: Add zsh, fish and powershell completion support
authorBen Mezger <me@benmezger.nl>
Wed, 6 Jan 2021 13:19:11 +0000 (10:19 -0300)
committerAnthony Fok <foka@debian.org>
Thu, 4 Feb 2021 22:32:51 +0000 (15:32 -0700)
See issue #4296

commands/genautocomplete.go
docs/content/en/commands/hugo_gen_autocomplete.md

index e8d9890cfbc1308f3782322317fb387b7a299468..b72e5a93054dc8f12337cf649013f7dcf36e626a 100644 (file)
@@ -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{})
index 377a5f203a35218dba45e6b14f4388fdf69ad561..fe3916309622ccb27a89cf59f882ba80b9a6f44d 100644 (file)
@@ -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