2020-06-11 08:27:01 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var shellVariant = "bash"
|
|
|
|
|
|
|
|
func createBashCompletionCmd(rootCmd *cobra.Command) *cobra.Command {
|
|
|
|
var completionCmd = &cobra.Command{
|
|
|
|
Use: "shell-completion",
|
|
|
|
Short: "Generates shell completion scripts",
|
2020-06-11 08:50:38 +00:00
|
|
|
Long: `To load completion for:
|
|
|
|
bash:
|
|
|
|
Run
|
2020-06-11 23:30:05 +00:00
|
|
|
. <(yq shell-completion)
|
2020-06-11 08:27:01 +00:00
|
|
|
|
2020-06-11 08:50:38 +00:00
|
|
|
To configure your bash shell to load completions for each session add to
|
|
|
|
your bashrc
|
2020-06-11 08:27:01 +00:00
|
|
|
|
|
|
|
# ~/.bashrc or ~/.profile
|
2020-06-11 23:30:05 +00:00
|
|
|
. <(yq shell-completion)
|
2020-06-11 08:50:38 +00:00
|
|
|
|
|
|
|
zsh:
|
|
|
|
The generated completion script should be put somewhere in your $fpath named _yq
|
|
|
|
|
|
|
|
powershell:
|
|
|
|
Users need PowerShell version 5.0 or above, which comes with Windows 10 and
|
|
|
|
can be downloaded separately for Windows 7 or 8.1. They can then write the
|
|
|
|
completions to a file and source this file from their PowerShell profile,
|
|
|
|
which is referenced by the $Profile environment variable.
|
|
|
|
|
|
|
|
fish:
|
|
|
|
Save the output to a fish file and add it to your completions directory.
|
|
|
|
|
2020-06-11 08:27:01 +00:00
|
|
|
`,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
switch shellVariant {
|
|
|
|
case "bash", "":
|
2020-06-11 08:30:45 +00:00
|
|
|
return rootCmd.GenBashCompletion(os.Stdout)
|
2020-06-11 08:27:01 +00:00
|
|
|
case "zsh":
|
2020-06-11 08:30:45 +00:00
|
|
|
return rootCmd.GenZshCompletion(os.Stdout)
|
2020-06-11 08:27:01 +00:00
|
|
|
case "fish":
|
2020-06-11 08:30:45 +00:00
|
|
|
return rootCmd.GenFishCompletion(os.Stdout, true)
|
2020-06-11 08:27:01 +00:00
|
|
|
case "powershell":
|
2020-06-11 08:30:45 +00:00
|
|
|
return rootCmd.GenPowerShellCompletion(os.Stdout)
|
2020-06-11 08:27:01 +00:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("Unknown variant %v", shellVariant)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
completionCmd.PersistentFlags().StringVarP(&shellVariant, "variation", "V", "", "shell variation: bash (default), zsh, fish, powershell")
|
|
|
|
return completionCmd
|
|
|
|
}
|