...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/chaosctl/cmd/completion.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/chaosctl/cmd

     1  // Copyright 2020 Chaos Mesh Authors.
     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  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package cmd
    15  
    16  import (
    17  	"os"
    18  
    19  	"github.com/spf13/cobra"
    20  )
    21  
    22  // completionCmd represents the completion command
    23  var completionCmd = &cobra.Command{
    24  	Use:   "completion [bash|zsh|fish|powershell]",
    25  	Short: "Generate completion script",
    26  	Long: `To load completions:
    27  
    28  Bash:
    29  
    30  $ source <(chaosctl completion bash)
    31  
    32  # To load completions for each session, execute once:
    33  Linux:
    34    $ ./bin/chaosctl completion bash > /etc/bash_completion.d/chaosctl
    35  MacOS:
    36    $ ./bin/chaosctl completion bash > /usr/local/etc/bash_completion.d/chaosctl
    37  
    38  Zsh:
    39  
    40  $ compdef _chaosctl ./bin/chaosctl
    41  
    42  # If shell completion is not already enabled in your environment you will need
    43  # to enable it.  You can execute the following once:
    44  
    45  $ echo "autoload -U compinit; compinit" >> ~/.zshrc
    46  
    47  # To load completions for each session, execute once:
    48  $ ./bin/chaosctl completion zsh > "${fpath[1]}/_chaosctl"
    49  
    50  # You will need to start a new shell for this setup to take effect.
    51  
    52  Fish:
    53  
    54  $ ./bin/chaosctl completion fish | source
    55  
    56  # To load completions for each session, execute once:
    57  $ ./bin/chaosctl completion fish > ~/.config/fish/completions/chaosctl.fish
    58  
    59  Powershell:
    60  
    61  PS> ./bin/chaosctl completion powershell | Out-String | Invoke-Expression
    62  
    63  # To load completions for every new session, run:
    64  PS> ./bin/chaosctl completion powershell > chaosctl.ps1
    65  # and source this file from your powershell profile.
    66  `,
    67  	DisableFlagsInUseLine: true,
    68  	ValidArgs:             []string{"bash", "zsh", "fish", "powershell"},
    69  	Args:                  cobra.ExactValidArgs(1),
    70  	Run: func(cmd *cobra.Command, args []string) {
    71  		switch args[0] {
    72  		case "bash":
    73  			cmd.Root().GenBashCompletion(os.Stdout)
    74  		case "zsh":
    75  			cmd.Root().GenZshCompletion(os.Stdout)
    76  		case "fish":
    77  			cmd.Root().GenFishCompletion(os.Stdout, true)
    78  		// TODO: powershell completion is still not fully supported, see https://github.com/spf13/cobra/pull/1208
    79  		// Need to update cobra version when this PR is merged
    80  		case "powershell":
    81  			cmd.Root().GenPowerShellCompletion(os.Stdout)
    82  		}
    83  	},
    84  }
    85  
    86  func init() {
    87  	rootCmd.AddCommand(completionCmd)
    88  }
    89