diff --git a/x/logic/autocli.go b/x/logic/autocli.go deleted file mode 100644 index 57f1fda1..00000000 --- a/x/logic/autocli.go +++ /dev/null @@ -1,44 +0,0 @@ -package logic - -import ( - "fmt" - - autocliv1 "cosmossdk.io/api/cosmos/autocli/v1" - - "github.com/cosmos/cosmos-sdk/version" - - "github.com/okp4/okp4d/x/logic/types" -) - -// AutoCLIOptions implements the autocli.HasAutoCLIConfig interface for the logic module. -func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions { - return &autocliv1.ModuleOptions{ - Query: &autocliv1.ServiceCommandDescriptor{ - Service: types.GrpcQueryServiceDesc().ServiceName, - RpcCommandOptions: []*autocliv1.RpcCommandOptions{ - { - RpcMethod: "Ask", - Use: "ask [query]", - Short: "Executes a logic query and returns the solution(s) found.", - Long: `Executes the [query] and return the solution(s) found. - -Optionally, a program can be transmitted, which will be compiled before the query is processed. - -Since the query is without any side-effect, the query is not executed in the context of a transaction and no fee -is charged for this, but the execution is constrained by the current limits configured in the module (that you can -query).`, - Example: fmt.Sprintf(`$ %s query %s ask "chain_id(X)." # returns the chain-id`, - version.AppName, - types.ModuleName), - PositionalArgs: []*autocliv1.PositionalArgDescriptor{{ProtoField: "query"}}, - FlagOptions: map[string]*autocliv1.FlagOptions{ - "program": { - Shorthand: "p", - Usage: "The program to compile before the query.", - }, - }, - }, - }, - }, - } -} diff --git a/x/logic/client/cli/query.go b/x/logic/client/cli/query.go new file mode 100644 index 00000000..3b14bc61 --- /dev/null +++ b/x/logic/client/cli/query.go @@ -0,0 +1,28 @@ +package cli + +import ( + "fmt" + + "github.com/spf13/cobra" + + "github.com/cosmos/cosmos-sdk/client" + + "github.com/okp4/okp4d/x/logic/types" +) + +// GetQueryCmd returns the cli query commands for this module. +func GetQueryCmd() *cobra.Command { + // Group logic queries under a subcommand + cmd := &cobra.Command{ + Use: types.ModuleName, + Short: fmt.Sprintf("Querying commands for the %s module", types.ModuleName), + DisableFlagParsing: true, + SuggestionsMinimumDistance: 2, + RunE: client.ValidateCmd, + } + + cmd.AddCommand(CmdQueryParams()) + cmd.AddCommand(CmdQueryAsk()) + + return cmd +} diff --git a/x/logic/client/cli/query_ask.go b/x/logic/client/cli/query_ask.go new file mode 100644 index 00000000..7e809cf0 --- /dev/null +++ b/x/logic/client/cli/query_ask.go @@ -0,0 +1,57 @@ +package cli + +import ( + "context" + "fmt" + + "github.com/spf13/cobra" + + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/version" + + "github.com/okp4/okp4d/x/logic/types" +) + +var program string + +func CmdQueryAsk() *cobra.Command { + cmd := &cobra.Command{ + Use: "ask [query]", + Short: "executes a logic query and returns the solutions found.", + Long: `Executes the [query] and return the solution(s) found. + Optionally, a program can be transmitted, which will be interpreted before the query is processed. + Since the query is without any side-effect, the query is not executed in the context of a transaction and no fee + is charged for this, but the execution is constrained by the current limits configured in the module (that you can + query).`, + Example: fmt.Sprintf(`$ %s query %s ask "chain_id(X)." # returns the chain-id`, + version.AppName, + types.ModuleName), + Args: cobra.MinimumNArgs(1), + RunE: func(cmd *cobra.Command, args []string) (err error) { + clientCtx := client.GetClientContextFromCmd(cmd) + query := args[0] + queryClient := types.NewQueryServiceClient(clientCtx) + + res, err := queryClient.Ask(context.Background(), &types.QueryServiceAskRequest{ + Program: program, + Query: query, + }) + if err != nil { + return + } + + return clientCtx.PrintProto(res) + }, + } + + cmd.Flags().StringVar( + &program, + "program", + "", + `reads the program from the given string.`) + + flags.AddQueryFlagsToCmd(cmd) + + return cmd +} diff --git a/x/logic/client/cli/query_params.go b/x/logic/client/cli/query_params.go new file mode 100644 index 00000000..01b9ca39 --- /dev/null +++ b/x/logic/client/cli/query_params.go @@ -0,0 +1,36 @@ +package cli + +import ( + "context" + + "github.com/spf13/cobra" + + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/flags" + + "github.com/okp4/okp4d/x/logic/types" +) + +func CmdQueryParams() *cobra.Command { + cmd := &cobra.Command{ + Use: "params", + Short: "shows the parameters of the module", + Args: cobra.NoArgs, + RunE: func(cmd *cobra.Command, args []string) error { + clientCtx := client.GetClientContextFromCmd(cmd) + + queryClient := types.NewQueryServiceClient(clientCtx) + + res, err := queryClient.Params(context.Background(), &types.QueryServiceParamsRequest{}) + if err != nil { + return err + } + + return clientCtx.PrintProto(res) + }, + } + + flags.AddQueryFlagsToCmd(cmd) + + return cmd +} diff --git a/x/logic/module.go b/x/logic/module.go index 363fa72d..36ade742 100644 --- a/x/logic/module.go +++ b/x/logic/module.go @@ -5,6 +5,9 @@ import ( "encoding/json" "fmt" + "github.com/okp4/okp4d/x/logic/client/cli" + "github.com/spf13/cobra" + "github.com/grpc-ecosystem/grpc-gateway/runtime" "cosmossdk.io/core/appmodule" @@ -77,6 +80,12 @@ func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *r _ = types.RegisterQueryServiceHandlerClient(context.Background(), mux, types.NewQueryServiceClient(clientCtx)) } +// GetQueryCmd returns the root query command for the module. The subcommands of this root command are used by end-users +// to generate new queries to the subset of the state defined by the module. +func (AppModuleBasic) GetQueryCmd() *cobra.Command { + return cli.GetQueryCmd() +} + // ---------------------------------------------------------------------------- // AppModule // ---------------------------------------------------------------------------- diff --git a/x/logic/types/types.go b/x/logic/types/types.go deleted file mode 100644 index 71795b56..00000000 --- a/x/logic/types/types.go +++ /dev/null @@ -1,10 +0,0 @@ -package types - -import "google.golang.org/grpc" - -// GrpcQueryServiceDesc represents the query server's RPC service specification. -// This gives access to the service name and method names needed for stargate -// queries. -func GrpcQueryServiceDesc() grpc.ServiceDesc { - return _QueryService_serviceDesc -}