mirror of
https://github.com/k3s-io/k3s.git
synced 2026-02-03 20:39:49 -05:00
Refac shell completion to a better command structure
* Refac for shell completion Signed-off-by: Vitor Savian <vitor.savian@suse.com> * Change FLAGS to OPTIONS Signed-off-by: Vitor Savian <vitor.savian@suse.com> * Refac bash and zsh func names Signed-off-by: Vitor Savian <vitor.savian@suse.com> * Refac bash and zsh func names Signed-off-by: Vitor Savian <vitor.savian@suse.com> --------- Signed-off-by: Vitor Savian <vitor.savian@suse.com>
This commit is contained in:
parent
907d03d841
commit
66102c5651
6 changed files with 83 additions and 51 deletions
|
|
@ -14,7 +14,10 @@ import (
|
|||
func main() {
|
||||
app := cmds.NewApp()
|
||||
app.Commands = []*cli.Command{
|
||||
cmds.NewCompletionCommand(completion.Run),
|
||||
cmds.NewCompletionCommand(
|
||||
completion.Bash,
|
||||
completion.Zsh,
|
||||
),
|
||||
}
|
||||
|
||||
if err := app.Run(os.Args); err != nil && !errors.Is(err, context.Canceled) {
|
||||
|
|
|
|||
|
|
@ -87,7 +87,10 @@ func main() {
|
|||
certCommand,
|
||||
certCommand,
|
||||
),
|
||||
cmds.NewCompletionCommand(internalCLIAction(version.Program+"-completion", dataDir, os.Args)),
|
||||
cmds.NewCompletionCommand(
|
||||
internalCLIAction(version.Program+"-completion", dataDir, os.Args),
|
||||
internalCLIAction(version.Program+"-completion", dataDir, os.Args),
|
||||
),
|
||||
}
|
||||
|
||||
if err := app.Run(os.Args); err != nil && !errors.Is(err, context.Canceled) {
|
||||
|
|
|
|||
|
|
@ -77,7 +77,10 @@ func main() {
|
|||
cert.Rotate,
|
||||
cert.RotateCA,
|
||||
),
|
||||
cmds.NewCompletionCommand(completion.Run),
|
||||
cmds.NewCompletionCommand(
|
||||
completion.Bash,
|
||||
completion.Zsh,
|
||||
),
|
||||
}
|
||||
|
||||
if err := app.Run(configfilearg.MustParse(os.Args)); err != nil && !errors.Is(err, context.Canceled) {
|
||||
|
|
|
|||
5
main.go
5
main.go
|
|
@ -51,7 +51,10 @@ func main() {
|
|||
cert.Rotate,
|
||||
cert.RotateCA,
|
||||
),
|
||||
cmds.NewCompletionCommand(completion.Run),
|
||||
cmds.NewCompletionCommand(
|
||||
completion.Bash,
|
||||
completion.Zsh,
|
||||
),
|
||||
}
|
||||
|
||||
if err := app.Run(configfilearg.MustParse(os.Args)); err != nil && !errors.Is(err, context.Canceled) {
|
||||
|
|
|
|||
|
|
@ -4,16 +4,34 @@ import (
|
|||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
func NewCompletionCommand(action func(*cli.Context) error) *cli.Command {
|
||||
func NewCompletionCommand(bash, zsh func(*cli.Context) error) *cli.Command {
|
||||
installFlag := cli.BoolFlag{
|
||||
Name: "i",
|
||||
Usage: "Install source line to rc file",
|
||||
}
|
||||
|
||||
return &cli.Command{
|
||||
Name: "completion",
|
||||
Usage: "Install shell completion script",
|
||||
UsageText: appName + " completion [SHELL] (valid shells: bash, zsh)",
|
||||
Action: action,
|
||||
Flags: []cli.Flag{
|
||||
&cli.BoolFlag{
|
||||
Name: "i",
|
||||
Usage: "Install source line to rc file",
|
||||
UsageText: appName + " completion [COMMAND]",
|
||||
Subcommands: []*cli.Command{
|
||||
{
|
||||
Name: "bash",
|
||||
Usage: "Bash completion",
|
||||
UsageText: appName + " completion bash [OPTIONS]",
|
||||
Action: bash,
|
||||
Flags: []cli.Flag{
|
||||
&installFlag,
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "zsh",
|
||||
Usage: "Zsh completion",
|
||||
Action: zsh,
|
||||
UsageText: appName + " completion zsh [OPTIONS]",
|
||||
Flags: []cli.Flag{
|
||||
&installFlag,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,26 +9,8 @@ import (
|
|||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
func Run(ctx *cli.Context) error {
|
||||
if ctx.NArg() < 1 {
|
||||
return fmt.Errorf("must provide a valid SHELL argument")
|
||||
}
|
||||
shell := ctx.Args().Get(0)
|
||||
completetionScript, err := genCompletionScript(shell)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if ctx.Bool("i") {
|
||||
return writeToRC(shell)
|
||||
}
|
||||
fmt.Println(completetionScript)
|
||||
return nil
|
||||
}
|
||||
|
||||
func genCompletionScript(shell string) (string, error) {
|
||||
var completionScript string
|
||||
if shell == "bash" {
|
||||
completionScript = fmt.Sprintf(`#!/usr/bin/env bash
|
||||
var (
|
||||
bashScript = `#!/usr/bin/env bash
|
||||
_cli_bash_autocomplete() {
|
||||
if [[ "${COMP_WORDS[0]}" != "source" ]]; then
|
||||
local cur opts base
|
||||
|
|
@ -45,9 +27,9 @@ fi
|
|||
}
|
||||
|
||||
complete -o bashdefault -o default -o nospace -F _cli_bash_autocomplete %s
|
||||
`, version.Program)
|
||||
} else if shell == "zsh" {
|
||||
completionScript = fmt.Sprintf(`#compdef %[1]s
|
||||
`
|
||||
|
||||
zshScript = `#compdef %[1]s
|
||||
_cli_zsh_autocomplete() {
|
||||
|
||||
local -a opts
|
||||
|
|
@ -68,36 +50,56 @@ _cli_zsh_autocomplete() {
|
|||
return
|
||||
}
|
||||
|
||||
compdef _cli_zsh_autocomplete %[1]s`, version.Program)
|
||||
} else {
|
||||
return "", fmt.Errorf("unknown shell: %s", shell)
|
||||
}
|
||||
compdef _cli_zsh_autocomplete %[1]s`
|
||||
)
|
||||
|
||||
func Bash(ctx *cli.Context) error {
|
||||
completetionScript, err := genCompletionScript(bashScript)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if ctx.Bool("i") {
|
||||
return writeToRC("bash", "/.bashrc")
|
||||
}
|
||||
fmt.Println(completetionScript)
|
||||
return nil
|
||||
}
|
||||
|
||||
func Zsh(ctx *cli.Context) error {
|
||||
completetionScript, err := genCompletionScript(zshScript)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if ctx.Bool("i") {
|
||||
return writeToRC("zsh", "/.zshrc")
|
||||
}
|
||||
fmt.Println(completetionScript)
|
||||
return nil
|
||||
}
|
||||
|
||||
func genCompletionScript(script string) (string, error) {
|
||||
completionScript := fmt.Sprintf(script, version.Program)
|
||||
return completionScript, nil
|
||||
}
|
||||
|
||||
func writeToRC(shell string) error {
|
||||
rcFileName := ""
|
||||
if shell == "bash" {
|
||||
rcFileName = "/.bashrc"
|
||||
} else if shell == "zsh" {
|
||||
rcFileName = "/.zshrc"
|
||||
}
|
||||
|
||||
func writeToRC(shell, envFileName string) error {
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
rcFileName = home + rcFileName
|
||||
f, err := os.OpenFile(rcFileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||
|
||||
envFileName = home + envFileName
|
||||
f, err := os.OpenFile(envFileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer f.Close()
|
||||
bashEntry := fmt.Sprintf("# >> %[1]s command completion (start)\n. <(%[1]s completion %s)\n# >> %[1]s command completion (end)", version.Program, shell)
|
||||
if _, err := f.WriteString(bashEntry); err != nil {
|
||||
shellEntry := fmt.Sprintf("# >> %[1]s command completion (start)\n. <(%[1]s completion %s)\n# >> %[1]s command completion (end)", version.Program, shell)
|
||||
if _, err := f.WriteString(shellEntry); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("Autocomplete for %s added to: %s\n", shell, rcFileName)
|
||||
|
||||
fmt.Printf("Autocomplete for %s added to: %s\n", shell, envFileName)
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue