diff --git a/rollkit/CHANGELOG.md b/rollkit/CHANGELOG.md index 15371cf9..0a5ed21c 100644 --- a/rollkit/CHANGELOG.md +++ b/rollkit/CHANGELOG.md @@ -1,5 +1,9 @@ # Rollkit App Changelog +## Unreleased + +* [#112](https://github.com/ignite/apps/pull/112) Use default command instead cobra commands. + ## [`v0.2.1`](https://github.com/ignite/apps/releases/tag/rollkit/v0.2.1) * [#106](https://github.com/ignite/apps/pull/106) Improve bonded tokens setting. diff --git a/rollkit/cmd/add.go b/rollkit/cmd/add.go index b0405c7c..1102ac02 100644 --- a/rollkit/cmd/add.go +++ b/rollkit/cmd/add.go @@ -4,13 +4,12 @@ import ( "context" "path/filepath" - "github.com/spf13/cobra" - "github.com/ignite/cli/v28/ignite/pkg/cliui" "github.com/ignite/cli/v28/ignite/pkg/gocmd" "github.com/ignite/cli/v28/ignite/pkg/placeholder" "github.com/ignite/cli/v28/ignite/pkg/xgenny" "github.com/ignite/cli/v28/ignite/services/chain" + "github.com/ignite/cli/v28/ignite/services/plugin" "github.com/ignite/apps/rollkit/template" ) @@ -21,51 +20,44 @@ const ( flagPath = "path" ) -func NewRollkitAdd() *cobra.Command { - c := &cobra.Command{ - Use: "add", - Short: "Add rollkit support", - Long: "Add rollkit support to your Cosmos SDK chain", - Args: cobra.NoArgs, - RunE: func(cmd *cobra.Command, args []string) error { - session := cliui.New(cliui.StartSpinnerWithText(statusScaffolding)) - defer session.End() - - appPath, err := cmd.Flags().GetString(flagPath) - if err != nil { - return err - } - absPath, err := filepath.Abs(appPath) - if err != nil { - return err - } - - c, err := chain.New(absPath, chain.CollectEvents(session.EventBus())) - if err != nil { - return err - } - - g, err := template.NewRollKitGenerator(c) - if err != nil { - return err - } - - _, err = xgenny.RunWithValidation(placeholder.New(), g) - if err != nil { - return err - } - - if finish(cmd.Context(), session, c.AppPath()) != nil { - return err - } - - return session.Printf("🎉 RollKit added (`%[1]v`).\n", c.AppPath(), c.Name()) - }, +func AddHandler(ctx context.Context, cmd *plugin.ExecutedCommand) error { + flags, err := cmd.NewFlags() + if err != nil { + return err + } + + session := cliui.New(cliui.StartSpinnerWithText(statusScaffolding)) + defer session.End() + + appPath, err := flags.GetString(flagPath) + if err != nil { + return err + } + absPath, err := filepath.Abs(appPath) + if err != nil { + return err } - c.Flags().StringP(flagPath, "p", ".", "path of the app") + c, err := chain.New(absPath, chain.CollectEvents(session.EventBus())) + if err != nil { + return err + } + + g, err := template.NewRollKitGenerator(c) + if err != nil { + return err + } + + _, err = xgenny.RunWithValidation(placeholder.New(), g) + if err != nil { + return err + } + + if finish(ctx, session, c.AppPath()) != nil { + return err + } - return c + return session.Printf("🎉 RollKit added (`%[1]v`).\n", c.AppPath(), c.Name()) } // finish finalize the scaffolded code (formating, dependencies) diff --git a/rollkit/cmd/cmd.go b/rollkit/cmd/cmd.go index c255e8ce..a5e2d22b 100644 --- a/rollkit/cmd/cmd.go +++ b/rollkit/cmd/cmd.go @@ -1,22 +1,43 @@ package cmd -import "github.com/spf13/cobra" +import ( + "github.com/ignite/cli/v28/ignite/services/plugin" +) -// NewRollkit creates a new rollkit command that holds -// some other sub commands related to Rollkit. -func NewRollkit() *cobra.Command { - c := &cobra.Command{ - Use: "rollkit [command]", - Aliases: []string{"r"}, - Short: "Ignite rollkit integration", - SilenceUsage: true, - SilenceErrors: true, +// GetCommands returns the list of app commands. +func GetCommands() []*plugin.Command { + return []*plugin.Command{ + { + Use: "rollkit [command]", + Aliases: []string{"r"}, + Short: "Ignite rollkit integration", + Commands: []*plugin.Command{ + { + Use: "add", + Short: "Add rollkit support", + Long: "Add rollkit support to your Cosmos SDK chain", + Flags: []*plugin.Flag{ + { + Name: flagPath, + Usage: "path of the app", + Shorthand: "p", + Type: plugin.FlagTypeString, + }, + }, + }, + { + Use: "init", + Short: "Init rollkit support", + Flags: []*plugin.Flag{ + { + Name: flagPath, + Usage: "path of the app", + Shorthand: "p", + Type: plugin.FlagTypeString, + }, + }, + }, + }, + }, } - - // add sub commands. - c.AddCommand( - NewRollkitAdd(), - NewRollkitInit(), - ) - return c } diff --git a/rollkit/cmd/init.go b/rollkit/cmd/init.go index 1d75b930..3c9d525b 100644 --- a/rollkit/cmd/init.go +++ b/rollkit/cmd/init.go @@ -1,6 +1,7 @@ package cmd import ( + "context" "math" "os" "path/filepath" @@ -12,107 +13,96 @@ import ( cmttypes "github.com/cometbft/cometbft/types" sdk "github.com/cosmos/cosmos-sdk/types" genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types" - "github.com/pkg/errors" - "github.com/spf13/cobra" - configchain "github.com/ignite/cli/v28/ignite/config/chain" "github.com/ignite/cli/v28/ignite/pkg/cliui" "github.com/ignite/cli/v28/ignite/pkg/cliui/colors" "github.com/ignite/cli/v28/ignite/services/chain" + "github.com/ignite/cli/v28/ignite/services/plugin" + "github.com/pkg/errors" ) const defaultValPower = 1 -func NewRollkitInit() *cobra.Command { - c := &cobra.Command{ - Use: "init", - Short: "Init rollkit support", - Args: cobra.NoArgs, - RunE: func(cmd *cobra.Command, args []string) error { - session := cliui.New() - defer session.End() - - appPath, err := cmd.Flags().GetString(flagPath) - if err != nil { - return err - } - absPath, err := filepath.Abs(appPath) - if err != nil { - return err - } - - rc, err := chain.New(absPath, chain.CollectEvents(session.EventBus())) - if err != nil { - return err - } - - // use val power to set validator power in ignite config.yaml - igniteConfig, err := rc.Config() - if err != nil { - return err - } - - coins := sdk.NewCoin("stake", sdkmath.NewInt((defaultValPower * int64(math.Pow10(6))))) - igniteConfig.Validators[0].Bonded = coins.String() - for i, account := range igniteConfig.Accounts { - if account.Name == igniteConfig.Validators[0].Name { - igniteConfig.Accounts[i].Coins = []string{coins.String()} - } - } - - if err := configchain.Save(*igniteConfig, rc.ConfigPath()); err != nil { - return err - } - - if err := rc.Init(cmd.Context(), chain.InitArgsAll); err != nil { - return err - } - - home, err := rc.Home() - if err != nil { - return err - } - - // modify genesis (add sequencer) - genesisPath, err := rc.GenesisPath() - if err != nil { - return err - } - - genesis, err := genutiltypes.AppGenesisFromFile(genesisPath) - if err != nil { - return err - } - - pubKey, err := getPubKey(home) - if err != nil { - return err - } - - genesis.Consensus.Validators = []cmttypes.GenesisValidator{ - { - Name: "Rollkit Sequencer", - Address: pubKey.Address(), - PubKey: pubKey, - Power: defaultValPower, - }, - } - - if err := genesis.SaveAs(genesisPath); err != nil { - return err - } - - return session.Printf("🗃 Initialized. Checkout your rollkit chain's home (data) directory: %s\n", colors.Info(home)) - }, +func InitHandler(ctx context.Context, cmd *plugin.ExecutedCommand) error { + flags, err := cmd.NewFlags() + if err != nil { + return err + } + + session := cliui.New() + defer session.End() + + appPath, err := flags.GetString(flagPath) + if err != nil { + return err + } + absPath, err := filepath.Abs(appPath) + if err != nil { + return err + } + + rc, err := chain.New(absPath, chain.CollectEvents(session.EventBus())) + if err != nil { + return err + } + + // use val power to set validator power in ignite config.yaml + igniteConfig, err := rc.Config() + if err != nil { + return err + } + + coins := sdk.NewCoin("stake", sdkmath.NewInt((defaultValPower * int64(math.Pow10(6))))) + igniteConfig.Validators[0].Bonded = coins.String() + for i, account := range igniteConfig.Accounts { + if account.Name == igniteConfig.Validators[0].Name { + igniteConfig.Accounts[i].Coins = []string{coins.String()} + } } - c.Flags().StringP(flagPath, "p", ".", "path of the app") + if err := configchain.Save(*igniteConfig, rc.ConfigPath()); err != nil { + return err + } + + if err := rc.Init(ctx, chain.InitArgsAll); err != nil { + return err + } + + home, err := rc.Home() + if err != nil { + return err + } - // deprecated flags - c.Flags().Bool("local-da", false, "this flag does nothing but is kept for backward compatibility") - c.Flags().MarkDeprecated("local-da", "this flag does nothing but is kept for backward compatibility") + // modify genesis (add sequencer) + genesisPath, err := rc.GenesisPath() + if err != nil { + return err + } + + genesis, err := genutiltypes.AppGenesisFromFile(genesisPath) + if err != nil { + return err + } + + pubKey, err := getPubKey(home) + if err != nil { + return err + } + + genesis.Consensus.Validators = []cmttypes.GenesisValidator{ + { + Name: "Rollkit Sequencer", + Address: pubKey.Address(), + PubKey: pubKey, + Power: defaultValPower, + }, + } + + if err := genesis.SaveAs(genesisPath); err != nil { + return err + } - return c + return session.Printf("🗃 Initialized. Checkout your rollkit chain's home (data) directory: %s\n", colors.Info(home)) } // getPubKey returns the validator public key. diff --git a/rollkit/go.mod b/rollkit/go.mod index 9a72e9ca..db15f3ff 100644 --- a/rollkit/go.mod +++ b/rollkit/go.mod @@ -11,7 +11,6 @@ require ( github.com/hashicorp/go-plugin v1.6.0 github.com/ignite/cli/v28 v28.4.0 github.com/pkg/errors v0.9.1 - github.com/spf13/cobra v1.8.0 github.com/stretchr/testify v1.9.0 ) @@ -187,6 +186,7 @@ require ( github.com/sourcegraph/syntaxhighlight v0.0.0-20170531221838-bd320f5d308e // indirect github.com/spf13/afero v1.11.0 // indirect github.com/spf13/cast v1.6.0 // indirect + github.com/spf13/cobra v1.8.0 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/spf13/viper v1.18.2 // indirect github.com/subosito/gotenv v1.6.0 // indirect diff --git a/rollkit/main.go b/rollkit/main.go index 6347d369..981afef0 100644 --- a/rollkit/main.go +++ b/rollkit/main.go @@ -2,11 +2,12 @@ package main import ( "context" - "os" hplugin "github.com/hashicorp/go-plugin" - "github.com/ignite/apps/rollkit/cmd" + "github.com/ignite/cli/v28/ignite/pkg/errors" "github.com/ignite/cli/v28/ignite/services/plugin" + + "github.com/ignite/apps/rollkit/cmd" ) var _ plugin.Interface = app{} @@ -14,19 +15,27 @@ var _ plugin.Interface = app{} type app struct{} func (app) Manifest(context.Context) (*plugin.Manifest, error) { - m := &plugin.Manifest{Name: "rollkit"} - m.ImportCobraCommand(cmd.NewRollkit(), "ignite") - return m, nil + return &plugin.Manifest{ + Name: "rollkit", + Commands: cmd.GetCommands(), + }, nil } -func (app) Execute(_ context.Context, c *plugin.ExecutedCommand, _ plugin.ClientAPI) error { +func (app) Execute(ctx context.Context, c *plugin.ExecutedCommand, _ plugin.ClientAPI) error { // Instead of a switch on c.Use, we run the root command like if // we were in a command line context. This implies to set os.Args // correctly. // Remove the first arg "ignite" from OSArgs because our rollkit // command root is "rollkit" not "ignite". - os.Args = c.OsArgs[1:] - return cmd.NewRollkit().Execute() + args := c.OsArgs[2:] + switch args[0] { + case "add": + return cmd.AddHandler(ctx, c) + case "init": + return cmd.InitHandler(ctx, c) + default: + return errors.Errorf("unknown command: %s", c.Path) + } } func (app) ExecuteHookPre(context.Context, *plugin.ExecutedHook, plugin.ClientAPI) error {