diff --git a/cmd/completion.go b/cmd/completion.go new file mode 100644 index 00000000..bb106c06 --- /dev/null +++ b/cmd/completion.go @@ -0,0 +1,91 @@ +/* +Copyright © 2021 NAME HERE + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package cmd + +import ( + "go.uber.org/zap" + "os" + + "github.com/spf13/cobra" +) + +// completionCmd represents the completion command +var completionCmd = &cobra.Command{ + Use: "completion [bash|zsh|fish|powershell]", + Short: "Generate completion script", + Long: `To load completions: + +Bash: + +$ source <(gebug completion bash) + +# To load completions for each session, execute once: +Linux: + $ gebug completion bash > /etc/bash_completion.d/gebug +MacOS: + $ gebug completion bash > /usr/local/etc/bash_completion.d/gebug + +Zsh: + +# If shell completion is not already enabled in your environment you will need +# to enable it. You can execute the following once: + +$ echo "autoload -U compinit; compinit" >> ~/.zshrc + +# To load completions for each session, execute once: +$ gebug completion zsh > "${fpath[1]}/_gebug" + +# You will need to start a new shell for this setup to take effect. + +Fish: + +$ gebug completion fish | source + +# To load completions for each session, execute once: +$ gebug completion fish > ~/.config/fish/completions/gebug.fish + +Powershell: + +PS> gebug completion powershell | Out-String | Invoke-Expression + +# To load completions for every new session, run: +PS> gebug completion powershell > gebug.ps1 +# and source this file from your powershell profile. +`, + DisableFlagsInUseLine: true, + ValidArgs: []string{"bash", "zsh", "fish", "powershell"}, + Args: cobra.ExactValidArgs(1), + Run: func(cmd *cobra.Command, args []string) { + var err error + switch args[0] { + case "bash": + err = cmd.Root().GenBashCompletion(os.Stdout) + case "zsh": + err = cmd.Root().GenZshCompletion(os.Stdout) + case "fish": + err = cmd.Root().GenFishCompletion(os.Stdout, true) + case "powershell": + err = cmd.Root().GenPowerShellCompletion(os.Stdout) + } + if err != nil { + zap.L().Error("Failed to generate completion", zap.String("shell", args[0]), zap.Error(err)) + } + }, +} + +func init() { + rootCmd.AddCommand(completionCmd) +}