-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
feat: add basic autocli config to every module #13786
Changes from all commits
055c848
e8fdf88
e8f39a9
a5c2b0e
2fc0943
c89c22b
8da86e4
fa33b5b
a4fadfb
c32e4cd
4d430c8
6087514
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,8 @@ import ( | |
"context" | ||
|
||
autocliv1 "cosmossdk.io/api/cosmos/autocli/v1" | ||
gogogrpc "github.com/cosmos/gogoproto/grpc" | ||
"google.golang.org/grpc" | ||
|
||
"github.com/cosmos/cosmos-sdk/types/module" | ||
) | ||
|
@@ -22,6 +24,31 @@ func NewAutoCLIQueryService(appModules map[string]module.AppModule) *AutoCLIQuer | |
AutoCLIOptions() *autocliv1.ModuleOptions | ||
}); ok { | ||
moduleOptions[modName] = autoCliMod.AutoCLIOptions() | ||
} else { | ||
// try to auto-discover options based on the last msg and query | ||
// services registered for the module | ||
cfg := &autocliConfigurator{} | ||
mod.RegisterServices(cfg) | ||
modOptions := &autocliv1.ModuleOptions{} | ||
haveServices := false | ||
|
||
if cfg.msgServer.serviceName != "" { | ||
haveServices = true | ||
modOptions.Tx = &autocliv1.ServiceCommandDescriptor{ | ||
Service: cfg.msgServer.serviceName, | ||
} | ||
} | ||
|
||
if cfg.queryServer.serviceName != "" { | ||
haveServices = true | ||
modOptions.Query = &autocliv1.ServiceCommandDescriptor{ | ||
Service: cfg.queryServer.serviceName, | ||
} | ||
} | ||
|
||
if haveServices { | ||
moduleOptions[modName] = modOptions | ||
} | ||
} | ||
} | ||
return &AutoCLIQueryService{ | ||
|
@@ -35,4 +62,27 @@ func (a AutoCLIQueryService) AppOptions(context.Context, *autocliv1.AppOptionsRe | |
}, nil | ||
} | ||
|
||
// autocliConfigurator allows us to call RegisterServices and introspect the services | ||
type autocliConfigurator struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit move that down next to its methods. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is just so that we can call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess this PR using the configurator approach is an intermediary solution, right? I'm in favor of removing the AppModule's RegisterServices should be simply There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exactly |
||
msgServer autocliServiceRegistrar | ||
queryServer autocliServiceRegistrar | ||
} | ||
|
||
func (a *autocliConfigurator) MsgServer() gogogrpc.Server { return &a.msgServer } | ||
|
||
func (a *autocliConfigurator) QueryServer() gogogrpc.Server { return &a.queryServer } | ||
|
||
func (a *autocliConfigurator) RegisterMigration(string, uint64, module.MigrationHandler) error { | ||
return nil | ||
} | ||
|
||
// autocliServiceRegistrar is used to capture the service name for registered services | ||
type autocliServiceRegistrar struct { | ||
serviceName string | ||
} | ||
|
||
func (a *autocliServiceRegistrar) RegisterService(sd *grpc.ServiceDesc, _ interface{}) { | ||
a.serviceName = sd.ServiceName | ||
} | ||
|
||
var _ autocliv1.QueryServer = &AutoCLIQueryService{} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package auth | ||
|
||
import ( | ||
authv1beta1 "cosmossdk.io/api/cosmos/auth/v1beta1" | ||
autocliv1 "cosmossdk.io/api/cosmos/autocli/v1" | ||
) | ||
|
||
// AutoCLIOptions implements the autocli.HasAutoCLIConfig interface. | ||
func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions { | ||
aaronc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return &autocliv1.ModuleOptions{ | ||
Query: &autocliv1.ServiceCommandDescriptor{ | ||
Service: authv1beta1.Query_ServiceDesc.ServiceName, | ||
RpcCommandOptions: []*autocliv1.RpcCommandOptions{ | ||
{ | ||
RpcMethod: "Account", | ||
Use: "account [address]", | ||
Short: "query account by address", | ||
PositionalArgs: []*autocliv1.PositionalArgDescriptor{{ProtoField: "address"}}, | ||
}, | ||
{ | ||
RpcMethod: "AccountAddressByID", | ||
Use: "address-by-id [acc-num]", | ||
Short: "query account address by account number", | ||
PositionalArgs: []*autocliv1.PositionalArgDescriptor{{ProtoField: "id"}}, | ||
}, | ||
}, | ||
}, | ||
Tx: &autocliv1.ServiceCommandDescriptor{ | ||
Service: authv1beta1.Msg_ServiceDesc.ServiceName, | ||
}, | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package gov | ||
|
||
import ( | ||
autocliv1 "cosmossdk.io/api/cosmos/autocli/v1" | ||
govv1 "cosmossdk.io/api/cosmos/gov/v1" | ||
govv1beta1 "cosmossdk.io/api/cosmos/gov/v1beta1" | ||
) | ||
|
||
// AutoCLIOptions implements the autocli.HasAutoCLIConfig interface. | ||
func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions { | ||
return &autocliv1.ModuleOptions{ | ||
Tx: &autocliv1.ServiceCommandDescriptor{ | ||
Service: govv1.Msg_ServiceDesc.ServiceName, | ||
// map v1beta1 as a sub-command | ||
SubCommands: map[string]*autocliv1.ServiceCommandDescriptor{ | ||
"v1beta1": {Service: govv1beta1.Msg_ServiceDesc.ServiceName}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. autocli is not wired up to simd's root cmd, right? When I try There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exactly |
||
}, | ||
}, | ||
Query: &autocliv1.ServiceCommandDescriptor{ | ||
Service: govv1.Query_ServiceDesc.ServiceName, | ||
// map v1beta1 as a sub-command | ||
SubCommands: map[string]*autocliv1.ServiceCommandDescriptor{ | ||
"v1beta1": {Service: govv1beta1.Query_ServiceDesc.ServiceName}, | ||
}, | ||
}, | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was asking above because what happens when we wire up autocli with the root cobra command, and there are conflicts in command names between this auto-discovered one and custom ones?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AutoCLI prefers the custom commands