Skip to content

Commit

Permalink
Add auto complete. (#1762)
Browse files Browse the repository at this point in the history
* Add auto complete.

* Add tests for auto complete.

* Change some text.

* Update changelog.

* Update test to remove branch since it's already included.
  • Loading branch information
Taztingo authored Nov 28, 2023
1 parent 57ebb37 commit 5553cbd
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Features

* Add CLI commands for the exchange module endpoints and queries [#1701](https://github.com/provenance-io/provenance/issues/1701).
* Add CLI command to generate autocomplete shell scripts [#1762](https://github.com/provenance-io/provenance/pull/1762).

### Improvements

Expand Down
54 changes: 54 additions & 0 deletions cmd/provenanced/cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/genutil/client/cli"

"github.com/provenance-io/provenance/cmd/provenanced/cmd"
"github.com/provenance-io/provenance/testutil/assertions"
)

func TestInitCmd(t *testing.T) {
Expand All @@ -24,3 +25,56 @@ func TestInitCmd(t *testing.T) {
err := cmd.Execute(rootCmd)
require.NoError(t, err)
}

func TestGenAutoCompleteCmd(t *testing.T) {
home := t.TempDir()

tests := []struct {
name string
args []string
err string
}{
{
name: "failure - missing arg",
err: "accepts 1 arg(s), received 0",
},
{
name: "failure - too many args",
args: []string{"bash", "fish"},
err: "accepts 1 arg(s), received 2",
},
{
name: "failure - invalid shell type",
args: []string{"badshellname"},
err: "shell badshellname is not supported",
},
{
name: "success - works with bash",
args: []string{"bash"},
},
{
name: "success - works with zsh",
args: []string{"zsh"},
},
{
name: "success - works with fish",
args: []string{"fish"},
},
{
name: "success - works with powershell",
args: []string{"powershell"},
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
args := []string{"--home", home, "enable-cli-autocomplete"}
args = append(args, tc.args...)

rootCmd, _ := cmd.NewRootCmd(false)
rootCmd.SetArgs(args)
err := cmd.Execute(rootCmd)
assertions.AssertErrorValue(t, err, tc.err, "should have the correct output value")
})
}
}
36 changes: 36 additions & 0 deletions cmd/provenanced/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func NewRootCmd(sealConfig bool) (*cobra.Command, params.EncodingConfig) {
return nil
},
}
genAutoCompleteCmd(rootCmd)
initRootCmd(rootCmd, encodingConfig)
overwriteFlagDefaults(rootCmd, map[string]string{
flags.FlagChainID: "",
Expand Down Expand Up @@ -180,6 +181,41 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig) {
startCmd.SilenceUsage = true
}

// genAutoCompleteCmd creates the command for autocomplete.
func genAutoCompleteCmd(rootCmd *cobra.Command) {
rootCmd.AddCommand(&cobra.Command{
Use: "enable-cli-autocomplete [bash|zsh|fish|powershell]",
Short: "Generates autocomplete scripts for the provenanced binary",
Long: `To configure your shell to load completions for each session, add to your profile:
# bash example
echo '. <(provenanced enable-cli-autocomplete bash)' >> ~/.bash_profile
source ~/.bash_profile
# zsh example
echo '. <(provenanced enable-cli-autocomplete zsh)' >> ~/.zshrc
source ~/.zshrc
`,
DisableFlagsInUseLine: true,
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
switch args[0] {
case "bash":
return cmd.Root().GenBashCompletion(os.Stdout)
case "zsh":
return cmd.Root().GenZshCompletion(os.Stdout)
case "fish":
return cmd.Root().GenFishCompletion(os.Stdout, true)
case "powershell":
return cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout)
}

return fmt.Errorf("shell %s is not supported", args[0])
},
})
}

func addModuleInitFlags(startCmd *cobra.Command) {
crisis.AddModuleInitFlags(startCmd)
}
Expand Down

0 comments on commit 5553cbd

Please sign in to comment.