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

Move genesis commands to a sub-command and add the SDK's auto-cli commands. #1971

Merged
merged 28 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
fe8f694
[1760]: Add all the auto-cli commands.
SpicyLemon May 14, 2024
12ffc31
[1760]: Add new query and tx commdands provided by the SDK.
SpicyLemon May 14, 2024
76cc9ae
[1760]: Move the genesis-related commands into the new genesis comman…
SpicyLemon May 14, 2024
fab740d
[1760]: Update the scripts that use the genesis commands.
SpicyLemon May 14, 2024
d5efe47
[1760]: Update the upgrade-config script to update the correct fields…
SpicyLemon May 14, 2024
441bd64
[1760]: Add a changelog entry about the commands being moved to genesis.
SpicyLemon May 14, 2024
92b1483
[1760]: Remove the enable-cli-autocomplete command because cobra now …
SpicyLemon May 14, 2024
43447a2
[1760]: Remove the genesis- part out of all the genesis commands sinc…
SpicyLemon May 14, 2024
1ce56eb
[1760]: Remove the calls to AddTxCommands and AddQueryCommands since …
SpicyLemon May 14, 2024
44244ed
[1760]: Add links to changelog entries.
SpicyLemon May 14, 2024
5c11015
[1760]: Take the table out of the changelog and put it in the PR desc…
SpicyLemon May 14, 2024
8b267f7
[1760]: Bump the blocksPerVotingPeriod to 8 (from 6) in the sanction …
SpicyLemon May 15, 2024
0881797
[1760]: Invoke grep directly instead of cat pipe grep.
SpicyLemon May 15, 2024
1ed19a8
Merge branch 'main' into dwedul/1760-auto-cli
SpicyLemon May 15, 2024
cc36fba
[1760]: Rename genaccounts.go to genesis.go since that file is for ge…
SpicyLemon May 15, 2024
da42897
[1760]: Add aliases for the genesis commands so that they also have t…
SpicyLemon May 15, 2024
e4f6080
[1760]: Align the aliases of the tx and query commands for each module.
SpicyLemon May 15, 2024
583fba1
[1760]: Add some cli sanity unit tests.
SpicyLemon May 15, 2024
cea6659
[1760]: Fix the wasm instantiate2 aliases.
SpicyLemon May 15, 2024
f160d3b
[1760]: Fix comment grammar.
SpicyLemon May 15, 2024
c93823d
[1760]: Disable address caching in the config command tests because i…
SpicyLemon May 15, 2024
fbd1c9b
[1760]: Create a hidden tree command that will list all possible comm…
SpicyLemon May 15, 2024
4284406
[1760]: Add the alias e to the provenacned metaaddress encode command…
SpicyLemon May 15, 2024
fbcf2e0
[1760]: Disable address caching for a bunch more tests because I can'…
SpicyLemon May 15, 2024
61b6b67
[1760]: Tweak the tree use-line to include the aliases flag but hide …
SpicyLemon May 15, 2024
c8601d3
[1760]: Move the tree command to its own file so that its easier to s…
SpicyLemon May 15, 2024
703abf1
[1760]: Add unit tests for the tree command.
SpicyLemon May 15, 2024
9c51585
Merge branch 'main' into dwedul/1760-auto-cli
SpicyLemon May 15, 2024
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ Ref: https://keepachangelog.com/en/1.0.0/
* Clean up all the module codecs [#1957](https://github.com/provenance-io/provenance/pull/1957).
* Switch to auto-generated `String` and `Equal` methods for most proto messages [#1957](https://github.com/provenance-io/provenance/pull/1957).
* Clean up the marker module's expected BankKeeper interface [#1954](https://github.com/provenance-io/provenance/pull/1954).
* Add the auto-cli commands and a few others newly added by the SDK [#1971](https://github.com/provenance-io/provenance/pull/1971).
SpicyLemon marked this conversation as resolved.
Show resolved Hide resolved

### Client Breaking

* The `provenanced query account` command has been removed. It is still available as `provenanced query auth account` [#1971](https://github.com/provenance-io/provenance/pull/1971).
* Move the genesis-related commands into a new `genesis` sub-command, and remove the `genesis-` parts of their names [#1971](https://github.com/provenance-io/provenance/pull/1971).
* Many of the SDK's query commands have had their usage altered [#1971](https://github.com/provenance-io/provenance/pull/1971).
SpicyLemon marked this conversation as resolved.
Show resolved Hide resolved

### Deprecated

Expand Down
25 changes: 25 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
abci "github.com/cometbft/cometbft/abci/types"
cmtos "github.com/cometbft/cometbft/libs/os"

"cosmossdk.io/client/v2/autocli"

Check failure on line 22 in app/app.go

View workflow job for this annotation

GitHub Actions / golangci-lint

import 'cosmossdk.io/client/v2/autocli' is not allowed from list 'main' (depguard)
"cosmossdk.io/core/appmodule"
"cosmossdk.io/log"
sdkmath "cosmossdk.io/math"
storetypes "cosmossdk.io/store/types"
Expand All @@ -42,6 +44,7 @@
"github.com/cosmos/cosmos-sdk/codec/address"
"github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/runtime"
runtimeservices "github.com/cosmos/cosmos-sdk/runtime/services"
"github.com/cosmos/cosmos-sdk/server/api"
serverconfig "github.com/cosmos/cosmos-sdk/server/config"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
Expand Down Expand Up @@ -1320,6 +1323,28 @@
nodeservice.RegisterNodeService(clientCtx, app.GRPCQueryRouter(), cfg)
}

// AutoCliOpts returns the autocli options for the app.
func (app *App) AutoCliOpts() autocli.AppOptions {
modules := make(map[string]appmodule.AppModule, 0)
for _, m := range app.mm.Modules {
if moduleWithName, ok := m.(module.HasName); ok {
moduleName := moduleWithName.Name()
if appModule, ok := moduleWithName.(appmodule.AppModule); ok {
modules[moduleName] = appModule
}
}
}

cfg := sdk.GetConfig()
return autocli.AppOptions{
Modules: modules,
ModuleOptions: runtimeservices.ExtractAutoCLIOptions(app.mm.Modules),
AddressCodec: authcodec.NewBech32Codec(cfg.GetBech32AccountAddrPrefix()),
ValidatorAddressCodec: authcodec.NewBech32Codec(cfg.GetBech32ValidatorAddrPrefix()),
ConsensusAddressCodec: authcodec.NewBech32Codec(cfg.GetBech32ConsensusAddrPrefix()),
}
}

// RegisterSwaggerAPI registers swagger route with API Server
func RegisterSwaggerAPI(_ client.Context, rtr *mux.Router) {
statikFS, err := fs.New()
Expand Down
5 changes: 3 additions & 2 deletions cmd/provenanced/cmd/addresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,9 @@ Excess (hex): %s
// AddMetaAddressEncoder returns metadata address encoder cobra Command.
func AddMetaAddressEncoder() *cobra.Command {
cmd := &cobra.Command{
Use: "encode [type] [uuid] [uuid|name]",
Short: "Encodes metadata uuids to bech32 address for specific type",
Use: "encode [type] [uuid] [uuid|name]",
Aliases: []string{"e"},
Short: "Encodes metadata uuids to bech32 address for specific type",
Long: fmt.Sprintf(`Encodes metadata uuids to bech32 address for specific type.

%[1]s encode type uuid [uuid|name]
Expand Down
57 changes: 0 additions & 57 deletions cmd/provenanced/cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ 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 @@ -28,59 +27,3 @@ 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)
rootCmd.SetOut(io.Discard)
rootCmd.SetErr(io.Discard)

err := cmd.Execute(rootCmd)
assertions.AssertErrorValue(t, err, tc.err, "should have the correct output value")
})
}
}
10 changes: 10 additions & 0 deletions cmd/provenanced/cmd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/server"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/provenance-io/provenance/app"
simappparams "github.com/provenance-io/provenance/app/params"
Expand All @@ -46,10 +47,15 @@ type ConfigTestSuite struct {
BaseFNApp string
BaseFNCMT string
baseFNClient string

origAddrCacheEnabled bool
}

func (s *ConfigTestSuite) SetupTest() {
s.Home = s.T().TempDir()
s.origAddrCacheEnabled = sdk.IsAddrCacheEnabled()
sdk.SetAddrCacheEnabled(false)

s.T().Logf("%s Home: %s", s.T().Name(), s.Home)

pioconfig.SetProvenanceConfig("confcoin", 5)
Expand Down Expand Up @@ -81,6 +87,10 @@ func (s *ConfigTestSuite) SetupTest() {
s.ensureConfigFiles()
}

func (s *ConfigTestSuite) TearDownTest() {
sdk.SetAddrCacheEnabled(s.origAddrCacheEnabled)
}

func TestConfigTestSuite(t *testing.T) {
suite.Run(t, new(ConfigTestSuite))
}
Expand Down
86 changes: 86 additions & 0 deletions cmd/provenanced/cmd/docgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"os"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
Expand Down Expand Up @@ -101,3 +102,88 @@ func exists(dir string) bool {
_, err := os.Stat(dir)
return err == nil
}

// GetTreeCmd gets the tree command that will output all the commands available.
func GetTreeCmd() *cobra.Command {
aliasesFlag := "aliases"
cmd := &cobra.Command{
Use: "tree [sub-command]",
Short: "Get a tree of the commands optionally limited to a specific sub-command",
Hidden: true,
Args: cobra.ArbitraryArgs,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 && args[0] != "provenanced" {
args = append([]string{"provenanced"}, args...)
}

incAliases, _ := cmd.Flags().GetBool(aliasesFlag)
cmds := getAllCommands(cmd.Root(), "", args, incAliases)
cmd.Printf(strings.Join(cmds, "\n") + "\n")
return nil
},
}
cmd.Flags().Bool(aliasesFlag, false, "Include command aliases in the output")

return cmd
}

// hasNameOrAlias returns true if the provided command as a name or an alias equal to the provided arg.
func hasNameOrAlias(cmd *cobra.Command, arg string) bool {
if cmd.Name() == arg {
return true
}
for _, alias := range cmd.Aliases {
if alias == arg {
return true
}
}
return false
}

// getNameAndAliases gets a list of the command's name and aliases (without any duplicates).
func getNameAndAliases(cmd *cobra.Command) []string {
rv := make([]string, 1, 1+len(cmd.Aliases))
rv[0] = cmd.Name()
for _, alias := range cmd.Aliases {
known := false
for _, prev := range rv {
if prev == alias {
known = true
}
}
if !known {
rv = append(rv, alias)
}
}
return rv
}

// getAllCommands gets all of the possible commands under the given command.
func getAllCommands(cmd *cobra.Command, prev string, limitTo []string, incAliases bool) []string {
if len(limitTo) > 0 {
if !hasNameOrAlias(cmd, limitTo[0]) {
return nil
}
limitTo = limitTo[1:]
}

name := cmd.Name()
if incAliases {
names := getNameAndAliases(cmd)
if len(names) > 1 {
name = "[" + strings.Join(names, " ") + "]"
}
}
cur := strings.TrimSpace(prev + " " + name)

subCmds := cmd.Commands()
if len(subCmds) == 0 {
return []string{cur}
}

var rv []string
for _, subCmd := range subCmds {
rv = append(rv, getAllCommands(subCmd, cur, limitTo, incAliases)...)
}
return rv
}
Loading
Loading