Skip to content

Commit

Permalink
feat: set the proto-dir flag only for the scaffold chain command …
Browse files Browse the repository at this point in the history
…and use the proto path from the config (#4100)

* only use the proto dir from config

* add proto dir only for scaffold a new chain

* add changelog

* fix integartion tests

* remove unused flag from integration tests cmds

* fix revive lint use

* add a helper to find the root chain path

* remove unused method

* remove unused tests

---------

Co-authored-by: Pantani <Pantani>
  • Loading branch information
Pantani authored Apr 25, 2024
1 parent 566ad31 commit af41d3d
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 156 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- [#4071](https://github.com/ignite/cli/pull/4071) Support custom proto path
- [#3718](https://github.com/ignite/cli/pull/3718) Add `gen-mig-diffs` tool app to compare scaffold output of two versions of ignite
- [#4077](https://github.com/ignite/cli/pull/4077) Merge the swagger files manually instead use nodetime `swagger-combine`
- [#4100](https://github.com/ignite/cli/pull/4100) Set the `proto-dir` flag only for the `scaffold chain` command and use the proto path from the config

### Changes

Expand Down
22 changes: 7 additions & 15 deletions ignite/cmd/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/ignite/cli/v29/ignite/pkg/cosmosgen"
"github.com/ignite/cli/v29/ignite/pkg/errors"
"github.com/ignite/cli/v29/ignite/pkg/goanalysis"
"github.com/ignite/cli/v29/ignite/pkg/gomodulepath"
"github.com/ignite/cli/v29/ignite/pkg/xast"
"github.com/ignite/cli/v29/ignite/pkg/xgenny"
"github.com/ignite/cli/v29/ignite/services/chain"
Expand Down Expand Up @@ -117,31 +116,25 @@ func preRunHandler(cmd *cobra.Command, _ []string) error {
session := cliui.New()
defer session.End()

path := flagGetPath(cmd)
path, err := filepath.Abs(path)
appPath, err := goModulePath(cmd)
if err != nil {
return err
}

_, appPath, err := gomodulepath.Find(path)
cfg, cfgPath, err := getChainConfig(cmd)
if err != nil {
return err
}

if err := configMigrationPreRunHandler(cmd, session, appPath); err != nil {
if err := configMigrationPreRunHandler(cmd, session, appPath, cfgPath); err != nil {
return err
}

if err := toolsMigrationPreRunHandler(cmd, session, appPath); err != nil {
return err
}

protoDir, err := getProtoDirFromConfig(cmd)
if err != nil {
return err
}

return bufMigrationPreRunHandler(cmd, session, appPath, protoDir)
return bufMigrationPreRunHandler(cmd, session, appPath, cfg.Build.Proto.Path)
}

func toolsMigrationPreRunHandler(cmd *cobra.Command, session *cliui.Session, appPath string) error {
Expand Down Expand Up @@ -254,8 +247,8 @@ func bufMigrationPreRunHandler(cmd *cobra.Command, session *cliui.Session, appPa
return nil
}

func configMigrationPreRunHandler(cmd *cobra.Command, session *cliui.Session, appPath string) error {
rawCfg, configPath, err := getRawConfig(cmd)
func configMigrationPreRunHandler(cmd *cobra.Command, session *cliui.Session, appPath, cfgPath string) error {
rawCfg, err := os.ReadFile(cfgPath)
if err != nil {
return err
}
Expand Down Expand Up @@ -295,10 +288,9 @@ func configMigrationPreRunHandler(cmd *cobra.Command, session *cliui.Session, ap
return err
}

if err := os.WriteFile(configPath, buf.Bytes(), 0o755); err != nil {
if err := os.WriteFile(cfgPath, buf.Bytes(), 0o755); err != nil {
return errors.Errorf("config file migration failed: %w", err)
}
}

return nil
}
60 changes: 36 additions & 24 deletions ignite/cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ignitecmd

import (
"bytes"
"context"
"fmt"
"os"
Expand All @@ -15,16 +14,23 @@ import (

"github.com/ignite/cli/v29/ignite/config"
chainconfig "github.com/ignite/cli/v29/ignite/config/chain"
"github.com/ignite/cli/v29/ignite/config/chain/defaults"
"github.com/ignite/cli/v29/ignite/pkg/cache"
"github.com/ignite/cli/v29/ignite/pkg/cliui"
uilog "github.com/ignite/cli/v29/ignite/pkg/cliui/log"
"github.com/ignite/cli/v29/ignite/pkg/errors"
"github.com/ignite/cli/v29/ignite/pkg/gitpod"
"github.com/ignite/cli/v29/ignite/pkg/goenv"
"github.com/ignite/cli/v29/ignite/pkg/gomodulepath"
"github.com/ignite/cli/v29/ignite/version"
)

type key int

const (
keyChainConfig key = iota
keyChainConfigPath key = iota
)

const (
flagPath = "path"
flagHome = "home"
Expand Down Expand Up @@ -123,6 +129,20 @@ func flagGetPath(cmd *cobra.Command) (path string) {
return
}

func goModulePath(cmd *cobra.Command) (string, error) {
path := flagGetPath(cmd)
path, err := filepath.Abs(path)
if err != nil {
return "", err
}

_, appPath, err := gomodulepath.Find(path)
if err != nil {
return "", err
}
return appPath, err
}

func flagSetHome() *flag.FlagSet {
fs := flag.NewFlagSet("", flag.ContinueOnError)
fs.String(flagHome, "", "directory where the blockchain node is initialized")
Expand All @@ -134,17 +154,6 @@ func getHome(cmd *cobra.Command) (home string) {
return
}

func flagSetProtoDir() *flag.FlagSet {
fs := flag.NewFlagSet("", flag.ContinueOnError)
fs.String(flagProtoDir, defaults.ProtoDir, "chain proto directory")
return fs
}

func flagGetProtoDir(cmd *cobra.Command) (config string) {
config, _ = cmd.Flags().GetString(flagProtoDir)
return
}

func flagSetConfig() *flag.FlagSet {
fs := flag.NewFlagSet("", flag.ContinueOnError)
fs.StringP(flagConfig, "c", "", "path to Ignite config file (default: ./config.yml)")
Expand All @@ -156,11 +165,15 @@ func getConfig(cmd *cobra.Command) (config string) {
return
}

func getRawConfig(cmd *cobra.Command) ([]byte, string, error) {
func getChainConfig(cmd *cobra.Command) (*chainconfig.Config, string, error) {
cfg, ok := cmd.Context().Value(keyChainConfig).(*chainconfig.Config)
if ok {
configPath := cmd.Context().Value(keyChainConfigPath).(string)
return cfg, configPath, nil
}
configPath := getConfig(cmd)

path := flagGetPath(cmd)
path, err := filepath.Abs(path)
path, err := goModulePath(cmd)
if err != nil {
return nil, "", err
}
Expand All @@ -171,16 +184,15 @@ func getRawConfig(cmd *cobra.Command) ([]byte, string, error) {
}
}

rawConfig, err := os.ReadFile(configPath)
return rawConfig, configPath, err
}

func getProtoDirFromConfig(cmd *cobra.Command) (string, error) {
rawCfg, _, err := getRawConfig(cmd)
cfg, err = chainconfig.ParseFile(configPath)
if err != nil {
return "", err
return nil, "", err
}
return chainconfig.ReadProtoPath(bytes.NewReader(rawCfg))
ctx := context.WithValue(cmd.Context(), keyChainConfig, cfg)
ctx = context.WithValue(ctx, keyChainConfigPath, configPath)
cmd.SetContext(ctx)

return cfg, configPath, err
}

func flagSetYes() *flag.FlagSet {
Expand Down
27 changes: 11 additions & 16 deletions ignite/cmd/scaffold.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package ignitecmd

import (
"path/filepath"

"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"

"github.com/ignite/cli/v29/ignite/pkg/cliui"
"github.com/ignite/cli/v29/ignite/pkg/cosmosver"
"github.com/ignite/cli/v29/ignite/pkg/errors"
"github.com/ignite/cli/v29/ignite/pkg/gomodulepath"
"github.com/ignite/cli/v29/ignite/pkg/xgit"
"github.com/ignite/cli/v29/ignite/services/scaffolder"
"github.com/ignite/cli/v29/ignite/version"
Expand Down Expand Up @@ -132,9 +129,6 @@ with an "--ibc" flag. Note that the default module is not IBC-enabled.
NewScaffoldReact(),
)

// Add flags required for the configMigrationPreRunHandler
c.PersistentFlags().AddFlagSet(flagSetProtoDir())

return c
}

Expand All @@ -143,18 +137,15 @@ func migrationPreRunHandler(cmd *cobra.Command, args []string) error {
return err
}

var (
path = flagGetPath(cmd)
protoDir = flagGetProtoDir(cmd)
session = cliui.New()
)
session := cliui.New()
defer session.End()
path, err := filepath.Abs(path)

cfg, _, err := getChainConfig(cmd)
if err != nil {
return err
}

_, appPath, err := gomodulepath.Find(path)
appPath, err := goModulePath(cmd)
if err != nil {
return err
}
Expand All @@ -172,7 +163,7 @@ func migrationPreRunHandler(cmd *cobra.Command, args []string) error {
return err
}

return bufMigrationPreRunHandler(cmd, session, appPath, protoDir)
return bufMigrationPreRunHandler(cmd, session, appPath, cfg.Build.Proto.Path)
}

func scaffoldType(
Expand All @@ -188,7 +179,6 @@ func scaffoldType(
withoutSimulation = flagGetNoSimulation(cmd)
signer = flagGetSigner(cmd)
appPath = flagGetPath(cmd)
protoDir = flagGetProtoDir(cmd)
)

var options []scaffolder.AddTypeOption
Expand All @@ -213,7 +203,12 @@ func scaffoldType(
session := cliui.New(cliui.StartSpinnerWithText(statusScaffolding))
defer session.End()

sc, err := scaffolder.New(cmd.Context(), appPath, protoDir)
cfg, _, err := getChainConfig(cmd)
if err != nil {
return err
}

sc, err := scaffolder.New(cmd.Context(), appPath, cfg.Build.Proto.Path)
if err != nil {
return err
}
Expand Down
12 changes: 8 additions & 4 deletions ignite/cmd/scaffold_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ignitecmd
import (
"github.com/spf13/cobra"

"github.com/ignite/cli/v29/ignite/config/chain/defaults"
"github.com/ignite/cli/v29/ignite/pkg/cliui"
"github.com/ignite/cli/v29/ignite/pkg/errors"
"github.com/ignite/cli/v29/ignite/pkg/xfilepath"
Expand Down Expand Up @@ -88,6 +89,8 @@ about Cosmos SDK on https://docs.cosmos.network
c.Flags().Bool(flagSkipProto, false, "skip proto generation")
c.Flags().Bool(flagMinimal, false, "create a minimal blockchain (with the minimum required Cosmos SDK modules)")
c.Flags().Bool(flagIsConsumer, false, "scafffold an ICS consumer chain")
c.Flags().String(flagProtoDir, defaults.ProtoDir, "chain proto directory")

// Cannot have both minimal and consumer flag
c.MarkFlagsMutuallyExclusive(flagIsConsumer, flagMinimal)

Expand All @@ -99,17 +102,18 @@ func scaffoldChainHandler(cmd *cobra.Command, args []string) error {
defer session.End()

var (
name = args[0]
addressPrefix = getAddressPrefix(cmd)
appPath = flagGetPath(cmd)
protoDir = flagGetProtoDir(cmd)
name = args[0]
addressPrefix = getAddressPrefix(cmd)
appPath = flagGetPath(cmd)

noDefaultModule, _ = cmd.Flags().GetBool(flagNoDefaultModule)
skipGit, _ = cmd.Flags().GetBool(flagSkipGit)
minimal, _ = cmd.Flags().GetBool(flagMinimal)
isConsumer, _ = cmd.Flags().GetBool(flagIsConsumer)
params, _ = cmd.Flags().GetStringSlice(flagParams)
moduleConfigs, _ = cmd.Flags().GetStringSlice(flagModuleConfigs)
skipProto, _ = cmd.Flags().GetBool(flagSkipProto)
protoDir, _ = cmd.Flags().GetString(flagProtoDir)
)

if noDefaultModule {
Expand Down
8 changes: 6 additions & 2 deletions ignite/cmd/scaffold_configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,22 @@ func scaffoldConfigsHandler(cmd *cobra.Command, args []string) error {
configs = args[0:]
appPath = flagGetPath(cmd)
moduleName = flagGetModule(cmd)
protoDir = flagGetProtoDir(cmd)
)

session := cliui.New(cliui.StartSpinnerWithText(statusScaffolding))
defer session.End()

cfg, _, err := getChainConfig(cmd)
if err != nil {
return err
}

cacheStorage, err := newCache(cmd)
if err != nil {
return err
}

sc, err := scaffolder.New(cmd.Context(), appPath, protoDir)
sc, err := scaffolder.New(cmd.Context(), appPath, cfg.Build.Proto.Path)
if err != nil {
return err
}
Expand Down
8 changes: 6 additions & 2 deletions ignite/cmd/scaffold_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,16 @@ func messageHandler(cmd *cobra.Command, args []string) error {
signer = flagGetSigner(cmd)
appPath = flagGetPath(cmd)
withoutSimulation = flagGetNoSimulation(cmd)
protoDir = flagGetProtoDir(cmd)
)

session := cliui.New(cliui.StartSpinnerWithText(statusScaffolding))
defer session.End()

cfg, _, err := getChainConfig(cmd)
if err != nil {
return err
}

cacheStorage, err := newCache(cmd)
if err != nil {
return err
Expand All @@ -114,7 +118,7 @@ func messageHandler(cmd *cobra.Command, args []string) error {
options = append(options, scaffolder.WithoutSimulation())
}

sc, err := scaffolder.New(cmd.Context(), appPath, protoDir)
sc, err := scaffolder.New(cmd.Context(), appPath, cfg.Build.Proto.Path)
if err != nil {
return err
}
Expand Down
12 changes: 8 additions & 4 deletions ignite/cmd/scaffold_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,18 @@ params.

func scaffoldModuleHandler(cmd *cobra.Command, args []string) error {
var (
name = args[0]
appPath = flagGetPath(cmd)
protoDir = flagGetProtoDir(cmd)
name = args[0]
appPath = flagGetPath(cmd)
)

session := cliui.New(cliui.StartSpinnerWithText(statusScaffolding))
defer session.End()

cfg, _, err := getChainConfig(cmd)
if err != nil {
return err
}

ibcModule, _ := cmd.Flags().GetBool(flagIBC)
ibcOrdering, _ := cmd.Flags().GetString(flagIBCOrdering)
requireRegistration, _ := cmd.Flags().GetBool(flagRequireRegistration)
Expand Down Expand Up @@ -172,7 +176,7 @@ func scaffoldModuleHandler(cmd *cobra.Command, args []string) error {
var msg bytes.Buffer
fmt.Fprintf(&msg, "\n🎉 Module created %s.\n\n", name)

sc, err := scaffolder.New(cmd.Context(), appPath, protoDir)
sc, err := scaffolder.New(cmd.Context(), appPath, cfg.Build.Proto.Path)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit af41d3d

Please sign in to comment.