From 6196635cb9adec63126032733218982db2d52b53 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Sat, 25 Nov 2023 16:24:09 +0100 Subject: [PATCH] feat: support autocli (#3694) --- Makefile | 6 +- changelog.md | 3 +- ignite/cmd/generate.go | 1 - ignite/cmd/generate_pulsar.go | 53 ----- ignite/pkg/cosmosgen/cosmosgen.go | 21 +- ignite/pkg/cosmosgen/generate_go.go | 2 +- ignite/services/chain/generate.go | 12 -- ignite/services/scaffolder/init.go | 9 +- ignite/services/scaffolder/scaffolder.go | 7 +- .../client/cli/query_{{queryName}}.go.plush | 66 ------ .../client/cli/tx_oracle.go.plush | 10 - .../client/cli/tx_{{queryName}}.go.plush | 118 ---------- .../client/cli/tx_{{packetName}}.go.plush | 3 + ignite/templates/ibc/oracle.go | 56 ++++- ignite/templates/ibc/packet.go | 23 +- ignite/templates/ibc/placeholders.go | 4 + .../templates/packet/tx.tpl} | 18 +- .../client/cli/tx_{{msgName}}.go.plush | 44 ---- ignite/templates/message/message.go | 31 ++- .../{{moduleName}}/client/cli/query.go.plush | 32 --- .../client/cli/query_params.go.plush | 36 ---- .../client/cli/suite_test.go.plush | 46 ---- .../x/{{moduleName}}/module/autocli.go.plush | 35 +++ .../x/{{moduleName}}/module/module.go.plush | 27 +-- .../client/cli/query_{{queryName}}.go.plush | 53 ----- ignite/templates/query/placeholders.go | 4 +- ignite/templates/query/query.go | 24 ++- .../client/cli/query_{{typeName}}.go.plush | 82 ------- .../cli/query_{{typeName}}_test.go.plush | 148 ------------- .../client/cli/tx_{{typeName}}.go.plush | 99 --------- .../client/cli/tx_{{typeName}}_test.go.plush | 180 ---------------- ignite/templates/typed/list/list.go | 69 ++++-- .../client/cli/query_{{typeName}}.go.plush | 81 ------- .../client/cli/tx_{{typeName}}.go.plush | 108 ---------- .../cli/query_{{typeName}}_test.go.plush | 160 -------------- .../client/cli/tx_{{typeName}}_test.go.plush | 203 ------------------ ignite/templates/typed/map/map.go | 78 +++++-- ignite/templates/typed/placeholders.go | 4 + .../client/cli/query_{{typeName}}.go.plush | 38 ---- .../cli/query_{{typeName}}_test.go.plush | 68 ------ .../client/cli/tx_{{typeName}}.go.plush | 87 -------- .../client/cli/tx_{{typeName}}_test.go.plush | 151 ------------- ignite/templates/typed/singleton/singleton.go | 63 ++++-- 43 files changed, 367 insertions(+), 1996 deletions(-) delete mode 100644 ignite/cmd/generate_pulsar.go delete mode 100644 ignite/templates/ibc/files/oracle/x/{{moduleName}}/client/cli/query_{{queryName}}.go.plush delete mode 100644 ignite/templates/ibc/files/oracle/x/{{moduleName}}/client/cli/tx_oracle.go.plush delete mode 100644 ignite/templates/ibc/files/oracle/x/{{moduleName}}/client/cli/tx_{{queryName}}.go.plush rename ignite/templates/{module/create/files/base/x/{{moduleName}}/client/cli/tx.go.plush => ibc/templates/packet/tx.tpl} (53%) delete mode 100644 ignite/templates/message/files/message/x/{{moduleName}}/client/cli/tx_{{msgName}}.go.plush delete mode 100644 ignite/templates/module/create/files/base/x/{{moduleName}}/client/cli/query.go.plush delete mode 100644 ignite/templates/module/create/files/base/x/{{moduleName}}/client/cli/query_params.go.plush delete mode 100644 ignite/templates/module/create/files/base/x/{{moduleName}}/client/cli/suite_test.go.plush create mode 100644 ignite/templates/module/create/files/base/x/{{moduleName}}/module/autocli.go.plush delete mode 100644 ignite/templates/query/files/x/{{moduleName}}/client/cli/query_{{queryName}}.go.plush delete mode 100644 ignite/templates/typed/list/files/component/x/{{moduleName}}/client/cli/query_{{typeName}}.go.plush delete mode 100644 ignite/templates/typed/list/files/component/x/{{moduleName}}/client/cli/query_{{typeName}}_test.go.plush delete mode 100644 ignite/templates/typed/list/files/messages/x/{{moduleName}}/client/cli/tx_{{typeName}}.go.plush delete mode 100644 ignite/templates/typed/list/files/messages/x/{{moduleName}}/client/cli/tx_{{typeName}}_test.go.plush delete mode 100644 ignite/templates/typed/map/files/component/x/{{moduleName}}/client/cli/query_{{typeName}}.go.plush delete mode 100644 ignite/templates/typed/map/files/messages/x/{{moduleName}}/client/cli/tx_{{typeName}}.go.plush delete mode 100644 ignite/templates/typed/map/files/tests/component/x/{{moduleName}}/client/cli/query_{{typeName}}_test.go.plush delete mode 100644 ignite/templates/typed/map/files/tests/messages/x/{{moduleName}}/client/cli/tx_{{typeName}}_test.go.plush delete mode 100644 ignite/templates/typed/singleton/files/component/x/{{moduleName}}/client/cli/query_{{typeName}}.go.plush delete mode 100644 ignite/templates/typed/singleton/files/component/x/{{moduleName}}/client/cli/query_{{typeName}}_test.go.plush delete mode 100644 ignite/templates/typed/singleton/files/messages/x/{{moduleName}}/client/cli/tx_{{typeName}}.go.plush delete mode 100644 ignite/templates/typed/singleton/files/messages/x/{{moduleName}}/client/cli/tx_{{typeName}}_test.go.plush diff --git a/Makefile b/Makefile index 7292ecdc66..97c20de6d9 100644 --- a/Makefile +++ b/Makefile @@ -54,9 +54,13 @@ format: ## lint: Run Golang CI Lint. lint: - @echo Running gocilint... + @echo Running golangci-lint... @go run github.com/golangci/golangci-lint/cmd/golangci-lint run --out-format=tab --issues-exit-code=0 +lint-fix: + @echo Running golangci-lint... + @go run github.com/golangci/golangci-lint/cmd/golangci-lint run --fix --out-format=tab --issues-exit-code=0 + .PHONY: govet format lint ## proto-all: Format, lint and generate code from proto files using buf. diff --git a/changelog.md b/changelog.md index d7f1dfbe1c..6211360d24 100644 --- a/changelog.md +++ b/changelog.md @@ -4,6 +4,7 @@ ### Features +- [#3694](https://github.com/ignite/cli/pull/3694) Query and Tx AutoCLI support - [#3544](https://github.com/ignite/cli/pull/3544) Add bidirectional communication to plugin system - [#3561](https://github.com/ignite/cli/pull/3561) Add GetChainInfo method to plugin system API - [#3626](https://github.com/ignite/cli/pull/3626) Add logging levels to relayer @@ -787,4 +788,4 @@ Our new name is **Ignite CLI**! ## `v0.0.9` -Initial release. +Initial release. \ No newline at end of file diff --git a/ignite/cmd/generate.go b/ignite/cmd/generate.go index 9e366c012a..4e3ad4d602 100644 --- a/ignite/cmd/generate.go +++ b/ignite/cmd/generate.go @@ -33,7 +33,6 @@ meant to be edited by hand. flagSetClearCache(c) c.AddCommand(NewGenerateGo()) - c.AddCommand(NewGeneratePulsar()) c.AddCommand(NewGenerateTSClient()) c.AddCommand(NewGenerateVuex()) c.AddCommand(NewGenerateComposables()) diff --git a/ignite/cmd/generate_pulsar.go b/ignite/cmd/generate_pulsar.go deleted file mode 100644 index 080063d980..0000000000 --- a/ignite/cmd/generate_pulsar.go +++ /dev/null @@ -1,53 +0,0 @@ -package ignitecmd - -import ( - "github.com/spf13/cobra" - - "github.com/ignite/cli/ignite/pkg/cliui" - "github.com/ignite/cli/ignite/pkg/cliui/icons" - "github.com/ignite/cli/ignite/services/chain" -) - -func NewGeneratePulsar() *cobra.Command { - c := &cobra.Command{ - Use: "proto-pulsar", - Short: "Compile protocol buffer files to Go pulsar source code required by Cosmos SDK", - RunE: generatePulsarHandler, - } - - c.Flags().AddFlagSet(flagSetYes()) - - return c -} - -func generatePulsarHandler(cmd *cobra.Command, _ []string) error { - session := cliui.New(cliui.StartSpinnerWithText(statusGenerating)) - defer session.End() - - c, err := newChainWithHomeFlags( - cmd, - chain.WithOutputer(session), - chain.CollectEvents(session.EventBus()), - chain.CheckCosmosSDKVersion(), - ) - if err != nil { - return err - } - - cacheStorage, err := newCache(cmd) - if err != nil { - return err - } - - var opts []chain.GenerateTarget - if flagGetEnableProtoVendor(cmd) { - opts = append(opts, chain.GenerateProtoVendor()) - } - - err = c.Generate(cmd.Context(), cacheStorage, chain.GeneratePulsar(), opts...) - if err != nil { - return err - } - - return session.Println(icons.OK, "Generated Go pulsar code") -} diff --git a/ignite/pkg/cosmosgen/cosmosgen.go b/ignite/pkg/cosmosgen/cosmosgen.go index 1e08ad0006..507a90c369 100644 --- a/ignite/pkg/cosmosgen/cosmosgen.go +++ b/ignite/pkg/cosmosgen/cosmosgen.go @@ -22,8 +22,7 @@ type generateOptions struct { updateBufModule bool ev events.Bus - isGoEnabled bool - isPulsarEnabled bool + generateProtobuf bool jsOut func(module.Module) string tsClientRootPath string @@ -77,17 +76,10 @@ func WithHooksGeneration(out ModulePathFunc, hooksRootPath string) Option { } } -// WithGoGeneration adds Go code generation. +// WithGoGeneration adds protobuf (gogoproto and pulsar) code generation. func WithGoGeneration() Option { return func(o *generateOptions) { - o.isGoEnabled = true - } -} - -// WithPulsarGeneration adds Go pulsar code generation. -func WithPulsarGeneration() Option { - return func(o *generateOptions) { - o.isPulsarEnabled = true + o.generateProtobuf = true } } @@ -189,12 +181,11 @@ func Generate(ctx context.Context, cacheStorage cache.Storage, appPath, protoDir // Go generation must run first so the types are created before other // generated code that requires sdk.Msg implementations to be defined - if g.opts.isGoEnabled { - if err := g.generateGo(ctx); err != nil { + if g.opts.generateProtobuf { + if err := g.generateGoGo(ctx); err != nil { return err } - } - if g.opts.isPulsarEnabled { + if err := g.generatePulsar(ctx); err != nil { return err } diff --git a/ignite/pkg/cosmosgen/generate_go.go b/ignite/pkg/cosmosgen/generate_go.go index 8423c17fca..3f6d70cc8d 100644 --- a/ignite/pkg/cosmosgen/generate_go.go +++ b/ignite/pkg/cosmosgen/generate_go.go @@ -18,7 +18,7 @@ func (g *generator) pulsarTemplate() string { return filepath.Join(g.appPath, g.protoDir, "buf.gen.pulsar.yaml") } -func (g *generator) generateGo(ctx context.Context) error { +func (g *generator) generateGoGo(ctx context.Context) error { // create a temporary dir to locate generated code under which later only some of them will be moved to the // app's source code. this also prevents having leftover files in the app's source code or its parent dir - when // command executed directly there - in case of an interrupt. diff --git a/ignite/services/chain/generate.go b/ignite/services/chain/generate.go index 3ebcbfaedb..3660da12eb 100644 --- a/ignite/services/chain/generate.go +++ b/ignite/services/chain/generate.go @@ -18,7 +18,6 @@ type generateOptions struct { useCache bool isProtoVendorEnabled bool isGoEnabled bool - isPulsarEnabled bool isTSClientEnabled bool isComposablesEnabled bool isHooksEnabled bool @@ -40,13 +39,6 @@ func GenerateGo() GenerateTarget { } } -// GeneratePulsar enables generating proto based Go code needed for the chain's source code. -func GeneratePulsar() GenerateTarget { - return func(o *generateOptions) { - o.isPulsarEnabled = true - } -} - // GenerateTSClient enables generating proto based Typescript Client. // The path assigns the output path to use for the generated Typescript client // overriding the configured or default path. Path can be an empty string. @@ -178,10 +170,6 @@ func (c *Chain) Generate( options = append(options, cosmosgen.WithGoGeneration()) } - if targetOptions.isPulsarEnabled { - options = append(options, cosmosgen.WithPulsarGeneration()) - } - if targetOptions.isProtoVendorEnabled { options = append(options, cosmosgen.UpdateBufModule()) } diff --git a/ignite/services/scaffolder/init.go b/ignite/services/scaffolder/init.go index 0c0d990c91..5e3b9000ae 100644 --- a/ignite/services/scaffolder/init.go +++ b/ignite/services/scaffolder/init.go @@ -9,7 +9,7 @@ import ( "github.com/gobuffalo/genny/v2" "github.com/ignite/cli/ignite/pkg/cache" - "github.com/ignite/cli/ignite/pkg/gocmd" + "github.com/ignite/cli/ignite/pkg/cosmosgen" "github.com/ignite/cli/ignite/pkg/gomodulepath" "github.com/ignite/cli/ignite/pkg/placeholder" "github.com/ignite/cli/ignite/pkg/xgit" @@ -116,6 +116,10 @@ func generate( return err } + if err := cosmosgen.InstallDepTools(ctx, absRoot); err != nil { + return err + } + // generate module template if !noDefaultModule { opts := &modulecreate.CreateOptions{ @@ -143,5 +147,6 @@ func generate( } } - return gocmd.ModTidy(ctx, absRoot) + + return nil } diff --git a/ignite/services/scaffolder/scaffolder.go b/ignite/services/scaffolder/scaffolder.go index 6052ddae13..6a2dc82f61 100644 --- a/ignite/services/scaffolder/scaffolder.go +++ b/ignite/services/scaffolder/scaffolder.go @@ -76,10 +76,6 @@ func finish(ctx context.Context, cacheStorage cache.Storage, path, gomodPath str } func protoc(ctx context.Context, cacheStorage cache.Storage, projectPath, gomodPath string) error { - if err := cosmosgen.InstallDepTools(ctx, projectPath); err != nil { - return err - } - confpath, err := chainconfig.LocateDefault(projectPath) if err != nil { return err @@ -90,9 +86,8 @@ func protoc(ctx context.Context, cacheStorage cache.Storage, projectPath, gomodP } options := []cosmosgen.Option{ - cosmosgen.WithGoGeneration(), - cosmosgen.WithPulsarGeneration(), cosmosgen.UpdateBufModule(), + cosmosgen.WithGoGeneration(), cosmosgen.IncludeDirs(conf.Build.Proto.ThirdPartyPaths), } diff --git a/ignite/templates/ibc/files/oracle/x/{{moduleName}}/client/cli/query_{{queryName}}.go.plush b/ignite/templates/ibc/files/oracle/x/{{moduleName}}/client/cli/query_{{queryName}}.go.plush deleted file mode 100644 index 514e103d16..0000000000 --- a/ignite/templates/ibc/files/oracle/x/{{moduleName}}/client/cli/query_{{queryName}}.go.plush +++ /dev/null @@ -1,66 +0,0 @@ -package cli - -import ( - "strconv" - - "github.com/cosmos/cosmos-sdk/client" - "github.com/cosmos/cosmos-sdk/client/flags" - "github.com/spf13/cobra" - "<%= ModulePath %>/x/<%= moduleName %>/types" -) - -// Cmd<%= queryName.UpperCamel %>Result queries request result by reqID -func Cmd<%= queryName.UpperCamel %>Result() *cobra.Command { - cmd := &cobra.Command{ - Use: "<%= queryName.Kebab %>-result [request-id]", - Short: "Query the <%= queryName.UpperCamel %> result data by id", - Args: cobra.ExactArgs(1), - RunE: func(cmd *cobra.Command, args []string) error { - clientCtx, err := client.GetClientQueryContext(cmd) - if err != nil { - return err - } - id, err := strconv.ParseInt(args[0], 10, 64) - if err != nil { - return err - } - queryClient := types.NewQueryClient(clientCtx) - r, err := queryClient.<%= queryName.UpperCamel %>Result(context.Background(), &types.Query<%= queryName.UpperCamel %>Request{RequestId: id}) - if err != nil { - return err - } - - return clientCtx.PrintProto(r) - }, - } - - flags.AddQueryFlagsToCmd(cmd) - - return cmd -} - -// CmdLast<%= queryName.UpperCamel %>ID queries latest request -func CmdLast<%= queryName.UpperCamel %>ID() *cobra.Command { - cmd := &cobra.Command{ - Use: "last-<%= queryName.Kebab %>-id", - Short: "Query the last request id returned by <%= queryName.UpperCamel %> ack packet", - Args: cobra.NoArgs, - RunE: func(cmd *cobra.Command, args []string) error { - clientCtx, err := client.GetClientQueryContext(cmd) - if err != nil { - return err - } - queryClient := types.NewQueryClient(clientCtx) - r, err := queryClient.Last<%= queryName.UpperCamel %>Id(cmd.Context(), &types.QueryLast<%= queryName.UpperCamel %>IdRequest{}) - if err != nil { - return err - } - - return clientCtx.PrintProto(r) - }, - } - - flags.AddQueryFlagsToCmd(cmd) - - return cmd -} diff --git a/ignite/templates/ibc/files/oracle/x/{{moduleName}}/client/cli/tx_oracle.go.plush b/ignite/templates/ibc/files/oracle/x/{{moduleName}}/client/cli/tx_oracle.go.plush deleted file mode 100644 index 85ee6da6d2..0000000000 --- a/ignite/templates/ibc/files/oracle/x/{{moduleName}}/client/cli/tx_oracle.go.plush +++ /dev/null @@ -1,10 +0,0 @@ -package cli - -const ( - flagChannel = "channel" - flagSymbols = "symbols" - flagMultiplier = "multiplier" - flagFeeLimit = "fee-limit" - flagPrepareGas = "prepare-gas" - flagExecuteGas = "execute-gas" -) diff --git a/ignite/templates/ibc/files/oracle/x/{{moduleName}}/client/cli/tx_{{queryName}}.go.plush b/ignite/templates/ibc/files/oracle/x/{{moduleName}}/client/cli/tx_{{queryName}}.go.plush deleted file mode 100644 index 931529fa57..0000000000 --- a/ignite/templates/ibc/files/oracle/x/{{moduleName}}/client/cli/tx_{{queryName}}.go.plush +++ /dev/null @@ -1,118 +0,0 @@ -package cli - -import ( - "strconv" - - "github.com/spf13/cobra" - - "github.com/cosmos/cosmos-sdk/client" - "github.com/cosmos/cosmos-sdk/client/flags" - "github.com/cosmos/cosmos-sdk/client/tx" - sdk "github.com/cosmos/cosmos-sdk/types" - "<%= ModulePath %>/x/<%= moduleName %>/types" -) - -// CmdRequest<%= queryName.UpperCamel %>Data creates and broadcast a <%= queryName.UpperCamel %> request transaction -func CmdRequest<%= queryName.UpperCamel %>Data() *cobra.Command { - cmd := &cobra.Command{ - Use: "<%= queryName.Kebab %>-data [oracle-script-id] [requested-validator-count] [sufficient-validator-count]", - Short: "Make a new <%= queryName.UpperCamel %> query request via an existing BandChain oracle script", - Args: cobra.ExactArgs(3), - RunE: func(cmd *cobra.Command, args []string) error { - // retrieve the oracle script id. - uint64OracleScriptID, err := strconv.ParseUint(args[0], 10, 64) - if err != nil { - return err - } - oracleScriptID := types.OracleScriptID(uint64OracleScriptID) - - // retrieve the requested validator count. - askCount, err := strconv.ParseUint(args[1], 10, 64) - if err != nil { - return err - } - - // retrieve the sufficient(minimum) validator count. - minCount, err := strconv.ParseUint(args[2], 10, 64) - if err != nil { - return err - } - - channel, err := cmd.Flags().GetString(flagChannel) - if err != nil { - return err - } - - // retrieve the list of symbols for the requested oracle script. - symbols, err := cmd.Flags().GetStringSlice(flagSymbols) - if err != nil { - return err - } - - // retrieve the multiplier for the symbols' price. - multiplier, err := cmd.Flags().GetUint64(flagMultiplier) - if err != nil { - return err - } - - calldata := &types.<%= queryName.UpperCamel %>CallData{ - Symbols: symbols, - Multiplier: multiplier, - } - - // retrieve the amount of coins allowed to be paid for oracle request fee from the pool account. - coinStr, err := cmd.Flags().GetString(flagFeeLimit) - if err != nil { - return err - } - feeLimit, err := sdk.ParseCoinsNormalized(coinStr) - if err != nil { - return err - } - - // retrieve the amount of gas allowed for the prepare step of the oracle script. - prepareGas, err := cmd.Flags().GetUint64(flagPrepareGas) - if err != nil { - return err - } - - // retrieve the amount of gas allowed for the execute step of the oracle script. - executeGas, err := cmd.Flags().GetUint64(flagExecuteGas) - if err != nil { - return err - } - - clientCtx, err := client.GetClientTxContext(cmd) - if err != nil { - return err - } - - msg := types.NewMsg<%= queryName.UpperCamel %>Data( - clientCtx.GetFromAddress().String(), - oracleScriptID, - channel, - calldata, - askCount, - minCount, - feeLimit, - prepareGas, - executeGas, - ) - if err := msg.ValidateBasic(); err != nil { - return err - } - return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg) - }, - } - - cmd.Flags().String(flagChannel, "", "The channel id") - cmd.MarkFlagRequired(flagChannel) - cmd.Flags().StringSlice(flagSymbols, nil, "Symbols used in calling the oracle script") - cmd.Flags().Uint64(flagMultiplier, 1000000, "Multiplier used in calling the oracle script") - cmd.Flags().String(flagFeeLimit, "", "the maximum tokens that will be paid to all data source providers") - cmd.Flags().Uint64(flagPrepareGas, 200000, "Prepare gas used in fee counting for prepare request") - cmd.Flags().Uint64(flagExecuteGas, 200000, "Execute gas used in fee counting for execute request") - flags.AddTxFlagsToCmd(cmd) - - return cmd -} diff --git a/ignite/templates/ibc/files/packet/messages/x/{{moduleName}}/client/cli/tx_{{packetName}}.go.plush b/ignite/templates/ibc/files/packet/messages/x/{{moduleName}}/client/cli/tx_{{packetName}}.go.plush index 164beba9a4..647cfda4c8 100644 --- a/ignite/templates/ibc/files/packet/messages/x/{{moduleName}}/client/cli/tx_{{packetName}}.go.plush +++ b/ignite/templates/ibc/files/packet/messages/x/{{moduleName}}/client/cli/tx_{{packetName}}.go.plush @@ -14,7 +14,10 @@ import ( var _ = strconv.Itoa(0) +// This command does not use AutoCLI because it gives a better UX to do not. func CmdSend<%= packetName.UpperCamel %>() *cobra.Command { + flagPacketTimeoutTimestamp := "packet-timeout-timestamp" + cmd := &cobra.Command{ Use: "send-<%= packetName.Kebab %> [src-port] [src-channel]<%= fields.String() %>", Short: "Send a <%= packetName.Original %> over IBC", diff --git a/ignite/templates/ibc/oracle.go b/ignite/templates/ibc/oracle.go index e58f574286..25a031f1ee 100644 --- a/ignite/templates/ibc/oracle.go +++ b/ignite/templates/ibc/oracle.go @@ -234,17 +234,31 @@ message Msg%[2]vDataResponse { // Deprecated: This function is no longer maintained. func clientCliQueryOracleModify(replacer placeholder.Replacer, opts *OracleOptions) genny.RunFn { return func(r *genny.Runner) error { - path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "client/cli/query.go") + path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "module/autocli.go") f, err := r.Disk.Find(path) if err != nil { return err } - template := ` - cmd.AddCommand(Cmd%[2]vResult()) - cmd.AddCommand(CmdLast%[2]vID()) -%[1]v` - replacement := fmt.Sprintf(template, Placeholder, opts.QueryName.UpperCamel) - content := replacer.Replace(f.String(), Placeholder, replacement) + + template := `{ + RpcMethod: "%[2]vResult", + Use: "%[3]v-result [request-id]", + Short: "Query the %[2]v result data by id", + PositionalArgs: []*autocliv1.PositionalArgDescriptor{{ProtoField: "request_id"}}, + }, + { + RpcMethod: "Last%[2]vId", + Use: "last-%[3]v-id", + Short: "Query the last request id returned by %[2]v ack packet", + }, + %[1]v` + + replacement := fmt.Sprintf(template, + PlaceholderAutoCLIQuery, + opts.QueryName.UpperCamel, + opts.QueryName.Kebab, + ) + content := replacer.Replace(f.String(), PlaceholderAutoCLIQuery, replacement) newFile := genny.NewFileS(path, content) return r.File(newFile) } @@ -253,15 +267,35 @@ func clientCliQueryOracleModify(replacer placeholder.Replacer, opts *OracleOptio // Deprecated: This function is no longer maintained. func clientCliTxOracleModify(replacer placeholder.Replacer, opts *OracleOptions) genny.RunFn { return func(r *genny.Runner) error { - path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "client/cli/tx.go") + path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "module/autocli.go") f, err := r.Disk.Find(path) if err != nil { return err } - template := `cmd.AddCommand(CmdRequest%[2]vData()) + + template := `{ + RpcMethod: "%[2]v", + Use: "%[3]v-data [oracle-script-id] [requested-validator-count] [sufficient-validator-count]", + Short: "Make a new %[2]v query request via an existing BandChain oracle script", + PositionalArgs: []*autocliv1.PositionalArgDescriptor{{ProtoField: "oracle_script_id"}, {ProtoField: "ask_count"}, {ProtoField: "min_count"}}, + FlagOptions: map[string]*autocliv1.FlagOptions{ + "source_channel": {Name: "channel", Usage: "The channel id"}, + "calldata": {Name: "calldata", DefaultValue: '{"multiplier": 1000000, symbols: []}', Usage: "Symbols and multiplier used in calling the oracle script"}, + "fee_limit": {Name: "fee-limit", Usage: "The maximum tokens that will be paid to all data source providers"}, + "prepare_gas": {Name: "prepare-gas", DefaultValue: "200000", Usage: "Prepare gas used in fee counting for prepare request"}, + "execute_gas": {Name: "execute-gas", DefaultValue: "200000", Usage: "Execute gas used in fee counting for execute request"}, + }, + }, + %[1]v` - replacement := fmt.Sprintf(template, Placeholder, opts.QueryName.UpperCamel) - content := replacer.Replace(f.String(), Placeholder, replacement) + + replacement := fmt.Sprintf( + template, + PlaceholderAutoCLITx, + opts.QueryName.UpperCamel, + opts.QueryName.Kebab, + ) + content := replacer.Replace(f.String(), PlaceholderAutoCLITx, replacement) newFile := genny.NewFileS(path, content) return r.File(newFile) } diff --git a/ignite/templates/ibc/packet.go b/ignite/templates/ibc/packet.go index 81dc2d3484..d8a4e5002e 100644 --- a/ignite/templates/ibc/packet.go +++ b/ignite/templates/ibc/packet.go @@ -2,7 +2,9 @@ package ibc import ( "embed" + "errors" "fmt" + "os" "path/filepath" "github.com/emicklei/proto" @@ -346,10 +348,25 @@ func protoTxModify(opts *PacketOptions) genny.RunFn { } } +//go:embed templates/packet/tx.tpl +var txTemplate string + +// clientCliTxModify does not use AutoCLI here, because it as a better UX as it is. func clientCliTxModify(replacer placeholder.Replacer, opts *PacketOptions) genny.RunFn { return func(r *genny.Runner) error { - path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "client/cli/tx.go") - f, err := r.Disk.Find(path) + filePath := filepath.Join(opts.AppPath, "x", opts.ModuleName, "client/cli/tx.go") + if _, err := os.Stat(filePath); errors.Is(err, os.ErrNotExist) { + content := fmt.Sprintf(txTemplate, opts.ModulePath, opts.ModuleName) + if err := os.MkdirAll(filepath.Dir(filePath), os.ModePerm); err != nil { + return err + } + + if err := os.WriteFile(filePath, []byte(content), 0o644); err != nil { + return err + } + } + + f, err := r.Disk.Find(filePath) if err != nil { return err } @@ -357,7 +374,7 @@ func clientCliTxModify(replacer placeholder.Replacer, opts *PacketOptions) genny %[1]v` replacement := fmt.Sprintf(template, Placeholder, opts.PacketName.UpperCamel) content := replacer.Replace(f.String(), Placeholder, replacement) - newFile := genny.NewFileS(path, content) + newFile := genny.NewFileS(filePath, content) return r.File(newFile) } } diff --git a/ignite/templates/ibc/placeholders.go b/ignite/templates/ibc/placeholders.go index f74dbcda7b..3b25117e28 100644 --- a/ignite/templates/ibc/placeholders.go +++ b/ignite/templates/ibc/placeholders.go @@ -22,4 +22,8 @@ const ( PlaceholderOraclePacketModuleAck = "// this line is used by starport scaffolding # oracle/packet/module/ack" PlaceholderOracleModuleRecv = "// this line is used by starport scaffolding # oracle/module/recv" PlaceholderOracleModuleAck = "// this line is used by starport scaffolding # oracle/module/ack" + + // Placeholders AutoCLI + PlaceholderAutoCLIQuery = "// this line is used by ignite scaffolding # autocli/query" + PlaceholderAutoCLITx = "// this line is used by ignite scaffolding # autocli/tx" ) diff --git a/ignite/templates/module/create/files/base/x/{{moduleName}}/client/cli/tx.go.plush b/ignite/templates/ibc/templates/packet/tx.tpl similarity index 53% rename from ignite/templates/module/create/files/base/x/{{moduleName}}/client/cli/tx.go.plush rename to ignite/templates/ibc/templates/packet/tx.tpl index 8f0d2003fc..1d09bbccfa 100644 --- a/ignite/templates/module/create/files/base/x/{{moduleName}}/client/cli/tx.go.plush +++ b/ignite/templates/ibc/templates/packet/tx.tpl @@ -7,24 +7,18 @@ import ( "github.com/spf13/cobra" "github.com/cosmos/cosmos-sdk/client" - // "github.com/cosmos/cosmos-sdk/client/flags" - "<%= modulePath %>/x/<%= moduleName %>/types" + "%s/x/%s/types" ) -var ( - DefaultRelativePacketTimeoutTimestamp = uint64((time.Duration(10) * time.Minute).Nanoseconds()) -) - -const ( - flagPacketTimeoutTimestamp = "packet-timeout-timestamp" - listSeparator = "," -) +var DefaultRelativePacketTimeoutTimestamp = uint64((time.Duration(10) * time.Minute).Nanoseconds()) + +const listSeparator = "," // GetTxCmd returns the transaction commands for this module func GetTxCmd() *cobra.Command { cmd := &cobra.Command{ Use: types.ModuleName, - Short: fmt.Sprintf("%s transactions subcommands", types.ModuleName), + Short: fmt.Sprintf("%%s transactions subcommands", types.ModuleName), DisableFlagParsing: true, SuggestionsMinimumDistance: 2, RunE: client.ValidateCmd, @@ -33,4 +27,4 @@ func GetTxCmd() *cobra.Command { // this line is used by starport scaffolding # 1 return cmd -} +} \ No newline at end of file diff --git a/ignite/templates/message/files/message/x/{{moduleName}}/client/cli/tx_{{msgName}}.go.plush b/ignite/templates/message/files/message/x/{{moduleName}}/client/cli/tx_{{msgName}}.go.plush deleted file mode 100644 index 304dfee081..0000000000 --- a/ignite/templates/message/files/message/x/{{moduleName}}/client/cli/tx_{{msgName}}.go.plush +++ /dev/null @@ -1,44 +0,0 @@ -package cli - -import ( - "strconv" - <%= for (goImport) in mergeGoImports(Fields) { %> - <%= goImport.Alias %> "<%= goImport.Name %>"<% } %> - "github.com/spf13/cobra" - "github.com/cosmos/cosmos-sdk/client" - "github.com/cosmos/cosmos-sdk/client/flags" - "github.com/cosmos/cosmos-sdk/client/tx" - "<%= ModulePath %>/x/<%= ModuleName %>/types" -) - -var _ = strconv.Itoa(0) - -func Cmd<%= MsgName.UpperCamel %>() *cobra.Command { - cmd := &cobra.Command{ - Use: "<%= MsgName.Kebab %><%= Fields.String() %>", - Short: "<%= MsgDesc %>", - Args: cobra.ExactArgs(<%= len(Fields) %>), - RunE: func(cmd *cobra.Command, args []string) (err error) { - <%= for (i, field) in Fields { %> <%= field.CLIArgs("arg", i) %> - <% } %> - clientCtx, err := client.GetClientTxContext(cmd) - if err != nil { - return err - } - - msg := types.NewMsg<%= MsgName.UpperCamel %>( - clientCtx.GetFromAddress().String(), - <%= for (i, field) in Fields { %>arg<%= field.Name.UpperCamel %>, - <% } %> - ) - if err := msg.ValidateBasic(); err != nil { - return err - } - return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg) - }, - } - - flags.AddTxFlagsToCmd(cmd) - - return cmd -} \ No newline at end of file diff --git a/ignite/templates/message/message.go b/ignite/templates/message/message.go index c3ff5c4cf4..4f292bcf90 100644 --- a/ignite/templates/message/message.go +++ b/ignite/templates/message/message.go @@ -4,6 +4,7 @@ import ( "embed" "fmt" "path/filepath" + "strings" "github.com/emicklei/proto" "github.com/gobuffalo/genny/v2" @@ -185,15 +186,35 @@ func typesCodecModify(replacer placeholder.Replacer, opts *Options) genny.RunFn func clientCliTxModify(replacer placeholder.Replacer, opts *Options) genny.RunFn { return func(r *genny.Runner) error { - path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "client/cli/tx.go") + path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "module/autocli.go") f, err := r.Disk.Find(path) if err != nil { return err } - template := `cmd.AddCommand(Cmd%[2]v()) -%[1]v` - replacement := fmt.Sprintf(template, Placeholder, opts.MsgName.UpperCamel) - content := replacer.Replace(f.String(), Placeholder, replacement) + + var positionalArgs string + for _, field := range opts.Fields { + positionalArgs += fmt.Sprintf(`{ProtoField: "%s"}, `, field.ProtoFieldName()) + } + + template := `{ + RpcMethod: "%[2]v", + Use: "%[3]v", + Short: "Send a %[4]v tx", + PositionalArgs: []*autocliv1.PositionalArgDescriptor{%[5]s}, + }, + %[1]v` + + replacement := fmt.Sprintf( + template, + typed.PlaceholderAutoCLITx, + opts.MsgName.UpperCamel, + strings.TrimSpace(fmt.Sprintf("%s%s", opts.MsgName.Kebab, opts.Fields.String())), + opts.MsgName.Original, + strings.TrimSpace(positionalArgs), + ) + + content := replacer.Replace(f.String(), typed.PlaceholderAutoCLITx, replacement) newFile := genny.NewFileS(path, content) return r.File(newFile) } diff --git a/ignite/templates/module/create/files/base/x/{{moduleName}}/client/cli/query.go.plush b/ignite/templates/module/create/files/base/x/{{moduleName}}/client/cli/query.go.plush deleted file mode 100644 index 971e5b47a9..0000000000 --- a/ignite/templates/module/create/files/base/x/{{moduleName}}/client/cli/query.go.plush +++ /dev/null @@ -1,32 +0,0 @@ -package cli - -import ( - "fmt" - // "strings" - - "github.com/spf13/cobra" - - "github.com/cosmos/cosmos-sdk/client" - // "github.com/cosmos/cosmos-sdk/client/flags" - // sdk "github.com/cosmos/cosmos-sdk/types" - - "<%= modulePath %>/x/<%= moduleName %>/types" -) - -// GetQueryCmd returns the cli query commands for this module -func GetQueryCmd() *cobra.Command { - // Group <%= moduleName %> 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()) - // this line is used by starport scaffolding # 1 - - return cmd -} - diff --git a/ignite/templates/module/create/files/base/x/{{moduleName}}/client/cli/query_params.go.plush b/ignite/templates/module/create/files/base/x/{{moduleName}}/client/cli/query_params.go.plush deleted file mode 100644 index 96ed66284b..0000000000 --- a/ignite/templates/module/create/files/base/x/{{moduleName}}/client/cli/query_params.go.plush +++ /dev/null @@ -1,36 +0,0 @@ -package cli - -import ( - "github.com/cosmos/cosmos-sdk/client" - "github.com/cosmos/cosmos-sdk/client/flags" - "github.com/spf13/cobra" - - "<%= modulePath %>/x/<%= moduleName %>/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, err := client.GetClientQueryContext(cmd) - if err != nil { - return err - } - - queryClient := types.NewQueryClient(clientCtx) - - res, err := queryClient.Params(cmd.Context(), &types.QueryParamsRequest{}) - if err != nil { - return err - } - - return clientCtx.PrintProto(res) - }, - } - - flags.AddQueryFlagsToCmd(cmd) - - return cmd -} diff --git a/ignite/templates/module/create/files/base/x/{{moduleName}}/client/cli/suite_test.go.plush b/ignite/templates/module/create/files/base/x/{{moduleName}}/client/cli/suite_test.go.plush deleted file mode 100644 index 1cbe9641c2..0000000000 --- a/ignite/templates/module/create/files/base/x/{{moduleName}}/client/cli/suite_test.go.plush +++ /dev/null @@ -1,46 +0,0 @@ -package cli_test - -import ( - "testing" - - "github.com/cosmos/gogoproto/proto" - "github.com/stretchr/testify/suite" - - "<%= modulePath %>/testutil/network" - "<%= modulePath %>/x/<%= moduleName %>/types" -) - -type IntegrationTestSuite struct { - suite.Suite -} - -func (s *IntegrationTestSuite) network(state proto.Message) *network.Network { - s.T().Helper() - cfg := network.DefaultConfig() - if state != nil { - buf, err := cfg.Codec.MarshalJSON(state) - s.Require().NoError(err) - cfg.GenesisState[types.ModuleName] = buf - } - return network.New(s.T(), cfg) -} - -func (s *IntegrationTestSuite) SetupTest() { - s.T().Log("setting up test") -} - -func (s *IntegrationTestSuite) SetupSuite() { - s.T().Log("setting up integration test suite") -} - -func (s *IntegrationTestSuite) TearDownTest() { - s.T().Log("tearing down test") -} - -func (s *IntegrationTestSuite) TearDownSuite() { - s.T().Log("tearing down integration test suite") -} - -func TestIntegrationTestSuite(t *testing.T) { - suite.Run(t, new(IntegrationTestSuite)) -} diff --git a/ignite/templates/module/create/files/base/x/{{moduleName}}/module/autocli.go.plush b/ignite/templates/module/create/files/base/x/{{moduleName}}/module/autocli.go.plush new file mode 100644 index 0000000000..b6eb59d93a --- /dev/null +++ b/ignite/templates/module/create/files/base/x/{{moduleName}}/module/autocli.go.plush @@ -0,0 +1,35 @@ +package <%= moduleName %> + +import ( + autocliv1 "cosmossdk.io/api/cosmos/autocli/v1" + + modulev1 "<%= modulePath %>/api/<%= appName %>/<%= moduleName %>" +) + +// AutoCLIOptions implements the autocli.HasAutoCLIConfig interface. +func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions { + return &autocliv1.ModuleOptions{ + Query: &autocliv1.ServiceCommandDescriptor{ + Service: modulev1.Query_ServiceDesc.ServiceName, + RpcCommandOptions: []*autocliv1.RpcCommandOptions{ + { + RpcMethod: "Params", + Use: "params", + Short: "Shows the parameters of the module", + }, + // this line is used by ignite scaffolding # autocli/query + }, + }, + Tx: &autocliv1.ServiceCommandDescriptor{ + Service: modulev1.Msg_ServiceDesc.ServiceName, + EnhanceCustomCommand: true, // only required if you want to use the custom command + RpcCommandOptions: []*autocliv1.RpcCommandOptions{ + { + RpcMethod: "UpdateParams", + Skip: true, // skipped because authority gated + }, + // this line is used by ignite scaffolding # autocli/tx + }, + }, + } +} \ No newline at end of file diff --git a/ignite/templates/module/create/files/base/x/{{moduleName}}/module/module.go.plush b/ignite/templates/module/create/files/base/x/{{moduleName}}/module/module.go.plush index 42a97558b8..558c9e745d 100644 --- a/ignite/templates/module/create/files/base/x/{{moduleName}}/module/module.go.plush +++ b/ignite/templates/module/create/files/base/x/{{moduleName}}/module/module.go.plush @@ -17,7 +17,6 @@ import ( authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" "github.com/grpc-ecosystem/grpc-gateway/runtime" - "github.com/spf13/cobra" <%= if (isIBC) { %>porttypes "github.com/cosmos/ibc-go/v8/modules/core/05-port/types" capabilitykeeper "github.com/cosmos/ibc-go/modules/capability/keeper" ibckeeper "github.com/cosmos/ibc-go/v8/modules/core/keeper"<% } %> @@ -25,7 +24,6 @@ import ( // this line is used by starport scaffolding # 1 modulev1 "<%= modulePath %>/api/<%= appName %>/<%= moduleName %>/module" - "<%= modulePath %>/x/<%= moduleName %>/client/cli" "<%= modulePath %>/x/<%= moduleName %>/keeper" "<%= modulePath %>/x/<%= moduleName %>/types" ) @@ -93,17 +91,12 @@ func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *r } } -// GetTxCmd returns the root Tx command for the module. The subcommands of -// this root command are used by end-users to generate new transactions -// containing messages defined in the module. -func (a AppModuleBasic) GetTxCmd() *cobra.Command { - return cli.GetTxCmd() -} - -// 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() -} +// GetTxCmd returns the root Tx command for the module. +// These commands enrich the AutoCLI tx commands. +// When creating non AutoCLI commands, add the following: +// func (a AppModuleBasic) GetTxCmd() *cobra.Command { +// return cli.GetTxCmd() +// } // ---------------------------------------------------------------------------- // AppModule @@ -190,7 +183,7 @@ func init() { ) } -type <%= title(moduleName) %>Inputs struct { +type ModuleInputs struct { depinject.In StoreService store.KVStoreService @@ -206,14 +199,14 @@ type <%= title(moduleName) %>Inputs struct { CapabilityScopedFn func(string) capabilitykeeper.ScopedKeeper `optional:"true"`<% } %> } -type <%= title(moduleName) %>Outputs struct { +type ModuleOutputs struct { depinject.Out <%= title(moduleName) %>Keeper keeper.Keeper Module appmodule.AppModule } -func ProvideModule(in <%= title(moduleName) %>Inputs) <%= title(moduleName) %>Outputs { +func ProvideModule(in ModuleInputs) ModuleOutputs { // default to governance authority if not provided authority := authtypes.NewModuleAddress(govtypes.ModuleName) if in.Config.Authority != "" { @@ -235,5 +228,5 @@ func ProvideModule(in <%= title(moduleName) %>Inputs) <%= title(moduleName) %>Ou in.BankKeeper, ) - return <%= title(moduleName) %>Outputs{<%= title(moduleName) %>Keeper: k, Module: m} + return ModuleOutputs{<%= title(moduleName) %>Keeper: k, Module: m} } diff --git a/ignite/templates/query/files/x/{{moduleName}}/client/cli/query_{{queryName}}.go.plush b/ignite/templates/query/files/x/{{moduleName}}/client/cli/query_{{queryName}}.go.plush deleted file mode 100644 index 2d69d2ee17..0000000000 --- a/ignite/templates/query/files/x/{{moduleName}}/client/cli/query_{{queryName}}.go.plush +++ /dev/null @@ -1,53 +0,0 @@ -package cli - -import ( - "strconv" - <%= for (goImport) in mergeGoImports(ReqFields) { %> - <%= goImport.Alias %> "<%= goImport.Name %>"<% } %> - "github.com/spf13/cobra" - "github.com/cosmos/cosmos-sdk/client" - "github.com/cosmos/cosmos-sdk/client/flags" - "<%= ModulePath %>/x/<%= ModuleName %>/types" -) - -var _ = strconv.Itoa(0) - -func Cmd<%= QueryName.UpperCamel %>() *cobra.Command { - cmd := &cobra.Command{ - Use: "<%= QueryName.Kebab %><%= ReqFields.String() %>", - Short: "<%= Description %>", - Args: cobra.ExactArgs(<%= len(ReqFields) %>), - RunE: func(cmd *cobra.Command, args []string) (err error) { - <%= for (i, field) in ReqFields { %> <%= field.CLIArgs("req", i) %> - <% } %> - clientCtx, err := client.GetClientQueryContext(cmd) - if err != nil { - return err - } - - queryClient := types.NewQueryClient(clientCtx) - - params := &types.Query<%= QueryName.UpperCamel %>Request{ - <%= for (i, field) in ReqFields { %> - <%= field.Name.UpperCamel %>: req<%= field.Name.UpperCamel %>, <% } %> - } - - <%= if (Paginated) { %>pageReq, err := client.ReadPageRequest(cmd.Flags()) - if err != nil { - return err - } - params.Pagination = pageReq<% } %> - - res, err := queryClient.<%= QueryName.UpperCamel %>(cmd.Context(), params) - if err != nil { - return err - } - - return clientCtx.PrintProto(res) - }, - } - - flags.AddQueryFlagsToCmd(cmd) - - return cmd -} diff --git a/ignite/templates/query/placeholders.go b/ignite/templates/query/placeholders.go index 96fbde8f43..2268a356d1 100644 --- a/ignite/templates/query/placeholders.go +++ b/ignite/templates/query/placeholders.go @@ -1,5 +1,3 @@ package query -const ( - Placeholder = "// this line is used by starport scaffolding # 1" -) +const PlaceholderAutoCLIQuery = "// this line is used by ignite scaffolding # autocli/query" diff --git a/ignite/templates/query/query.go b/ignite/templates/query/query.go index 1072d5beca..6483e4e249 100644 --- a/ignite/templates/query/query.go +++ b/ignite/templates/query/query.go @@ -4,6 +4,7 @@ import ( "embed" "fmt" "path/filepath" + "strings" "github.com/emicklei/proto" @@ -146,21 +147,34 @@ func protoQueryModify(opts *Options) genny.RunFn { func cliQueryModify(replacer placeholder.Replacer, opts *Options) genny.RunFn { return func(r *genny.Runner) error { - path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "client/cli/query.go") + path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "module/autocli.go") f, err := r.Disk.Find(path) if err != nil { return err } - template := `cmd.AddCommand(Cmd%[2]v()) + var positionalArgs string + for _, field := range opts.ReqFields { + positionalArgs += fmt.Sprintf(`{ProtoField: "%s"}, `, field.ProtoFieldName()) + } + + template := `{ + RpcMethod: "%[2]v", + Use: "%[3]v", + Short: "%[4]v", + PositionalArgs: []*autocliv1.PositionalArgDescriptor{%[5]s}, + }, -%[1]v` + %[1]v` replacement := fmt.Sprintf( template, - Placeholder, + PlaceholderAutoCLIQuery, opts.QueryName.UpperCamel, + strings.TrimSpace(fmt.Sprintf("%s%s", opts.QueryName.Kebab, opts.ReqFields.String())), + opts.Description, + strings.TrimSpace(positionalArgs), ) - content := replacer.Replace(f.String(), Placeholder, replacement) + content := replacer.Replace(f.String(), PlaceholderAutoCLIQuery, replacement) newFile := genny.NewFileS(path, content) return r.File(newFile) diff --git a/ignite/templates/typed/list/files/component/x/{{moduleName}}/client/cli/query_{{typeName}}.go.plush b/ignite/templates/typed/list/files/component/x/{{moduleName}}/client/cli/query_{{typeName}}.go.plush deleted file mode 100644 index a904c6ce1e..0000000000 --- a/ignite/templates/typed/list/files/component/x/{{moduleName}}/client/cli/query_{{typeName}}.go.plush +++ /dev/null @@ -1,82 +0,0 @@ -package cli - -import ( - "strconv" - - "github.com/cosmos/cosmos-sdk/client" - "github.com/cosmos/cosmos-sdk/client/flags" - "github.com/spf13/cobra" - "<%= ModulePath %>/x/<%= ModuleName %>/types" -) - -func CmdList<%= TypeName.UpperCamel %>() *cobra.Command { - cmd := &cobra.Command{ - Use: "list-<%= TypeName.Kebab %>", - Short: "list all <%= TypeName.Original %>", - RunE: func(cmd *cobra.Command, args []string) error { - clientCtx, err := client.GetClientQueryContext(cmd) - if err != nil { - return err - } - - pageReq, err := client.ReadPageRequest(cmd.Flags()) - if err != nil { - return err - } - - queryClient := types.NewQueryClient(clientCtx) - - params := &types.QueryAll<%= TypeName.UpperCamel %>Request{ - Pagination: pageReq, - } - - res, err := queryClient.<%= TypeName.UpperCamel %>All(cmd.Context(), params) - if err != nil { - return err - } - - return clientCtx.PrintProto(res) - }, - } - - flags.AddPaginationFlagsToCmd(cmd, cmd.Use) - flags.AddQueryFlagsToCmd(cmd) - - return cmd -} - -func CmdShow<%= TypeName.UpperCamel %>() *cobra.Command { - cmd := &cobra.Command{ - Use: "show-<%= TypeName.Kebab %> [id]", - Short: "shows a <%= TypeName.Original %>", - Args: cobra.ExactArgs(1), - RunE: func(cmd *cobra.Command, args []string) error { - clientCtx, err := client.GetClientQueryContext(cmd) - if err != nil { - return err - } - - queryClient := types.NewQueryClient(clientCtx) - - id, err := strconv.ParseUint(args[0], 10, 64) - if err != nil { - return err - } - - params := &types.QueryGet<%= TypeName.UpperCamel %>Request{ - Id: id, - } - - res, err := queryClient.<%= TypeName.UpperCamel %>(cmd.Context(), params) - if err != nil { - return err - } - - return clientCtx.PrintProto(res) - }, - } - - flags.AddQueryFlagsToCmd(cmd) - - return cmd -} diff --git a/ignite/templates/typed/list/files/component/x/{{moduleName}}/client/cli/query_{{typeName}}_test.go.plush b/ignite/templates/typed/list/files/component/x/{{moduleName}}/client/cli/query_{{typeName}}_test.go.plush deleted file mode 100644 index 20ae7cd582..0000000000 --- a/ignite/templates/typed/list/files/component/x/{{moduleName}}/client/cli/query_{{typeName}}_test.go.plush +++ /dev/null @@ -1,148 +0,0 @@ -package cli_test - -import ( - "fmt" - "testing" - - "github.com/cosmos/cosmos-sdk/client/flags" - clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" - "github.com/stretchr/testify/require" - tmcli "github.com/cometbft/cometbft/libs/cli" - "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" - - "<%= ModulePath %>/testutil/network" - "<%= ModulePath %>/testutil/nullify" - "<%= ModulePath %>/x/<%= ModuleName %>/client/cli" - "<%= ModulePath %>/x/<%= ModuleName %>/types" -) - -func (s *IntegrationTestSuite) networkWith<%= TypeName.UpperCamel %>Objects(n int) (*network.Network, []types.<%= TypeName.UpperCamel %>) { - s.T().Helper() - state := types.GenesisState{<%= if (IsIBC) { %>PortId: types.PortID<% } %>} - for i := 0; i < n; i++ { - <%= TypeName.LowerCamel %> := types.<%= TypeName.UpperCamel %>{ - Id: uint64(i), - } - nullify.Fill(&<%= TypeName.LowerCamel %>) - state.<%= TypeName.UpperCamel %>List = append(state.<%= TypeName.UpperCamel %>List, <%= TypeName.LowerCamel %>) - } - return s.network(&state), state.<%= TypeName.UpperCamel %>List -} - -func (s *IntegrationTestSuite) TestShow<%= TypeName.UpperCamel %>() { - var ( - net, objs = s.networkWith<%= TypeName.UpperCamel %>Objects(2) - ctx = net.Validators[0].ClientCtx - common = []string{ - fmt.Sprintf("--%s=json", tmcli.OutputFlag), - } - ) - tests := []struct { - desc string - id string - args []string - err error - obj types.<%= TypeName.UpperCamel %> - }{ - { - desc: "found", - id: fmt.Sprintf("%d", objs[0].Id), - args: common, - obj: objs[0], - }, - { - desc: "not found", - id: "not_found", - args: common, - err: status.Error(codes.NotFound, "not found"), - }, - } - for _, tc := range tests { - s.T().Run(tc.desc, func(t *testing.T) { - args := append([]string{tc.id}, tc.args...) - out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdShow<%= TypeName.UpperCamel %>(), args) - if tc.err != nil { - stat, ok := status.FromError(tc.err) - require.True(t, ok) - require.ErrorIs(t, stat.Err(), tc.err) - return - } - require.NoError(t, err) - var resp types.QueryGet<%= TypeName.UpperCamel %>Response - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) - require.NotNil(t, resp.<%= TypeName.UpperCamel %>) - require.Equal(t, - nullify.Fill(&tc.obj), - nullify.Fill(&resp.<%= TypeName.UpperCamel %>), - ) - }) - } -} - -func (s *IntegrationTestSuite) TestList<%= TypeName.UpperCamel %>() { - var ( - net, objs = s.networkWith<%= TypeName.UpperCamel %>Objects(5) - ctx = net.Validators[0].ClientCtx - ) - request := func(next []byte, offset, limit uint64, total bool) []string { - args := []string{ - fmt.Sprintf("--%s=json", tmcli.OutputFlag), - } - if next == nil { - args = append(args, fmt.Sprintf("--%s=%d", flags.FlagOffset, offset)) - } else { - args = append(args, fmt.Sprintf("--%s=%s", flags.FlagPageKey, next)) - } - args = append(args, fmt.Sprintf("--%s=%d", flags.FlagLimit, limit)) - if total { - args = append(args, fmt.Sprintf("--%s", flags.FlagCountTotal)) - } - return args - } - s.T().Run("ByOffset", func(t *testing.T) { - step := 2 - for i := 0; i < len(objs); i += step { - args := request(nil, uint64(i), uint64(step), false) - out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdList<%= TypeName.UpperCamel %>(), args) - require.NoError(t, err) - var resp types.QueryAll<%= TypeName.UpperCamel %>Response - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) - require.LessOrEqual(t, len(resp.<%= TypeName.UpperCamel %>), step) - require.Subset(t, - nullify.Fill(objs), - nullify.Fill(resp.<%= TypeName.UpperCamel %>), - ) - } - }) - s.T().Run("ByKey", func(t *testing.T) { - step := 2 - var next []byte - for i := 0; i < len(objs); i += step { - args := request(next, 0, uint64(step), false) - out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdList<%= TypeName.UpperCamel %>(), args) - require.NoError(t, err) - var resp types.QueryAll<%= TypeName.UpperCamel %>Response - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) - require.LessOrEqual(t, len(resp.<%= TypeName.UpperCamel %>), step) - require.Subset(t, - nullify.Fill(objs), - nullify.Fill(resp.<%= TypeName.UpperCamel %>), - ) - next = resp.Pagination.NextKey - } - }) - s.T().Run("Total", func(t *testing.T) { - args := request(nil, 0, uint64(len(objs)), true) - out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdList<%= TypeName.UpperCamel %>(), args) - require.NoError(t, err) - var resp types.QueryAll<%= TypeName.UpperCamel %>Response - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) - require.NoError(t, err) - require.Equal(t, len(objs), int(resp.Pagination.Total)) - require.ElementsMatch(t, - nullify.Fill(objs), - nullify.Fill(resp.<%= TypeName.UpperCamel %>), - ) - }) -} diff --git a/ignite/templates/typed/list/files/messages/x/{{moduleName}}/client/cli/tx_{{typeName}}.go.plush b/ignite/templates/typed/list/files/messages/x/{{moduleName}}/client/cli/tx_{{typeName}}.go.plush deleted file mode 100644 index c2dde5b285..0000000000 --- a/ignite/templates/typed/list/files/messages/x/{{moduleName}}/client/cli/tx_{{typeName}}.go.plush +++ /dev/null @@ -1,99 +0,0 @@ -package cli - -import ( - "strconv" - <%= for (goImport) in mergeGoImports(Fields) { %> - <%= goImport.Alias %> "<%= goImport.Name %>"<% } %> - "github.com/spf13/cobra" - "github.com/cosmos/cosmos-sdk/client" - "github.com/cosmos/cosmos-sdk/client/flags" - "github.com/cosmos/cosmos-sdk/client/tx" - "<%= ModulePath %>/x/<%= ModuleName %>/types" -) - -func CmdCreate<%= TypeName.UpperCamel %>() *cobra.Command { - cmd := &cobra.Command{ - Use: "create-<%= TypeName.Kebab %><%= Fields.String() %>", - Short: "Create a new <%= TypeName.Original %>", - Args: cobra.ExactArgs(<%= len(Fields) %>), - RunE: func(cmd *cobra.Command, args []string) (err error) { - <%= for (i, field) in Fields { %> <%= field.CLIArgs("arg", i) %> - <% } %> - clientCtx, err := client.GetClientTxContext(cmd) - if err != nil { - return err - } - - msg := types.NewMsgCreate<%= TypeName.UpperCamel %>(clientCtx.GetFromAddress().String()<%= for (i, field) in Fields { %>, arg<%= field.Name.UpperCamel %><% } %>) - if err := msg.ValidateBasic(); err != nil { - return err - } - return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg) - }, - } - - flags.AddTxFlagsToCmd(cmd) - - return cmd -} - -func CmdUpdate<%= TypeName.UpperCamel %>() *cobra.Command { - cmd := &cobra.Command{ - Use: "update-<%= TypeName.Kebab %> [id]<%= Fields.String() %>", - Short: "Update a <%= TypeName.Original %>", - Args: cobra.ExactArgs(<%= len(Fields) + 1 %>), - RunE: func(cmd *cobra.Command, args []string) (err error) { - id, err := strconv.ParseUint(args[0], 10, 64) - if err != nil { - return err - } - - <%= for (i, field) in Fields { %> - <%= field.CLIArgs("arg", i+1) %> - <% } %> - clientCtx, err := client.GetClientTxContext(cmd) - if err != nil { - return err - } - - msg := types.NewMsgUpdate<%= TypeName.UpperCamel %>(clientCtx.GetFromAddress().String(), id<%= for (i, field) in Fields { %>, arg<%= field.Name.UpperCamel %><% } %>) - if err := msg.ValidateBasic(); err != nil { - return err - } - return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg) - }, - } - - flags.AddTxFlagsToCmd(cmd) - - return cmd -} - -func CmdDelete<%= TypeName.UpperCamel %>() *cobra.Command { - cmd := &cobra.Command{ - Use: "delete-<%= TypeName.Kebab %> [id]", - Short: "Delete a <%= TypeName.Original %> by id", - Args: cobra.ExactArgs(1), - RunE: func(cmd *cobra.Command, args []string) error { - id, err := strconv.ParseUint(args[0], 10, 64) - if err != nil { - return err - } - - clientCtx, err := client.GetClientTxContext(cmd) - if err != nil { - return err - } - - msg := types.NewMsgDelete<%= TypeName.UpperCamel %>(clientCtx.GetFromAddress().String(), id) - if err := msg.ValidateBasic(); err != nil { - return err - } - return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg) - }, - } - - flags.AddTxFlagsToCmd(cmd) - - return cmd -} diff --git a/ignite/templates/typed/list/files/messages/x/{{moduleName}}/client/cli/tx_{{typeName}}_test.go.plush b/ignite/templates/typed/list/files/messages/x/{{moduleName}}/client/cli/tx_{{typeName}}_test.go.plush deleted file mode 100644 index 7da6b72cd9..0000000000 --- a/ignite/templates/typed/list/files/messages/x/{{moduleName}}/client/cli/tx_{{typeName}}_test.go.plush +++ /dev/null @@ -1,180 +0,0 @@ -package cli_test - -import ( - "fmt" - "strconv" - "testing" - - sdkmath "cosmossdk.io/math" - "github.com/cosmos/cosmos-sdk/client/flags" - clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" - sdk "github.com/cosmos/cosmos-sdk/types" - sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" - "github.com/stretchr/testify/require" - - "<%= ModulePath %>/x/<%= ModuleName %>/client/cli" -) - -func (s *IntegrationTestSuite) TestCreate<%= TypeName.UpperCamel %>() { - var ( - net = s.network(nil) - val = net.Validators[0] - ctx = val.ClientCtx - fields = []string{<%= for (field) in Fields { %> "<%= field.DefaultTestValue() %>", <% } %>} - ) - - tests := []struct { - desc string - args []string - err error - code uint32 - }{ - { - desc: "valid", - args: []string{ - fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()), - fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), - fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), - fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(net.Config.BondDenom, sdkmath.NewInt(10))).String()), - }, - }, - } - for _, tc := range tests { - s.T().Run(tc.desc, func(t *testing.T) { - args := append([]string{}, fields...) - args = append(args, tc.args...) - out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdCreate<%= TypeName.UpperCamel %>(), args) - if tc.err != nil { - require.ErrorIs(t, err, tc.err) - return - } - require.NoError(t, err) - - var resp sdk.TxResponse - require.NoError(t, ctx.Codec.UnmarshalJSON(out.Bytes(), &resp)) - require.NoError(t, clitestutil.CheckTxCode(net, ctx, resp.TxHash, tc.code)) - }) - } -} - -func (s *IntegrationTestSuite) TestUpdate<%= TypeName.UpperCamel %>() { - var ( - net = s.network(nil) - val = net.Validators[0] - ctx = val.ClientCtx - fields = []string{<%= for (field) in Fields { %> "<%= field.DefaultTestValue() %>", <% } %>} - common = []string{ - fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()), - fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), - fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), - fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(net.Config.BondDenom, sdkmath.NewInt(10))).String()), - } - args = append([]string{}, fields...) - ) - args = append(args, common...) - - _, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdCreate<%= TypeName.UpperCamel %>(), args) - s.Require().NoError(err) - s.Require().NoError(net.WaitForNextBlock()) - - tests := []struct { - desc string - id string - args []string - code uint32 - err error - }{ - { - desc: "valid", - id: "0", - args: common, - }, - { - desc: "key not found", - id: "1", - args: common, - code: sdkerrors.ErrKeyNotFound.ABCICode(), - }, - { - desc: "invalid key", - id: "invalid", - err: strconv.ErrSyntax, - }, - } - for _, tc := range tests { - s.T().Run(tc.desc, func(t *testing.T) { - args := append([]string{tc.id}, fields...) - args = append(args, tc.args...) - out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdUpdate<%= TypeName.UpperCamel %>(), args) - if tc.err != nil { - require.ErrorIs(t, err, tc.err) - return - } - require.NoError(t, err) - - var resp sdk.TxResponse - require.NoError(t, ctx.Codec.UnmarshalJSON(out.Bytes(), &resp)) - require.NoError(t, clitestutil.CheckTxCode(net, ctx, resp.TxHash, tc.code)) - }) - } -} - -func (s *IntegrationTestSuite) TestDelete<%= TypeName.UpperCamel %>() { - var ( - net = s.network(nil) - val = net.Validators[0] - ctx = val.ClientCtx - fields = []string{<%= for (field) in Fields { %> "<%= field.DefaultTestValue() %>", <% } %>} - common = []string{ - fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()), - fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), - fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), - fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(net.Config.BondDenom, sdkmath.NewInt(10))).String()), - } - args = append([]string{}, fields...) - ) - args = append(args, common...) - - _, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdCreate<%= TypeName.UpperCamel %>(), args) - s.Require().NoError(err) - s.Require().NoError(net.WaitForNextBlock()) - - tests := []struct { - desc string - id string - args []string - code uint32 - err error - }{ - { - desc: "valid", - id: "0", - args: common, - }, - { - desc: "key not found", - id: "1", - args: common, - code: sdkerrors.ErrKeyNotFound.ABCICode(), - }, - { - desc: "invalid key", - id: "invalid", - err: strconv.ErrSyntax, - }, - } - for _, tc := range tests { - s.T().Run(tc.desc, func(t *testing.T) { - out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdDelete<%= TypeName.UpperCamel %>(), append([]string{tc.id}, tc.args...)) - if tc.err != nil { - require.ErrorIs(t, err, tc.err) - return - } - require.NoError(t, err) - - var resp sdk.TxResponse - require.NoError(t, ctx.Codec.UnmarshalJSON(out.Bytes(), &resp)) - require.NoError(t, clitestutil.CheckTxCode(net, ctx, resp.TxHash, tc.code)) - }) - } -} diff --git a/ignite/templates/typed/list/list.go b/ignite/templates/typed/list/list.go index 1bfbd678cb..e3603b5ff8 100644 --- a/ignite/templates/typed/list/list.go +++ b/ignite/templates/typed/list/list.go @@ -310,17 +310,46 @@ func typesCodecModify(replacer placeholder.Replacer, opts *typed.Options) genny. func clientCliTxModify(replacer placeholder.Replacer, opts *typed.Options) genny.RunFn { return func(r *genny.Runner) error { - path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "client/cli/tx.go") + path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "module/autocli.go") f, err := r.Disk.Find(path) if err != nil { return err } - template := `cmd.AddCommand(CmdCreate%[2]v()) - cmd.AddCommand(CmdUpdate%[2]v()) - cmd.AddCommand(CmdDelete%[2]v()) -%[1]v` - replacement := fmt.Sprintf(template, typed.Placeholder, opts.TypeName.UpperCamel) - content := replacer.Replace(f.String(), typed.Placeholder, replacement) + + var positionalArgs string + for _, field := range opts.Fields { + positionalArgs += fmt.Sprintf(`{ProtoField: "%s"}, `, field.ProtoFieldName()) + } + + template := `{ + RpcMethod: "Create%[2]v", + Use: "create-%[3]v", + Short: "Create %[4]v", + PositionalArgs: []*autocliv1.PositionalArgDescriptor{%[5]s}, + }, + { + RpcMethod: "Update%[2]v", + Use: "update-%[3]v", + Short: "Update %[4]v", + PositionalArgs: []*autocliv1.PositionalArgDescriptor{%[5]s}, + }, + { + RpcMethod: "Delete%[2]v", + Use: "delete-%[3]v", + Short: "Delete %[4]v", + }, + %[1]v` + + replacement := fmt.Sprintf( + template, + typed.PlaceholderAutoCLITx, + opts.TypeName.UpperCamel, + opts.TypeName.Kebab, + opts.TypeName.Original, + strings.TrimSpace(positionalArgs), + ) + + content := replacer.Replace(f.String(), typed.PlaceholderAutoCLITx, replacement) newFile := genny.NewFileS(path, content) return r.File(newFile) } @@ -328,18 +357,32 @@ func clientCliTxModify(replacer placeholder.Replacer, opts *typed.Options) genny func clientCliQueryModify(replacer placeholder.Replacer, opts *typed.Options) genny.RunFn { return func(r *genny.Runner) error { - path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "client/cli/query.go") + path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "module/autocli.go") f, err := r.Disk.Find(path) if err != nil { return err } - template := `cmd.AddCommand(CmdList%[2]v()) - cmd.AddCommand(CmdShow%[2]v()) -%[1]v` - replacement := fmt.Sprintf(template, typed.Placeholder, + + template := `{ + RpcMethod: "%[2]vAll", + Use: "list-%[3]v", + Short: "List all %[4]v", + }, + { + RpcMethod: "%[2]v", + Use: "show-%[3]v [id]", + Short: "Shows a %[4]v by id", + PositionalArgs: []*autocliv1.PositionalArgDescriptor{{ProtoField: "id"}}, + }, + %[1]v` + replacement := fmt.Sprintf( + template, + typed.PlaceholderAutoCLIQuery, opts.TypeName.UpperCamel, + opts.TypeName.Kebab, + opts.TypeName.Original, ) - content := replacer.Replace(f.String(), typed.Placeholder, replacement) + content := replacer.Replace(f.String(), typed.PlaceholderAutoCLIQuery, replacement) newFile := genny.NewFileS(path, content) return r.File(newFile) } diff --git a/ignite/templates/typed/map/files/component/x/{{moduleName}}/client/cli/query_{{typeName}}.go.plush b/ignite/templates/typed/map/files/component/x/{{moduleName}}/client/cli/query_{{typeName}}.go.plush deleted file mode 100644 index 2ae7648f6c..0000000000 --- a/ignite/templates/typed/map/files/component/x/{{moduleName}}/client/cli/query_{{typeName}}.go.plush +++ /dev/null @@ -1,81 +0,0 @@ -package cli - -import ( - "github.com/spf13/cobra" - "github.com/cosmos/cosmos-sdk/client" - "github.com/cosmos/cosmos-sdk/client/flags" - - <%= for (goImport) in mergeGoImports(Indexes) { %> - <%= goImport.Alias %> "<%= goImport.Name %>"<% } %> - "<%= ModulePath %>/x/<%= ModuleName %>/types" -) - -func CmdList<%= TypeName.UpperCamel %>() *cobra.Command { - cmd := &cobra.Command{ - Use: "list-<%= TypeName.Kebab %>", - Short: "list all <%= TypeName.Original %>", - RunE: func(cmd *cobra.Command, args []string) error { - clientCtx, err := client.GetClientQueryContext(cmd) - if err != nil { - return err - } - - pageReq, err := client.ReadPageRequest(cmd.Flags()) - if err != nil { - return err - } - - queryClient := types.NewQueryClient(clientCtx) - - params := &types.QueryAll<%= TypeName.UpperCamel %>Request{ - Pagination: pageReq, - } - - res, err := queryClient.<%= TypeName.UpperCamel %>All(cmd.Context(), params) - if err != nil { - return err - } - - return clientCtx.PrintProto(res) - }, - } - - flags.AddPaginationFlagsToCmd(cmd, cmd.Use) - flags.AddQueryFlagsToCmd(cmd) - - return cmd -} - -func CmdShow<%= TypeName.UpperCamel %>() *cobra.Command { - cmd := &cobra.Command{ - Use: "show-<%= TypeName.Kebab %><%= Indexes.String() %>", - Short: "shows a <%= TypeName.Original %>", - Args: cobra.ExactArgs(<%= len(Indexes) %>), - RunE: func(cmd *cobra.Command, args []string) (err error) { - clientCtx, err := client.GetClientQueryContext(cmd) - if err != nil { - return err - } - - queryClient := types.NewQueryClient(clientCtx) - - <%= for (i, field) in Indexes { %> <%= field.CLIArgs("arg", i) %> - <% } %> - params := &types.QueryGet<%= TypeName.UpperCamel %>Request{ - <%= for (i, index) in Indexes { %><%= index.Name.UpperCamel %>: arg<%= index.Name.UpperCamel %>, - <% } %> - } - - res, err := queryClient.<%= TypeName.UpperCamel %>(cmd.Context(), params) - if err != nil { - return err - } - - return clientCtx.PrintProto(res) - }, - } - - flags.AddQueryFlagsToCmd(cmd) - - return cmd -} diff --git a/ignite/templates/typed/map/files/messages/x/{{moduleName}}/client/cli/tx_{{typeName}}.go.plush b/ignite/templates/typed/map/files/messages/x/{{moduleName}}/client/cli/tx_{{typeName}}.go.plush deleted file mode 100644 index 94feb6425f..0000000000 --- a/ignite/templates/typed/map/files/messages/x/{{moduleName}}/client/cli/tx_{{typeName}}.go.plush +++ /dev/null @@ -1,108 +0,0 @@ -package cli - -import ( - <%= for (goImport) in mergeGoImports(Indexes, Fields) { %> - <%= goImport.Alias %> "<%= goImport.Name %>"<% } %> - "github.com/spf13/cobra" - "github.com/cosmos/cosmos-sdk/client" - "github.com/cosmos/cosmos-sdk/client/flags" - "github.com/cosmos/cosmos-sdk/client/tx" - "<%= ModulePath %>/x/<%= ModuleName %>/types" -) - -func CmdCreate<%= TypeName.UpperCamel %>() *cobra.Command { - cmd := &cobra.Command{ - Use: "create-<%= TypeName.Kebab %><%= Indexes.String() %><%= Fields.String() %>", - Short: "Create a new <%= TypeName.Original %>", - Args: cobra.ExactArgs(<%= len(Fields) + len(Indexes) %>), - RunE: func(cmd *cobra.Command, args []string) (err error) { - // Get indexes - <%= for (i, field) in Indexes { %> <%= field.CLIArgs("index", i) %> - <% } %> - // Get value arguments - <%= for (i, field) in Fields { %> <%= field.CLIArgs("arg", i+len(Indexes)) %> - <% } %> - clientCtx, err := client.GetClientTxContext(cmd) - if err != nil { - return err - } - - msg := types.NewMsgCreate<%= TypeName.UpperCamel %>( - clientCtx.GetFromAddress().String(), - <%= for (i, index) in Indexes { %>index<%= index.Name.UpperCamel %>, - <% } %><%= for (i, field) in Fields { %>arg<%= field.Name.UpperCamel %>, - <% } %>) - if err := msg.ValidateBasic(); err != nil { - return err - } - return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg) - }, - } - - flags.AddTxFlagsToCmd(cmd) - - return cmd -} - -func CmdUpdate<%= TypeName.UpperCamel %>() *cobra.Command { - cmd := &cobra.Command{ - Use: "update-<%= TypeName.Kebab %><%= Indexes.String() %><%= Fields.String() %>", - Short: "Update a <%= TypeName.Original %>", - Args: cobra.ExactArgs(<%= len(Fields) + len(Indexes) %>), - RunE: func(cmd *cobra.Command, args []string) (err error) { - // Get indexes - <%= for (i, field) in Indexes { %> <%= field.CLIArgs("index", i) %> - <% } %> - // Get value arguments - <%= for (i, field) in Fields { %> <%= field.CLIArgs("arg", i+len(Indexes)) %> - <% } %> - clientCtx, err := client.GetClientTxContext(cmd) - if err != nil { - return err - } - - msg := types.NewMsgUpdate<%= TypeName.UpperCamel %>( - clientCtx.GetFromAddress().String(), - <%= for (i, index) in Indexes { %>index<%= index.Name.UpperCamel %>, - <% } %><%= for (i, field) in Fields { %>arg<%= field.Name.UpperCamel %>, - <% } %>) - if err := msg.ValidateBasic(); err != nil { - return err - } - return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg) - }, - } - - flags.AddTxFlagsToCmd(cmd) - - return cmd -} - -func CmdDelete<%= TypeName.UpperCamel %>() *cobra.Command { - cmd := &cobra.Command{ - Use: "delete-<%= TypeName.Kebab %><%= Indexes.String() %>", - Short: "Delete a <%= TypeName.Original %>", - Args: cobra.ExactArgs(<%= len(Indexes) %>), - RunE: func(cmd *cobra.Command, args []string) (err error) { - <%= for (i, field) in Indexes { %> <%= field.CLIArgs("index", i) %> - <% } %> - clientCtx, err := client.GetClientTxContext(cmd) - if err != nil { - return err - } - - msg := types.NewMsgDelete<%= TypeName.UpperCamel %>( - clientCtx.GetFromAddress().String(), - <%= for (i, index) in Indexes { %>index<%= index.Name.UpperCamel %>, - <% } %>) - if err := msg.ValidateBasic(); err != nil { - return err - } - return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg) - }, - } - - flags.AddTxFlagsToCmd(cmd) - - return cmd -} \ No newline at end of file diff --git a/ignite/templates/typed/map/files/tests/component/x/{{moduleName}}/client/cli/query_{{typeName}}_test.go.plush b/ignite/templates/typed/map/files/tests/component/x/{{moduleName}}/client/cli/query_{{typeName}}_test.go.plush deleted file mode 100644 index 012b2a3fb6..0000000000 --- a/ignite/templates/typed/map/files/tests/component/x/{{moduleName}}/client/cli/query_{{typeName}}_test.go.plush +++ /dev/null @@ -1,160 +0,0 @@ -package cli_test - -import ( - "fmt" - "strconv" - "testing" - - "github.com/cosmos/cosmos-sdk/client/flags" - clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" - "github.com/stretchr/testify/require" - tmcli "github.com/cometbft/cometbft/libs/cli" - "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" - - "<%= ModulePath %>/testutil/network" - "<%= ModulePath %>/testutil/nullify" - "<%= ModulePath %>/x/<%= ModuleName %>/client/cli" - "<%= ModulePath %>/x/<%= ModuleName %>/types" -) - -// Prevent strconv unused error -var _ = strconv.IntSize - -func (s *IntegrationTestSuite) networkWith<%= TypeName.UpperCamel %>Objects(n int) (*network.Network, []types.<%= TypeName.UpperCamel %>) { - s.T().Helper() - state := types.GenesisState{<%= if (IsIBC) { %>PortId: types.PortID<% } %>} - for i := 0; i < n; i++ { - <%= TypeName.LowerCamel %> := types.<%= TypeName.UpperCamel %>{ - <%= for (index) in Indexes { %><%= index.Name.UpperCamel %>: <%= index.ValueLoop() %>, - <% } %> - } - nullify.Fill(&<%= TypeName.LowerCamel %>) - state.<%= TypeName.UpperCamel %>List = append(state.<%= TypeName.UpperCamel %>List, <%= TypeName.LowerCamel %>) - } - return s.network(&state), state.<%= TypeName.UpperCamel %>List -} - -func (s *IntegrationTestSuite) TestShow<%= TypeName.UpperCamel %>() { - var ( - net, objs = s.networkWith<%= TypeName.UpperCamel %>Objects(2) - ctx = net.Validators[0].ClientCtx - common = []string{ - fmt.Sprintf("--%s=json", tmcli.OutputFlag), - } - ) - tests := []struct { - desc string - <%= for (i, index) in Indexes { %>id<%= index.Name.UpperCamel %> <%= index.DataType() %> - <% } %> - args []string - err error - obj types.<%= TypeName.UpperCamel %> - }{ - { - desc: "found", - <%= for (i, index) in Indexes { %>id<%= index.Name.UpperCamel %>: objs[0].<%= index.Name.UpperCamel %>, - <% } %> - args: common, - obj: objs[0], - }, - { - desc: "not found", - <%= for (i, index) in Indexes { %>id<%= index.Name.UpperCamel %>: <%= index.ValueInvalidIndex() %>, - <% } %> - args: common, - err: status.Error(codes.NotFound, "not found"), - }, - } - for _, tc := range tests { - s.T().Run(tc.desc, func(t *testing.T) { - args := []string{ - <%= for (i, index) in Indexes { %><%= index.ToString("tc.id" + index.Name.UpperCamel) %>, - <% } %> - } - args = append(args, tc.args...) - out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdShow<%= TypeName.UpperCamel %>(), args) - if tc.err != nil { - stat, ok := status.FromError(tc.err) - require.True(t, ok) - require.ErrorIs(t, stat.Err(), tc.err) - return - } - require.NoError(t, err) - var resp types.QueryGet<%= TypeName.UpperCamel %>Response - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) - require.NotNil(t, resp.<%= TypeName.UpperCamel %>) - require.Equal(t, - nullify.Fill(&tc.obj), - nullify.Fill(&resp.<%= TypeName.UpperCamel %>), - ) - }) - } -} - -func (s *IntegrationTestSuite) TestList<%= TypeName.UpperCamel %>() { - var ( - net, objs = s.networkWith<%= TypeName.UpperCamel %>Objects(5) - ctx = net.Validators[0].ClientCtx - ) - request := func(next []byte, offset, limit uint64, total bool) []string { - args := []string{ - fmt.Sprintf("--%s=json", tmcli.OutputFlag), - } - if next == nil { - args = append(args, fmt.Sprintf("--%s=%d", flags.FlagOffset, offset)) - } else { - args = append(args, fmt.Sprintf("--%s=%s", flags.FlagPageKey, next)) - } - args = append(args, fmt.Sprintf("--%s=%d", flags.FlagLimit, limit)) - if total { - args = append(args, fmt.Sprintf("--%s", flags.FlagCountTotal)) - } - return args - } - s.T().Run("ByOffset", func(t *testing.T) { - step := 2 - for i := 0; i < len(objs); i += step { - args := request(nil, uint64(i), uint64(step), false) - out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdList<%= TypeName.UpperCamel %>(), args) - require.NoError(t, err) - var resp types.QueryAll<%= TypeName.UpperCamel %>Response - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) - require.LessOrEqual(t, len(resp.<%= TypeName.UpperCamel %>), step) - require.Subset(t, - nullify.Fill(objs), - nullify.Fill(resp.<%= TypeName.UpperCamel %>), - ) - } - }) - s.T().Run("ByKey", func(t *testing.T) { - step := 2 - var next []byte - for i := 0; i < len(objs); i += step { - args := request(next, 0, uint64(step), false) - out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdList<%= TypeName.UpperCamel %>(), args) - require.NoError(t, err) - var resp types.QueryAll<%= TypeName.UpperCamel %>Response - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) - require.LessOrEqual(t, len(resp.<%= TypeName.UpperCamel %>), step) - require.Subset(t, - nullify.Fill(objs), - nullify.Fill(resp.<%= TypeName.UpperCamel %>), - ) - next = resp.Pagination.NextKey - } - }) - s.T().Run("Total", func(t *testing.T) { - args := request(nil, 0, uint64(len(objs)), true) - out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdList<%= TypeName.UpperCamel %>(), args) - require.NoError(t, err) - var resp types.QueryAll<%= TypeName.UpperCamel %>Response - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) - require.NoError(t, err) - require.Equal(t, len(objs), int(resp.Pagination.Total)) - require.ElementsMatch(t, - nullify.Fill(objs), - nullify.Fill(resp.<%= TypeName.UpperCamel %>), - ) - }) -} diff --git a/ignite/templates/typed/map/files/tests/messages/x/{{moduleName}}/client/cli/tx_{{typeName}}_test.go.plush b/ignite/templates/typed/map/files/tests/messages/x/{{moduleName}}/client/cli/tx_{{typeName}}_test.go.plush deleted file mode 100644 index 07bd7a61bd..0000000000 --- a/ignite/templates/typed/map/files/tests/messages/x/{{moduleName}}/client/cli/tx_{{typeName}}_test.go.plush +++ /dev/null @@ -1,203 +0,0 @@ -package cli_test - -import ( - "fmt" - "strconv" - "testing" - - sdkmath "cosmossdk.io/math" - "github.com/cosmos/cosmos-sdk/client/flags" - clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" - sdk "github.com/cosmos/cosmos-sdk/types" - sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" - "github.com/stretchr/testify/require" - - "<%= ModulePath %>/x/<%= ModuleName %>/client/cli" -) - -// Prevent strconv unused error -var _ = strconv.IntSize - -func (s *IntegrationTestSuite) TestCreate<%= TypeName.UpperCamel %>() { - var ( - net = s.network(nil) - val = net.Validators[0] - ctx = val.ClientCtx - fields = []string{<%= for (field) in Fields { %> "<%= field.DefaultTestValue() %>", <% } %>} - ) - tests := []struct { - desc string - <%= for (i, index) in Indexes { %>id<%= index.Name.UpperCamel %> <%= index.DataType() %> - <% } %> - args []string - err error - code uint32 - }{ - { - <%= for (i, index) in Indexes { %>id<%= index.Name.UpperCamel %>: <%= index.ValueIndex() %>, - <% } %> - desc: "valid", - args: []string{ - fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()), - fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), - fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), - fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(net.Config.BondDenom, sdkmath.NewInt(10))).String()), - }, - }, - } - for _, tc := range tests { - s.T().Run(tc.desc, func(t *testing.T) { - args := []string{ - <%= for (i, index) in Indexes { %><%= index.ToString("tc.id" + index.Name.UpperCamel) %>, - <% } %> - } - args = append(args, fields...) - args = append(args, tc.args...) - out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdCreate<%= TypeName.UpperCamel %>(), args) - if tc.err != nil { - require.ErrorIs(t, err, tc.err) - return - } - require.NoError(t, err) - - var resp sdk.TxResponse - require.NoError(t, ctx.Codec.UnmarshalJSON(out.Bytes(), &resp)) - require.NoError(t, clitestutil.CheckTxCode(net, ctx, resp.TxHash, tc.code)) - }) - } -} - -func (s *IntegrationTestSuite) TestUpdate<%= TypeName.UpperCamel %>() { - var ( - net = s.network(nil) - val = net.Validators[0] - ctx = val.ClientCtx - fields = []string{<%= for (field) in Fields { %> "<%= field.DefaultTestValue() %>", <% } %>} - common = []string{ - fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()), - fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), - fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), - fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(net.Config.BondDenom, sdkmath.NewInt(10))).String()), - } - args = []string{ - <%= for (i, index) in Indexes { %>"0", - <% } %> - } - ) - args = append(args, fields...) - args = append(args, common...) - - _, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdCreate<%= TypeName.UpperCamel %>(), args) - s.Require().NoError(err) - s.Require().NoError(net.WaitForNextBlock()) - - tests := []struct { - desc string - <%= for (i, index) in Indexes { %>id<%= index.Name.UpperCamel %> <%= index.DataType() %> - <% } %> - args []string - code uint32 - err error - }{ - { - desc: "valid", - <%= for (i, index) in Indexes { %>id<%= index.Name.UpperCamel %>: <%= index.ValueIndex() %>, - <% } %> - args: common, - }, - { - desc: "key not found", - <%= for (i, index) in Indexes { %>id<%= index.Name.UpperCamel %>: <%= index.ValueInvalidIndex() %>, - <% } %> - args: common, - code: sdkerrors.ErrKeyNotFound.ABCICode(), - }, - } - for _, tc := range tests { - s.T().Run(tc.desc, func(t *testing.T) { - args := []string{ - <%= for (i, index) in Indexes { %><%= index.ToString("tc.id" + index.Name.UpperCamel) %>, - <% } %> - } - args = append(args, fields...) - args = append(args, tc.args...) - out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdUpdate<%= TypeName.UpperCamel %>(), args) - if tc.err != nil { - require.ErrorIs(t, err, tc.err) - return - } - require.NoError(t, err) - - var resp sdk.TxResponse - require.NoError(t, ctx.Codec.UnmarshalJSON(out.Bytes(), &resp)) - require.NoError(t, clitestutil.CheckTxCode(net, ctx, resp.TxHash, tc.code)) - }) - } -} - -func (s *IntegrationTestSuite) TestDelete<%= TypeName.UpperCamel %>() { - var ( - net = s.network(nil) - val = net.Validators[0] - ctx = val.ClientCtx - fields = []string{<%= for (field) in Fields { %> "<%= field.DefaultTestValue() %>", <% } %>} - common = []string{ - fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()), - fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), - fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), - fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(net.Config.BondDenom, sdkmath.NewInt(10))).String()), - } - args = []string{ - <%= for (i, index) in Indexes { %>"0", - <% } %> - } - ) - args = append(args, fields...) - args = append(args, common...) - - _, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdCreate<%= TypeName.UpperCamel %>(), args) - s.Require().NoError(err) - s.Require().NoError(net.WaitForNextBlock()) - - tests := []struct { - desc string - <%= for (i, index) in Indexes { %>id<%= index.Name.UpperCamel %> <%= index.DataType() %> - <% } %> - args []string - code uint32 - err error - }{ - { - desc: "valid", - <%= for (i, index) in Indexes { %>id<%= index.Name.UpperCamel %>: <%= index.ValueIndex() %>, - <% } %> - args: common, - }, - { - desc: "key not found", - <%= for (i, index) in Indexes { %>id<%= index.Name.UpperCamel %>: <%= index.ValueInvalidIndex() %>, - <% } %> - args: common, - code: sdkerrors.ErrKeyNotFound.ABCICode(), - }, - } - for _, tc := range tests { - s.T().Run(tc.desc, func(t *testing.T) { - args := []string{ - <%= for (i, index) in Indexes { %><%= index.ToString("tc.id" + index.Name.UpperCamel) %>, - <% } %> - } - args = append(args, tc.args...) - out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdDelete<%= TypeName.UpperCamel %>(), args) - if tc.err != nil { - require.ErrorIs(t, err, tc.err) - return - } - require.NoError(t, err) - - var resp sdk.TxResponse - require.NoError(t, ctx.Codec.UnmarshalJSON(out.Bytes(), &resp)) - require.NoError(t, clitestutil.CheckTxCode(net, ctx, resp.TxHash, tc.code)) - }) - } -} diff --git a/ignite/templates/typed/map/map.go b/ignite/templates/typed/map/map.go index 9177fb7d43..5e60c4b4bb 100644 --- a/ignite/templates/typed/map/map.go +++ b/ignite/templates/typed/map/map.go @@ -236,18 +236,38 @@ func protoRPCModify(opts *typed.Options) genny.RunFn { func clientCliQueryModify(replacer placeholder.Replacer, opts *typed.Options) genny.RunFn { return func(r *genny.Runner) error { - path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "client/cli/query.go") + path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "module/autocli.go") f, err := r.Disk.Find(path) if err != nil { return err } - template := `cmd.AddCommand(CmdList%[2]v()) - cmd.AddCommand(CmdShow%[2]v()) -%[1]v` - replacement := fmt.Sprintf(template, typed.Placeholder, + + var positionalArgs string + for _, field := range opts.Indexes { + positionalArgs += fmt.Sprintf(`{ProtoField: "%s"}, `, field.ProtoFieldName()) + } + + template := `{ + RpcMethod: "%[2]vAll", + Use: "list-%[3]v", + Short: "List all %[4]v", + }, + { + RpcMethod: "%[2]v", + Use: "show-%[3]v [id]", + Short: "Shows a %[4]v", + PositionalArgs: []*autocliv1.PositionalArgDescriptor{%s}, + }, + %[1]v` + replacement := fmt.Sprintf( + template, + typed.PlaceholderAutoCLIQuery, opts.TypeName.UpperCamel, + opts.TypeName.Kebab, + opts.TypeName.Original, + strings.TrimSpace(positionalArgs), ) - content := replacer.Replace(f.String(), typed.Placeholder, replacement) + content := replacer.Replace(f.String(), typed.PlaceholderAutoCLIQuery, replacement) newFile := genny.NewFileS(path, content) return r.File(newFile) } @@ -583,17 +603,49 @@ func protoTxModify(opts *typed.Options) genny.RunFn { func clientCliTxModify(replacer placeholder.Replacer, opts *typed.Options) genny.RunFn { return func(r *genny.Runner) error { - path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "client/cli/tx.go") + path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "module/autocli.go") f, err := r.Disk.Find(path) if err != nil { return err } - template := `cmd.AddCommand(CmdCreate%[2]v()) - cmd.AddCommand(CmdUpdate%[2]v()) - cmd.AddCommand(CmdDelete%[2]v()) -%[1]v` - replacement := fmt.Sprintf(template, typed.Placeholder, opts.TypeName.UpperCamel) - content := replacer.Replace(f.String(), typed.Placeholder, replacement) + + var positionalArgs string + for _, field := range opts.Fields { + positionalArgs += fmt.Sprintf(`{ProtoField: "%s"}, `, field.ProtoFieldName()) + } + for _, field := range opts.Indexes { + positionalArgs += fmt.Sprintf(`{ProtoField: "%s"}, `, field.ProtoFieldName()) + } + + template := `{ + RpcMethod: "Create%[2]v", + Use: "create-%[3]v", + Short: "Create a new %[4]v", + PositionalArgs: []*autocliv1.PositionalArgDescriptor{%[5]s}, + }, + { + RpcMethod: "Update%[2]v", + Use: "update-%[3]v", + Short: "Update %[4]v", + PositionalArgs: []*autocliv1.PositionalArgDescriptor{%[5]s}, + }, + { + RpcMethod: "Delete%[2]v", + Use: "delete-%[3]v", + Short: "Delete %[4]v", + }, + %[1]v` + + replacement := fmt.Sprintf( + template, + typed.PlaceholderAutoCLITx, + opts.TypeName.UpperCamel, + opts.TypeName.Kebab, + opts.TypeName.Original, + strings.TrimSpace(positionalArgs), + ) + + content := replacer.Replace(f.String(), typed.PlaceholderAutoCLITx, replacement) newFile := genny.NewFileS(path, content) return r.File(newFile) } diff --git a/ignite/templates/typed/placeholders.go b/ignite/templates/typed/placeholders.go index 1ca63e4b1b..1ca61b23fb 100644 --- a/ignite/templates/typed/placeholders.go +++ b/ignite/templates/typed/placeholders.go @@ -18,4 +18,8 @@ const ( PlaceholderSimappGenesisState = "// this line is used by starport scaffolding # simapp/module/genesisState" PlaceholderSimappOperation = "// this line is used by starport scaffolding # simapp/module/operation" PlaceholderSimappOperationMsg = "// this line is used by starport scaffolding # simapp/module/OpMsg" + + // Placeholders AutoCLI + PlaceholderAutoCLIQuery = "// this line is used by ignite scaffolding # autocli/query" + PlaceholderAutoCLITx = "// this line is used by ignite scaffolding # autocli/tx" ) diff --git a/ignite/templates/typed/singleton/files/component/x/{{moduleName}}/client/cli/query_{{typeName}}.go.plush b/ignite/templates/typed/singleton/files/component/x/{{moduleName}}/client/cli/query_{{typeName}}.go.plush deleted file mode 100644 index 2a699fc716..0000000000 --- a/ignite/templates/typed/singleton/files/component/x/{{moduleName}}/client/cli/query_{{typeName}}.go.plush +++ /dev/null @@ -1,38 +0,0 @@ -package cli - -import ( - "github.com/cosmos/cosmos-sdk/client" - "github.com/cosmos/cosmos-sdk/client/flags" - "github.com/spf13/cobra" - - "<%= ModulePath %>/x/<%= ModuleName %>/types" -) - -func CmdShow<%= TypeName.UpperCamel %>() *cobra.Command { - cmd := &cobra.Command{ - Use: "show-<%= TypeName.Kebab %>", - Short: "shows <%= TypeName.Original %>", - Args: cobra.NoArgs, - RunE: func(cmd *cobra.Command, args []string) error { - clientCtx, err := client.GetClientQueryContext(cmd) - if err != nil { - return err - } - - queryClient := types.NewQueryClient(clientCtx) - - params := &types.QueryGet<%= TypeName.UpperCamel %>Request{} - - res, err := queryClient.<%= TypeName.UpperCamel %>(cmd.Context(), params) - if err != nil { - return err - } - - return clientCtx.PrintProto(res) - }, - } - - flags.AddQueryFlagsToCmd(cmd) - - return cmd -} diff --git a/ignite/templates/typed/singleton/files/component/x/{{moduleName}}/client/cli/query_{{typeName}}_test.go.plush b/ignite/templates/typed/singleton/files/component/x/{{moduleName}}/client/cli/query_{{typeName}}_test.go.plush deleted file mode 100644 index 185d949dde..0000000000 --- a/ignite/templates/typed/singleton/files/component/x/{{moduleName}}/client/cli/query_{{typeName}}_test.go.plush +++ /dev/null @@ -1,68 +0,0 @@ -package cli_test - -import ( - "fmt" - "testing" - - clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" - "github.com/stretchr/testify/require" - tmcli "github.com/cometbft/cometbft/libs/cli" - "google.golang.org/grpc/status" - - "<%= ModulePath %>/testutil/network" - "<%= ModulePath %>/testutil/nullify" - "<%= ModulePath %>/x/<%= ModuleName %>/client/cli" - "<%= ModulePath %>/x/<%= ModuleName %>/types" -) - -func (s *IntegrationTestSuite) networkWith<%= TypeName.UpperCamel %>Object() (*network.Network, types.<%= TypeName.UpperCamel %>) { - s.T().Helper() - state := types.GenesisState{<%= if (IsIBC) { %>PortId: types.PortID<% } %>} - <%= TypeName.LowerCamel %> := &types.<%= TypeName.UpperCamel %>{} - nullify.Fill(&<%= TypeName.LowerCamel %>) - state.<%= TypeName.UpperCamel %> = <%= TypeName.LowerCamel %> - return s.network(&state), *state.<%= TypeName.UpperCamel %> -} - -func (s *IntegrationTestSuite) TestShow<%= TypeName.UpperCamel %>() { - var ( - net, obj = s.networkWith<%= TypeName.UpperCamel %>Object() - ctx = net.Validators[0].ClientCtx - common = []string{ - fmt.Sprintf("--%s=json", tmcli.OutputFlag), - } - ) - tests := []struct { - desc string - args []string - err error - obj types.<%= TypeName.UpperCamel %> - }{ - { - desc: "get", - args: common, - obj: obj, - }, - } - for _, tc := range tests { - s.T().Run(tc.desc, func(t *testing.T) { - args := append([]string{}, tc.args...) - out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdShow<%= TypeName.UpperCamel %>(), args) - if tc.err != nil { - stat, ok := status.FromError(tc.err) - require.True(t, ok) - require.ErrorIs(t, stat.Err(), tc.err) - return - } - require.NoError(t, err) - var resp types.QueryGet<%= TypeName.UpperCamel %>Response - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) - require.NotNil(t, resp.<%= TypeName.UpperCamel %>) - require.Equal(t, - nullify.Fill(&tc.obj), - nullify.Fill(&resp.<%= TypeName.UpperCamel %>), - ) - }) - } -} - diff --git a/ignite/templates/typed/singleton/files/messages/x/{{moduleName}}/client/cli/tx_{{typeName}}.go.plush b/ignite/templates/typed/singleton/files/messages/x/{{moduleName}}/client/cli/tx_{{typeName}}.go.plush deleted file mode 100644 index 5c27fa30a4..0000000000 --- a/ignite/templates/typed/singleton/files/messages/x/{{moduleName}}/client/cli/tx_{{typeName}}.go.plush +++ /dev/null @@ -1,87 +0,0 @@ -package cli - -import ( - <%= for (goImport) in mergeGoImports(Fields) { %> - <%= goImport.Alias %> "<%= goImport.Name %>"<% } %> - "github.com/spf13/cobra" - "github.com/cosmos/cosmos-sdk/client" - "github.com/cosmos/cosmos-sdk/client/flags" - "github.com/cosmos/cosmos-sdk/client/tx" - "<%= ModulePath %>/x/<%= ModuleName %>/types" -) - -func CmdCreate<%= TypeName.UpperCamel %>() *cobra.Command { - cmd := &cobra.Command{ - Use: "create-<%= TypeName.Kebab %><%= Fields.String() %>", - Short: "Create <%= TypeName.Original %>", - Args: cobra.ExactArgs(<%= len(Fields) %>), - RunE: func(cmd *cobra.Command, args []string) (err error) { - <%= for (i, field) in Fields { %> <%= field.CLIArgs("arg", i) %> - <% } %> - clientCtx, err := client.GetClientTxContext(cmd) - if err != nil { - return err - } - - msg := types.NewMsgCreate<%= TypeName.UpperCamel %>(clientCtx.GetFromAddress().String()<%= for (i, field) in Fields { %>, arg<%= field.Name.UpperCamel %><% } %>) - if err := msg.ValidateBasic(); err != nil { - return err - } - return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg) - }, - } - - flags.AddTxFlagsToCmd(cmd) - - return cmd -} - -func CmdUpdate<%= TypeName.UpperCamel %>() *cobra.Command { - cmd := &cobra.Command{ - Use: "update-<%= TypeName.Kebab %><%= Fields.String() %>", - Short: "Update <%= TypeName.Original %>", - Args: cobra.ExactArgs(<%= len(Fields) %>), - RunE: func(cmd *cobra.Command, args []string) (err error) { - <%= for (i, field) in Fields { %> <%= field.CLIArgs("arg", i) %> - <% } %> - clientCtx, err := client.GetClientTxContext(cmd) - if err != nil { - return err - } - - msg := types.NewMsgUpdate<%= TypeName.UpperCamel %>(clientCtx.GetFromAddress().String()<%= for (i, field) in Fields { %>, arg<%= field.Name.UpperCamel %><% } %>) - if err := msg.ValidateBasic(); err != nil { - return err - } - return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg) - }, - } - - flags.AddTxFlagsToCmd(cmd) - - return cmd -} - -func CmdDelete<%= TypeName.UpperCamel %>() *cobra.Command { - cmd := &cobra.Command{ - Use: "delete-<%= TypeName.Kebab %>", - Short: "Delete <%= TypeName.Original %>", - Args: cobra.NoArgs, - RunE: func(cmd *cobra.Command, args []string) error { - clientCtx, err := client.GetClientTxContext(cmd) - if err != nil { - return err - } - - msg := types.NewMsgDelete<%= TypeName.UpperCamel %>(clientCtx.GetFromAddress().String()) - if err := msg.ValidateBasic(); err != nil { - return err - } - return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg) - }, - } - - flags.AddTxFlagsToCmd(cmd) - - return cmd -} \ No newline at end of file diff --git a/ignite/templates/typed/singleton/files/messages/x/{{moduleName}}/client/cli/tx_{{typeName}}_test.go.plush b/ignite/templates/typed/singleton/files/messages/x/{{moduleName}}/client/cli/tx_{{typeName}}_test.go.plush deleted file mode 100644 index 1d3b6a8409..0000000000 --- a/ignite/templates/typed/singleton/files/messages/x/{{moduleName}}/client/cli/tx_{{typeName}}_test.go.plush +++ /dev/null @@ -1,151 +0,0 @@ -package cli_test - -import ( - "fmt" - "testing" - - sdkmath "cosmossdk.io/math" - "github.com/cosmos/cosmos-sdk/client/flags" - clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/stretchr/testify/require" - - "<%= ModulePath %>/x/<%= ModuleName %>/client/cli" -) - -func (s *IntegrationTestSuite) TestCreate<%= TypeName.UpperCamel %>() { - var ( - net = s.network(nil) - val = net.Validators[0] - ctx = val.ClientCtx - fields = []string{<%= for (field) in Fields { %> "<%= field.DefaultTestValue() %>", <% } %>} - ) - tests := []struct { - desc string - args []string - err error - code uint32 - }{ - { - desc: "valid", - args: []string{ - fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()), - fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), - fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), - fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(net.Config.BondDenom, sdkmath.NewInt(10))).String()), - }, - }, - } - for _, tc := range tests { - s.T().Run(tc.desc, func(t *testing.T) { - args := append([]string{}, fields...) - args = append(args, tc.args...) - out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdCreate<%= TypeName.UpperCamel %>(), args) - if tc.err != nil { - require.ErrorIs(t, err, tc.err) - return - } - require.NoError(t, err) - - var resp sdk.TxResponse - require.NoError(t, ctx.Codec.UnmarshalJSON(out.Bytes(), &resp)) - require.NoError(t, clitestutil.CheckTxCode(net, ctx, resp.TxHash, tc.code)) - }) - } -} - -func (s *IntegrationTestSuite) TestUpdate<%= TypeName.UpperCamel %>() { - var ( - net = s.network(nil) - val = net.Validators[0] - ctx = val.ClientCtx - fields = []string{<%= for (field) in Fields { %> "<%= field.DefaultTestValue() %>", <% } %>} - common = []string{ - fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()), - fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), - fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), - fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(net.Config.BondDenom, sdkmath.NewInt(10))).String()), - } - args = append([]string{}, fields...) - ) - args = append(args, common...) - - _, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdCreate<%= TypeName.UpperCamel %>(), args) - s.Require().NoError(err) - s.Require().NoError(net.WaitForNextBlock()) - - tests := []struct { - desc string - args []string - code uint32 - err error - }{ - { - desc: "valid", - args: common, - }, - } - for _, tc := range tests { - s.T().Run(tc.desc, func(t *testing.T) { - args := append([]string{}, fields...) - args = append(args, tc.args...) - out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdUpdate<%= TypeName.UpperCamel %>(), args) - if tc.err != nil { - require.ErrorIs(t, err, tc.err) - return - } - require.NoError(t, err) - - var resp sdk.TxResponse - require.NoError(t, ctx.Codec.UnmarshalJSON(out.Bytes(), &resp)) - require.NoError(t, clitestutil.CheckTxCode(net, ctx, resp.TxHash, tc.code)) - }) - } -} - -func (s *IntegrationTestSuite) TestDelete<%= TypeName.UpperCamel %>() { - var ( - net = s.network(nil) - val = net.Validators[0] - ctx = val.ClientCtx - fields = []string{<%= for (field) in Fields { %> "<%= field.DefaultTestValue() %>", <% } %>} - common = []string{ - fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()), - fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), - fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), - fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(net.Config.BondDenom, sdkmath.NewInt(10))).String()), - } - args = append([]string{}, fields...) - ) - args = append(args, common...) - - _, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdCreate<%= TypeName.UpperCamel %>(), args) - s.Require().NoError(err) - s.Require().NoError(net.WaitForNextBlock()) - - tests := []struct { - desc string - args []string - code uint32 - err error - }{ - { - desc: "valid", - args: common, - }, - } - for _, tc := range tests { - s.T().Run(tc.desc, func(t *testing.T) { - out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdDelete<%= TypeName.UpperCamel %>(), append([]string{}, tc.args...)) - if tc.err != nil { - require.ErrorIs(t, err, tc.err) - return - } - require.NoError(t, err) - - var resp sdk.TxResponse - require.NoError(t, ctx.Codec.UnmarshalJSON(out.Bytes(), &resp)) - require.NoError(t, clitestutil.CheckTxCode(net, ctx, resp.TxHash, tc.code)) - }) - } -} diff --git a/ignite/templates/typed/singleton/singleton.go b/ignite/templates/typed/singleton/singleton.go index 10d8cc45c8..9059678129 100644 --- a/ignite/templates/typed/singleton/singleton.go +++ b/ignite/templates/typed/singleton/singleton.go @@ -5,6 +5,7 @@ import ( "fmt" "math/rand" "path/filepath" + "strings" "github.com/emicklei/proto" @@ -157,17 +158,26 @@ func protoRPCModify(opts *typed.Options) genny.RunFn { func clientCliQueryModify(replacer placeholder.Replacer, opts *typed.Options) genny.RunFn { return func(r *genny.Runner) error { - path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "client/cli/query.go") + path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "module/autocli.go") f, err := r.Disk.Find(path) if err != nil { return err } - template := `cmd.AddCommand(CmdShow%[2]v()) -%[1]v` - replacement := fmt.Sprintf(template, typed.Placeholder, + + template := `{ + RpcMethod: "%[2]v", + Use: "show-%[3]v", + Short: "show %[4]v", + }, + %[1]v` + replacement := fmt.Sprintf( + template, + typed.PlaceholderAutoCLIQuery, opts.TypeName.UpperCamel, + opts.TypeName.Kebab, + opts.TypeName.Original, ) - content := replacer.Replace(f.String(), typed.Placeholder, replacement) + content := replacer.Replace(f.String(), typed.PlaceholderAutoCLIQuery, replacement) newFile := genny.NewFileS(path, content) return r.File(newFile) } @@ -429,17 +439,46 @@ func protoTxModify(opts *typed.Options) genny.RunFn { func clientCliTxModify(replacer placeholder.Replacer, opts *typed.Options) genny.RunFn { return func(r *genny.Runner) error { - path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "client/cli/tx.go") + path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "module/autocli.go") f, err := r.Disk.Find(path) if err != nil { return err } - template := `cmd.AddCommand(CmdCreate%[2]v()) - cmd.AddCommand(CmdUpdate%[2]v()) - cmd.AddCommand(CmdDelete%[2]v()) -%[1]v` - replacement := fmt.Sprintf(template, typed.Placeholder, opts.TypeName.UpperCamel) - content := replacer.Replace(f.String(), typed.Placeholder, replacement) + + var positionalArgs string + for _, field := range opts.Fields { + positionalArgs += fmt.Sprintf(`{ProtoField: "%s"}, `, field.ProtoFieldName()) + } + + template := `{ + RpcMethod: "Create%[2]v", + Use: "create-%[3]v", + Short: "Create %[4]v", + PositionalArgs: []*autocliv1.PositionalArgDescriptor{%[5]s}, + }, + { + RpcMethod: "Update%[2]v", + Use: "update-%[3]v", + Short: "Update %[4]v", + PositionalArgs: []*autocliv1.PositionalArgDescriptor{%[5]s}, + }, + { + RpcMethod: "Delete%[2]v", + Use: "delete-%[3]v", + Short: "Delete %[4]v", + }, + %[1]v` + + replacement := fmt.Sprintf( + template, + typed.PlaceholderAutoCLITx, + opts.TypeName.UpperCamel, + opts.TypeName.Kebab, + opts.TypeName.Original, + strings.TrimSpace(positionalArgs), + ) + + content := replacer.Replace(f.String(), typed.PlaceholderAutoCLITx, replacement) newFile := genny.NewFileS(path, content) return r.File(newFile) }