From 809f4497262a64cf5d11dff0f1cadd371089782f Mon Sep 17 00:00:00 2001 From: Karl Hepworth Date: Fri, 9 Apr 2021 22:18:18 +1000 Subject: [PATCH] #309: Add shell completion (#310) * add shell completion * gofmt * gometalinter --- cmd/completion.go | 91 +++++++++++++++++++++++++++++++++++++++++++++++ cmd/root.go | 14 +++++--- main.go | 4 ++- 3 files changed, 103 insertions(+), 6 deletions(-) create mode 100644 cmd/completion.go diff --git a/cmd/completion.go b/cmd/completion.go new file mode 100644 index 00000000..5538f672 --- /dev/null +++ b/cmd/completion.go @@ -0,0 +1,91 @@ +// Copyright © 2019 Karl Hepworth +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to +// deal in the Software without restriction, including without limitation the +// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +// sell copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +// IN THE SOFTWARE. + +package cmd + +import ( + "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 <(pygmy-go completion bash) + + # To load completions for each session, execute once: + # Linux: + $ pygmy-go completion bash > /etc/bash_completion.d/pygmy-go + # macOS: + $ pygmy-go completion bash > /usr/local/etc/bash_completion.d/pygmy-go + +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: + $ pygmy-go completion zsh > "${fpath[1]}/pygmy-go" + + # You will need to start a new shell for this setup to take effect. + +fish: + + $ pygmy-go completion fish | source + + # To load completions for each session, execute once: + $ pygmy-go completion fish > ~/.config/fish/completions/pygmy-go.fish + +PowerShell: + + PS> pygmy-go completion powershell | Out-String | Invoke-Expression + + # To load completions for every new session, run: + PS> pygmy-go completion powershell > pygmy-go.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) { + switch args[0] { + case "bash": + _ = cmd.Root().GenBashCompletion(os.Stdout) + case "zsh": + _ = cmd.Root().GenZshCompletion(os.Stdout) + case "fish": + _ = cmd.Root().GenFishCompletion(os.Stdout, true) + case "powershell": + _ = cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout) + } + }, +} + +func init() { + rootCmd.AddCommand(completionCmd) +} diff --git a/cmd/root.go b/cmd/root.go index 4af0c9f4..ee13ee97 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -33,14 +33,16 @@ import ( ) var ( - cfgFile string - c library.Config + cfgFile string + c library.Config + validArgs = []string{"addkey", "clean", "down", "export", "pull", "restart", "status", "up", "update", "version"} ) // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ - Use: "pygmy", - Short: "Amazeeio's local development tool", + Use: "pygmy-go", + ValidArgs: validArgs, + Short: "Amazeeio's local development tool", Long: `Amazeeio's local development tool, Runs DNSMasq, HAProxy, MailHog and an SSH Agent in local containers for local development.`, @@ -133,6 +135,8 @@ func initConfig() { // If a config file is found, read it in. if err := viper.ReadInConfig(); err == nil { - fmt.Println("Using config file:", viper.ConfigFileUsed()) + if os.Args[1] != "completion" { + fmt.Println("Using config file:", viper.ConfigFileUsed()) + } } } diff --git a/main.go b/main.go index 4ae4c8ba..ebb00c70 100644 --- a/main.go +++ b/main.go @@ -20,7 +20,9 @@ package main -import "github.com/fubarhouse/pygmy-go/cmd" +import ( + "github.com/fubarhouse/pygmy-go/cmd" +) func main() { cmd.Execute()