Skip to content

Commit

Permalink
feat(rollkit): use default command instead cobra commands (#112)
Browse files Browse the repository at this point in the history
* use default command instead cobra commands

* fix GetCommands comment

* remove unused flag and dependency
  • Loading branch information
Pantani authored Jul 11, 2024
1 parent 3a96605 commit 439bf17
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 159 deletions.
4 changes: 4 additions & 0 deletions rollkit/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
80 changes: 36 additions & 44 deletions rollkit/cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)
Expand Down
55 changes: 38 additions & 17 deletions rollkit/cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -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
}
168 changes: 79 additions & 89 deletions rollkit/cmd/init.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"context"
"math"
"os"
"path/filepath"
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion rollkit/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit 439bf17

Please sign in to comment.