github.com/gohugoio/hugo@v0.88.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  	"io"
    18  	"os"
    19  
    20  	"github.com/spf13/cobra"
    21  	jww "github.com/spf13/jwalterweatherman"
    22  )
    23  
    24  var _ cmder = (*genautocompleteCmd)(nil)
    25  
    26  type genautocompleteCmd struct {
    27  	autocompleteTarget string
    28  
    29  	// bash, zsh, fish or powershell
    30  	autocompleteType string
    31  
    32  	*baseCmd
    33  }
    34  
    35  func newGenautocompleteCmd() *genautocompleteCmd {
    36  	cc := &genautocompleteCmd{}
    37  
    38  	cc.baseCmd = newBaseCmd(&cobra.Command{
    39  		Use:   "autocomplete",
    40  		Short: "Generate shell autocompletion script for Hugo",
    41  		Long: `Generates a shell autocompletion script for Hugo.
    42  
    43  The script is written to the console (stdout).
    44  
    45  To write to file, add the ` + "`--completionfile=/path/to/file`" + ` flag.
    46  
    47  Add ` + "`--type={bash, zsh, fish or powershell}`" + ` flag to set alternative
    48  shell type.
    49  
    50  Logout and in again to reload the completion scripts,
    51  or just source them in directly:
    52  
    53  	$ . /etc/bash_completion or /path/to/file`,
    54  
    55  		RunE: func(cmd *cobra.Command, args []string) error {
    56  			var err error
    57  			var target io.Writer
    58  
    59  			if cc.autocompleteTarget == "" {
    60  				target = os.Stdout
    61  			} else {
    62  				target, _ = os.OpenFile(cc.autocompleteTarget, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
    63  			}
    64  
    65  			switch cc.autocompleteType {
    66  			case "bash":
    67  				err = cmd.Root().GenBashCompletion(target)
    68  			case "zsh":
    69  				err = cmd.Root().GenZshCompletion(target)
    70  			case "fish":
    71  				err = cmd.Root().GenFishCompletion(target, true)
    72  			case "powershell":
    73  				err = cmd.Root().GenPowerShellCompletion(target)
    74  			default:
    75  				return newUserError("Unsupported completion type")
    76  			}
    77  
    78  			if err != nil {
    79  				return err
    80  			}
    81  
    82  			if cc.autocompleteTarget != "" {
    83  				jww.FEEDBACK.Println(cc.autocompleteType+" completion file for Hugo saved to", cc.autocompleteTarget)
    84  			}
    85  			return nil
    86  		},
    87  	})
    88  
    89  	cc.cmd.PersistentFlags().StringVarP(&cc.autocompleteTarget, "completionfile", "f", "", "autocompletion file, defaults to stdout")
    90  	cc.cmd.PersistentFlags().StringVarP(&cc.autocompleteType, "type", "t", "bash", "autocompletion type (bash, zsh, fish, or powershell)")
    91  
    92  	return cc
    93  }