Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R4R: add command to generate bash completion for gaia{d,cli} #3334

Merged
merged 4 commits into from
Jan 22, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions cmd/gaia/cmd/gaiacli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions cmd/gaia/cmd/gaiad/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
32 changes: 32 additions & 0 deletions server/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,38 @@ func UpgradeOldPrivValFile(config *cfg.Config) {
}
}

// NewCompletionCmd builds a cobra.Command that generate bash completion
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this wants to live somewhere in the client folder? We need to do some thinking on CLI code organization.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

// 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
Expand Down