Skip to content

Commit

Permalink
Add bash completion for gaiad and gaiacli (#3334)
Browse files Browse the repository at this point in the history
  • Loading branch information
alessio authored and jackzampolin committed Jan 22, 2019
1 parent 7f789d2 commit cffa39c
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ FEATURES
* [\#3198](https://github.com/cosmos/cosmos-sdk/issues/3198) New `multisign` command to generate multisig signatures.
* [\#3198](https://github.com/cosmos/cosmos-sdk/issues/3198) New `sign --multisig` flag to enable multisig mode.
* [\#2715](https://github.com/cosmos/cosmos-sdk/issues/2715) Reintroduce gaia server's insecure mode.
* [\#3334](https://github.com/cosmos/cosmos-sdk/pull/3334) New `gaiad completion` and `gaiacli completion` to generate Bash/Zsh completion scripts.

* Gaia
* [\#2182] [x/staking] Added querier for querying a single redelegation
Expand Down
33 changes: 33 additions & 0 deletions client/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"fmt"
"os"
"strconv"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -162,3 +163,35 @@ func ParseGas(gasStr string) (simulateAndExecute bool, gas uint64, err error) {
}
return
}

// NewCompletionCmd builds a cobra.Command that generate bash completion
// scripts for the given root command. If hidden is true, the command
// will not show up in the root command's list of available commands.
func NewCompletionCmd(rootCmd *cobra.Command, hidden bool) *cobra.Command {
flagZsh := "zsh"
cmd := &cobra.Command{
Use: "completion",
Short: "Generate Bash/Zsh completion script to STDOUT",
Long: `To load completion script run
. <(completion_script)
To configure your bash shell to load completions for each session add to your bashrc
# ~/.bashrc or ~/.profile
. <(completion_script)
`,
RunE: func(_ *cobra.Command, _ []string) error {
if viper.GetBool(flagZsh) {
return rootCmd.GenZshCompletion(os.Stdout)
}
return rootCmd.GenBashCompletion(os.Stdout)
},
Hidden: hidden,
Args: cobra.NoArgs,
}

cmd.Flags().Bool(flagZsh, false, "Generate Zsh completion script")

return cmd
}
1 change: 1 addition & 0 deletions cmd/gaia/cmd/gaiacli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func main() {
keys.Commands(),
client.LineBreak,
version.VersionCmd,
client.NewCompletionCmd(rootCmd, true),
)

// Add flags and prefix all env exposed with GA
Expand Down
2 changes: 2 additions & 0 deletions cmd/gaia/cmd/gaiad/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
tmtypes "github.com/tendermint/tendermint/types"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/cmd/gaia/app"
gaiaInit "github.com/cosmos/cosmos-sdk/cmd/gaia/init"
"github.com/cosmos/cosmos-sdk/server"
Expand Down Expand Up @@ -42,6 +43,7 @@ func main() {
rootCmd.AddCommand(gaiaInit.TestnetFilesCmd(ctx, cdc))
rootCmd.AddCommand(gaiaInit.GenTxCmd(ctx, cdc))
rootCmd.AddCommand(gaiaInit.AddGenesisAccountCmd(ctx, cdc))
rootCmd.AddCommand(client.NewCompletionCmd(rootCmd, true))

server.AddCommands(ctx, cdc, rootCmd, newApp, exportAppStateAndTMValidators)

Expand Down
33 changes: 33 additions & 0 deletions docs/gaia/gaiacli.md
Original file line number Diff line number Diff line change
Expand Up @@ -761,3 +761,36 @@ The transaction can now be sent to the node:
```bash
gaiacli tx broadcast signedTx.json
```

## Shells completion scripts

Completion scripts for popular UNIX shell interpreters such as `Bash` and `Zsh`
can be generated through the `completion` command, which is available for both
`gaiad` and `gaiacli`.

If you want to generate `Bash` completion scripts run the following command:

```bash
gaiad completion > gaiad_completion
gaiacli completion > gaiacli_completion
```

If you want to generate `Zsh` completion scripts run the following command:

```bash
gaiad completion --zsh > gaiad_completion
gaiacli completion --zsh > gaiacli_completion
```

::: tip Note
On most UNIX systems, such scripts may be loaded in `.bashrc` or
`.bash_profile` to enable Bash autocompletion:

```bash
echo '. gaiad_completion' >> ~/.bashrc
echo '. gaiacli_completion' >> ~/.bashrc
```

Refer to the user's manual of your interpreter provided by your
operating system for information on how to enable shell autocompletion.
:::

0 comments on commit cffa39c

Please sign in to comment.