Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: set the proto-dir flag only for the scaffold chain command and use the proto path from the config #4100

Merged
merged 12 commits into from
Apr 25, 2024
Next Next commit
only use the proto dir from config
  • Loading branch information
Pantani authored and Pantani committed Apr 23, 2024
commit 9961aa4973dcb7603abf80fb13c7610f33aaf575
17 changes: 8 additions & 9 deletions ignite/cmd/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,20 +128,20 @@ func preRunHandler(cmd *cobra.Command, _ []string) error {
return err
}

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

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

protoDir, err := getProtoDirFromConfig(cmd)
if err != nil {
if err := toolsMigrationPreRunHandler(cmd, session, appPath); 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 +254,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 +295,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
}
38 changes: 16 additions & 22 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,7 +14,6 @@

"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"
Expand All @@ -32,6 +30,9 @@
flagClearCache = "clear-cache"
flagSkipProto = "skip-proto"

ctxChainConfig = "chain-config"
ctxChainConfigPath = "chain-config-path"

checkVersionTimeout = time.Millisecond * 600
cacheFileName = "ignite_cache.db"

Expand Down Expand Up @@ -134,17 +135,6 @@
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,7 +146,12 @@
return
}

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

path := flagGetPath(cmd)
Expand All @@ -171,16 +166,15 @@
}
}

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(), ctxChainConfig, cfg)

Check warning on line 173 in ignite/cmd/cmd.go

View workflow job for this annotation

GitHub Actions / Lint Go code

context-keys-type: should not use basic type string as key in context.WithValue (revive)
ctx = context.WithValue(ctx, ctxChainConfigPath, configPath)

Check warning on line 174 in ignite/cmd/cmd.go

View workflow job for this annotation

GitHub Actions / Lint Go code

context-keys-type: should not use basic type string as key in context.WithValue (revive)
cmd.SetContext(ctx)

return cfg, configPath, err
}

func flagSetYes() *flag.FlagSet {
Expand Down
27 changes: 16 additions & 11 deletions ignite/cmd/scaffold.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ const (
flagNoSimulation = "no-simulation"
flagResponse = "response"
flagDescription = "desc"
flagProtoDir = "proto-dir"

msgCommitPrefix = "Your saved project changes have not been committed.\nTo enable reverting to your current state, commit your saved changes."
msgCommitPrompt = "Do you want to proceed without committing your saved changes"
Expand Down Expand Up @@ -132,9 +131,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 @@ -144,12 +140,17 @@ func migrationPreRunHandler(cmd *cobra.Command, args []string) error {
}

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

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

path, err = filepath.Abs(path)
if err != nil {
return err
}
Expand All @@ -172,7 +173,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 +189,6 @@ func scaffoldType(
withoutSimulation = flagGetNoSimulation(cmd)
signer = flagGetSigner(cmd)
appPath = flagGetPath(cmd)
protoDir = flagGetProtoDir(cmd)
)

var options []scaffolder.AddTypeOption
Expand All @@ -213,7 +213,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
10 changes: 7 additions & 3 deletions ignite/cmd/scaffold_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ func scaffoldChainHandler(cmd *cobra.Command, args []string) error {
name = args[0]
addressPrefix = getAddressPrefix(cmd)
appPath = flagGetPath(cmd)
protoDir = flagGetProtoDir(cmd)
noDefaultModule, _ = cmd.Flags().GetBool(flagNoDefaultModule)
skipGit, _ = cmd.Flags().GetBool(flagSkipGit)
minimal, _ = cmd.Flags().GetBool(flagMinimal)
Expand All @@ -120,6 +119,11 @@ func scaffoldChainHandler(cmd *cobra.Command, args []string) error {
}
}

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

cacheStorage, err := newCache(cmd)
if err != nil {
return err
Expand All @@ -132,7 +136,7 @@ func scaffoldChainHandler(cmd *cobra.Command, args []string) error {
appPath,
name,
addressPrefix,
protoDir,
cfg.Build.Proto.Path,
noDefaultModule,
minimal,
isConsumer,
Expand All @@ -152,7 +156,7 @@ func scaffoldChainHandler(cmd *cobra.Command, args []string) error {
return err
}

if err := scaffolder.PostScaffold(cmd.Context(), cacheStorage, appDir, protoDir, goModule, skipProto); err != nil {
if err := scaffolder.PostScaffold(cmd.Context(), cacheStorage, appDir, cfg.Build.Proto.Path, goModule, skipProto); err != nil {
return err
}

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
8 changes: 6 additions & 2 deletions ignite/cmd/scaffold_packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,16 @@ func createPacketHandler(cmd *cobra.Command, args []string) error {
packetFields = args[1:]
signer = flagGetSigner(cmd)
appPath = flagGetPath(cmd)
protoDir = flagGetProtoDir(cmd)
)

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

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

module, _ := cmd.Flags().GetString(flagModule)
if module == "" {
return errors.New("please specify a module to create the packet into: --module <module_name>")
Expand All @@ -67,7 +71,7 @@ func createPacketHandler(cmd *cobra.Command, args []string) error {
options = append(options, scaffolder.PacketWithSigner(signer))
}

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_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,22 @@ func scaffoldParamsHandler(cmd *cobra.Command, args []string) error {
params = 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
Loading
Loading