...

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