Refactor: Write to stdout by default
authorBen Mezger <me@benmezger.nl>
Tue, 2 Feb 2021 23:03:31 +0000 (20:03 -0300)
committerAnthony Fok <foka@debian.org>
Thu, 4 Feb 2021 22:32:51 +0000 (15:32 -0700)
commands/genautocomplete.go
docs/content/en/commands/hugo_gen_autocomplete.md

index 044a052e69f5d4767c03a881484f02c2f7ec80b6..3f3bf9c8784cf87120149a92e1ba53b29aabbbd3 100644 (file)
@@ -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
 }
index 033e4fa820365a96094a4f9f2335567db6ce6dcb..9fbfac9554e38955fd2de1c6b54d3c60795dbfc0 100644 (file)
@@ -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