Skip to content

Commit

Permalink
#309: Add shell completion (#310)
Browse files Browse the repository at this point in the history
* add shell completion
* gofmt
* gometalinter
  • Loading branch information
fubarhouse committed Apr 9, 2021
1 parent cc64bf0 commit 809f449
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 6 deletions.
91 changes: 91 additions & 0 deletions cmd/completion.go
@@ -0,0 +1,91 @@
// Copyright © 2019 Karl Hepworth <Karl.Hepworth@gmail.com>
//
// 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)
}
14 changes: 9 additions & 5 deletions cmd/root.go
Expand Up @@ -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.`,
Expand Down Expand Up @@ -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())
}
}
}
4 changes: 3 additions & 1 deletion main.go
Expand Up @@ -20,7 +20,9 @@

package main

import "github.com/fubarhouse/pygmy-go/cmd"
import (
"github.com/fubarhouse/pygmy-go/cmd"
)

func main() {
cmd.Execute()
Expand Down

0 comments on commit 809f449

Please sign in to comment.