From 0f5147bf753d33b31ebf61453d88ddb14a053ed4 Mon Sep 17 00:00:00 2001 From: Marc Falzon Date: Wed, 26 May 2021 16:25:50 +0200 Subject: [PATCH] Add autocompletion generation for more shells This change adds shell autocompletion support for Fish, PowerShell & Zsh. --- .goreleaser.yml | 2 ++ Makefile | 8 +++++--- completion/main.go | 25 ++++++++++++++++++++++++- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/.goreleaser.yml b/.goreleaser.yml index cbd98f92e..ba3915235 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -72,6 +72,8 @@ brews: bin.install "exo" man1.install Dir["manpage/exo*.1"] bash_completion.install "contrib/completion/bash/exo" + fish_completion.install "contrib/completion/fish/exo" + zsh_completion.install "contrib/completion/zsh/exo" dockers: - goos: linux diff --git a/Makefile b/Makefile index 5c75f318d..aa4bdf96b 100644 --- a/Makefile +++ b/Makefile @@ -45,9 +45,11 @@ manpages: manpage .PHONY: completions completions: - mkdir -p contrib/completion/bash - $(GO) run -mod vendor completion/main.go - mv bash_completion contrib/completion/bash/exo + mkdir -p contrib/completion/{bash,fish,powershell,zsh} + $(GO) run -mod vendor completion/main.go bash ; mv bash_completion contrib/completion/bash/exo + $(GO) run -mod vendor completion/main.go fish ; mv fish_completion contrib/completion/fish/exo + $(GO) run -mod vendor completion/main.go powershell ; mv powershell_completion contrib/completion/powershell/exo + $(GO) run -mod vendor completion/main.go zsh ; mv zsh_completion contrib/completion/zsh/exo .PHONY: clean clean:: diff --git a/completion/main.go b/completion/main.go index 04fbbeff7..651647281 100644 --- a/completion/main.go +++ b/completion/main.go @@ -2,12 +2,35 @@ package main import ( "log" + "os" "github.com/exoscale/cli/cmd" ) func main() { - if err := cmd.RootCmd.GenBashCompletionFile("bash_completion"); err != nil { + if len(os.Args) != 2 { + log.Fatalf("usage: %s (bash|fish|powershell|zsh)", os.Args[0]) + } + + var err error + switch os.Args[1] { + case "bash": + err = cmd.RootCmd.GenBashCompletionFile("bash_completion") + + case "fish": + err = cmd.RootCmd.GenFishCompletionFile("fish_completion", true) + + case "powershell": + err = cmd.RootCmd.GenPowerShellCompletionFile("powershell_completion") + + case "zsh": + err = cmd.RootCmd.GenZshCompletionFile("zsh_completion") + + default: + log.Fatalf("unsupported shell %q", os.Args[1]) + } + + if err != nil { log.Fatal(err) } }