gitee.com/mirrors/Hugo-Go@v0.47.1/commands/genautocomplete.go (about) 1 // Copyright 2015 The Hugo Authors. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package commands 15 16 import ( 17 "github.com/spf13/cobra" 18 jww "github.com/spf13/jwalterweatherman" 19 ) 20 21 var _ cmder = (*genautocompleteCmd)(nil) 22 23 type genautocompleteCmd struct { 24 autocompleteTarget string 25 26 // bash for now (zsh and others will come) 27 autocompleteType string 28 29 *baseCmd 30 } 31 32 func newGenautocompleteCmd() *genautocompleteCmd { 33 cc := &genautocompleteCmd{} 34 35 cc.baseCmd = newBaseCmd(&cobra.Command{ 36 Use: "autocomplete", 37 Short: "Generate shell autocompletion script for Hugo", 38 Long: `Generates a shell autocompletion script for Hugo. 39 40 NOTE: The current version supports Bash only. 41 This should work for *nix systems with Bash installed. 42 43 By default, the file is written directly to /etc/bash_completion.d 44 for convenience, and the command may need superuser rights, e.g.: 45 46 $ sudo hugo gen autocomplete 47 48 Add ` + "`--completionfile=/path/to/file`" + ` flag to set alternative 49 file-path and name. 50 51 Logout and in again to reload the completion scripts, 52 or just source them in directly: 53 54 $ . /etc/bash_completion`, 55 56 RunE: func(cmd *cobra.Command, args []string) error { 57 if cc.autocompleteType != "bash" { 58 return newUserError("Only Bash is supported for now") 59 } 60 61 err := cmd.Root().GenBashCompletionFile(cc.autocompleteTarget) 62 63 if err != nil { 64 return err 65 } 66 67 jww.FEEDBACK.Println("Bash completion file for Hugo saved to", cc.autocompleteTarget) 68 69 return nil 70 }, 71 }) 72 73 cc.cmd.PersistentFlags().StringVarP(&cc.autocompleteTarget, "completionfile", "", "/etc/bash_completion.d/hugo.sh", "autocompletion file") 74 cc.cmd.PersistentFlags().StringVarP(&cc.autocompleteType, "type", "", "bash", "autocompletion type (currently only bash supported)") 75 76 // For bash-completion 77 cc.cmd.PersistentFlags().SetAnnotation("completionfile", cobra.BashCompFilenameExt, []string{}) 78 79 return cc 80 }