From c75ac72af7e289a3027aa6a0c57fb20ed5258aa7 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Mon, 21 Jan 2019 13:13:08 +0000 Subject: [PATCH 1/4] add command to generate shell completion scripts --- PENDING.md | 1 + cmd/gaia/cmd/gaiacli/main.go | 2 ++ cmd/gaia/cmd/gaiad/main.go | 1 + server/util.go | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 36 insertions(+) diff --git a/PENDING.md b/PENDING.md index b3a05abf9271..c8c165663a01 100644 --- a/PENDING.md +++ b/PENDING.md @@ -61,6 +61,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 diff --git a/cmd/gaia/cmd/gaiacli/main.go b/cmd/gaia/cmd/gaiacli/main.go index 52514d411664..ab3e31d75f14 100644 --- a/cmd/gaia/cmd/gaiacli/main.go +++ b/cmd/gaia/cmd/gaiacli/main.go @@ -19,6 +19,7 @@ import ( "github.com/cosmos/cosmos-sdk/client/rpc" "github.com/cosmos/cosmos-sdk/client/tx" "github.com/cosmos/cosmos-sdk/cmd/gaia/app" + "github.com/cosmos/cosmos-sdk/server" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/version" @@ -93,6 +94,7 @@ func main() { keys.Commands(), client.LineBreak, version.VersionCmd, + server.NewCompletionCmd(rootCmd, true), ) // Add flags and prefix all env exposed with GA diff --git a/cmd/gaia/cmd/gaiad/main.go b/cmd/gaia/cmd/gaiad/main.go index 1cb65b3c75ca..096075a953c5 100644 --- a/cmd/gaia/cmd/gaiad/main.go +++ b/cmd/gaia/cmd/gaiad/main.go @@ -42,6 +42,7 @@ func main() { rootCmd.AddCommand(gaiaInit.TestnetFilesCmd(ctx, cdc)) rootCmd.AddCommand(gaiaInit.GenTxCmd(ctx, cdc)) rootCmd.AddCommand(gaiaInit.AddGenesisAccountCmd(ctx, cdc)) + rootCmd.AddCommand(server.NewCompletionCmd(rootCmd, true)) server.AddCommands(ctx, cdc, rootCmd, newApp, exportAppStateAndTMValidators) diff --git a/server/util.go b/server/util.go index 5092cab9ad81..cb59aff01189 100644 --- a/server/util.go +++ b/server/util.go @@ -233,6 +233,38 @@ func UpgradeOldPrivValFile(config *cfg.Config) { } } +// 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 +} + func skipInterface(iface net.Interface) bool { if iface.Flags&net.FlagUp == 0 { return true // interface down From a8b20dcedd6310341d81af69638e8863feec719a Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Mon, 21 Jan 2019 17:03:39 +0000 Subject: [PATCH 2/4] Add docs --- docs/gaia/gaiacli.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/docs/gaia/gaiacli.md b/docs/gaia/gaiacli.md index 0ec271b68b7a..2018b37fa46b 100644 --- a/docs/gaia/gaiacli.md +++ b/docs/gaia/gaiacli.md @@ -761,3 +761,26 @@ 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 +``` + +Refer to the user's manual of your interpreter for information +on how to enable shell autocompletion. From 2e41835acd8e425b37997853fa3e184431659511 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Tue, 22 Jan 2019 01:40:16 +0000 Subject: [PATCH 3/4] Move NewCompletionCmd into client --- client/flags.go | 33 +++++++++++++++++++++++++++++++++ cmd/gaia/cmd/gaiacli/main.go | 3 +-- cmd/gaia/cmd/gaiad/main.go | 3 ++- server/util.go | 32 -------------------------------- 4 files changed, 36 insertions(+), 35 deletions(-) diff --git a/client/flags.go b/client/flags.go index 50890635c4ef..76ebb9e87425 100644 --- a/client/flags.go +++ b/client/flags.go @@ -2,6 +2,7 @@ package client import ( "fmt" + "os" "strconv" "github.com/spf13/cobra" @@ -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 +} diff --git a/cmd/gaia/cmd/gaiacli/main.go b/cmd/gaia/cmd/gaiacli/main.go index ab3e31d75f14..f390324aa383 100644 --- a/cmd/gaia/cmd/gaiacli/main.go +++ b/cmd/gaia/cmd/gaiacli/main.go @@ -19,7 +19,6 @@ import ( "github.com/cosmos/cosmos-sdk/client/rpc" "github.com/cosmos/cosmos-sdk/client/tx" "github.com/cosmos/cosmos-sdk/cmd/gaia/app" - "github.com/cosmos/cosmos-sdk/server" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/version" @@ -94,7 +93,7 @@ func main() { keys.Commands(), client.LineBreak, version.VersionCmd, - server.NewCompletionCmd(rootCmd, true), + client.NewCompletionCmd(rootCmd, true), ) // Add flags and prefix all env exposed with GA diff --git a/cmd/gaia/cmd/gaiad/main.go b/cmd/gaia/cmd/gaiad/main.go index 096075a953c5..4014bef7330e 100644 --- a/cmd/gaia/cmd/gaiad/main.go +++ b/cmd/gaia/cmd/gaiad/main.go @@ -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" @@ -42,7 +43,7 @@ func main() { rootCmd.AddCommand(gaiaInit.TestnetFilesCmd(ctx, cdc)) rootCmd.AddCommand(gaiaInit.GenTxCmd(ctx, cdc)) rootCmd.AddCommand(gaiaInit.AddGenesisAccountCmd(ctx, cdc)) - rootCmd.AddCommand(server.NewCompletionCmd(rootCmd, true)) + rootCmd.AddCommand(client.NewCompletionCmd(rootCmd, true)) server.AddCommands(ctx, cdc, rootCmd, newApp, exportAppStateAndTMValidators) diff --git a/server/util.go b/server/util.go index cb59aff01189..5092cab9ad81 100644 --- a/server/util.go +++ b/server/util.go @@ -233,38 +233,6 @@ func UpgradeOldPrivValFile(config *cfg.Config) { } } -// 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 -} - func skipInterface(iface net.Interface) bool { if iface.Flags&net.FlagUp == 0 { return true // interface down From a6fbcc7ae30999d3731ff6c6319c67cbd823b437 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Tue, 22 Jan 2019 02:22:23 +0000 Subject: [PATCH 4/4] Added a small example --- docs/gaia/gaiacli.md | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/docs/gaia/gaiacli.md b/docs/gaia/gaiacli.md index 2018b37fa46b..fad2c9859b3b 100644 --- a/docs/gaia/gaiacli.md +++ b/docs/gaia/gaiacli.md @@ -782,5 +782,15 @@ gaiad completion --zsh > gaiad_completion gaiacli completion --zsh > gaiacli_completion ``` -Refer to the user's manual of your interpreter for information -on how to enable shell autocompletion. +::: 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. +:::