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: add scaffold configs and scaffold params commands and start remove placeholders #3770

Merged
merged 62 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
5cd468e
add parameters placeholders and modifier
Nov 22, 2023
6752e7b
add scaffold params command
Nov 23, 2023
2ae4256
Merge branch 'main' into feat/add-params
Pantani Nov 23, 2023
b862349
add changelog
Nov 23, 2023
aef509a
check if the parameter already exist
Nov 24, 2023
58ece42
add integration tests and fix some import issues
Nov 24, 2023
452187c
improve the scaffolder logic
Nov 24, 2023
cb73d70
improve the log message
Nov 24, 2023
600e0e9
fix the log message
Nov 24, 2023
b8dc81a
Merge branch 'main' into feat/add-params
Pantani Nov 25, 2023
0e516fd
Merge branch 'main' into feat/add-params
Pantani Nov 27, 2023
57693a7
Merge branch 'main' into feat/add-params
Pantani Nov 28, 2023
8457c5e
Merge branch 'main' into feat/add-params
Pantani Dec 1, 2023
a62fa47
Merge branch 'main' into feat/add-params
Pantani Dec 1, 2023
81bffd0
Merge branch 'main' into feat/add-params
Pantani Dec 1, 2023
efac026
Merge branch 'main' into feat/add-params
Pantani Dec 1, 2023
0ef6666
Merge branch 'main' into feat/add-params
Pantani Dec 2, 2023
d659e38
Merge branch 'main' into feat/add-params
Pantani Dec 2, 2023
278f547
Merge branch 'main' into feat/add-params
Pantani Dec 4, 2023
3742073
Merge remote-tracking branch 'origin/main' into feat/add-params
Dec 5, 2023
e9a64ff
fix changelog
Dec 5, 2023
d1a79ec
fix 28 imports
Dec 5, 2023
758457b
remove params module
Dec 5, 2023
039cd89
add module configs command
Dec 5, 2023
b30cdce
Merge branch 'main' into feat/add-params
Pantani Dec 5, 2023
a6a3f93
rollbak empty line for proto
Dec 5, 2023
8564b7d
improve code readbility and add unit tests
Dec 6, 2023
a227249
Merge branch 'main' into feat/add-params
Pantani Dec 6, 2023
bdc1234
Merge branch 'main' into feat/add-params
Pantani Dec 10, 2023
94d4fca
Merge branch 'main' into feat/add-params
Pantani Dec 12, 2023
e07fa93
Merge remote-tracking branch 'origin/main' into feat/add-params
Dec 12, 2023
ba0a50e
fix changelog
Dec 12, 2023
594db1f
Merge branch 'main' into feat/add-params
Pantani Dec 15, 2023
ad4b093
Merge branch 'main' into feat/add-params
Pantani Dec 21, 2023
0fb1ce8
Merge branch 'main' into feat/add-params
Pantani Dec 21, 2023
bfbb581
Merge branch 'main' into feat/add-params
Pantani Dec 21, 2023
cef9405
use cli error package
Dec 21, 2023
c2dfbb3
Merge branch 'main' into feat/add-params
Pantani Dec 23, 2023
839867a
Update ignite/cmd/scaffold_configs.go
Pantani Jan 15, 2024
4cfc2af
Merge remote-tracking branch 'origin/main' into feat/add-params
Jan 16, 2024
557eed3
apply pr suggestions
Jan 16, 2024
52f7239
fix some typos
Jan 16, 2024
d650546
remove unused vars and casting
Jan 16, 2024
f8d1ab9
Merge remote-tracking branch 'origin/main' into feat/add-params
Feb 15, 2024
d9030ec
add replacer pkg for goanalysis
Feb 17, 2024
272986e
add TODO comment
Feb 19, 2024
1c0e63a
Merge remote-tracking branch 'origin/main' into feat/add-params
Feb 19, 2024
8c8499f
add modify call function
Feb 19, 2024
c14f9c5
test readbility
Feb 19, 2024
62864db
improve organization
Feb 19, 2024
951de5e
fix the function modifier logic
Feb 20, 2024
35c8487
fix the arg idente for caller replace
Feb 20, 2024
98064b1
add more test cases and last fixes
Feb 20, 2024
e0d5796
start replace the params template to the xast
Feb 20, 2024
f56bdf5
Merge remote-tracking branch 'origin/main' into feat/add-params
Feb 20, 2024
6b10809
add append struct param option
Feb 20, 2024
f229ede
add support to add new struct params
Feb 20, 2024
75cf0f8
fix some lint issues
Feb 20, 2024
e797634
add small fixes and improve code readbility
Feb 20, 2024
df6f165
fix append code parser
Feb 20, 2024
5798289
fix changelog
Feb 20, 2024
ffade6c
Merge branch 'main' into feat/add-params
Pantani Feb 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Features

- [#3977](https://github.com/ignite/cli/pull/3977) Add `chain lint` command to lint the chain's codebase using `golangci-lint`
- [#3770](https://github.com/ignite/cli/pull/3770) Add `scaffold configs` and `scaffold params` commands

### Changes

Expand Down
3 changes: 2 additions & 1 deletion ignite/cmd/chain_lint.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package ignitecmd

import (
"github.com/spf13/cobra"

"github.com/ignite/cli/v28/ignite/pkg/cliui"
"github.com/ignite/cli/v28/ignite/services/chain"
"github.com/spf13/cobra"
)

// NewChainLint returns a lint command to build a blockchain app.
Expand Down
2 changes: 2 additions & 0 deletions ignite/cmd/scaffold.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@
NewScaffoldMap(),
NewScaffoldSingle(),
NewScaffoldType(),
NewScaffoldParams(),
NewScaffoldConfigs(),

Check warning on line 127 in ignite/cmd/scaffold.go

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/scaffold.go#L126-L127

Added lines #L126 - L127 were not covered by tests
NewScaffoldMessage(),
NewScaffoldQuery(),
NewScaffoldPacket(),
Expand Down
11 changes: 9 additions & 2 deletions ignite/cmd/scaffold_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
c.Flags().StringP(flagPath, "p", "", "create a project in a specific path")
c.Flags().Bool(flagNoDefaultModule, false, "create a project without a default module")
c.Flags().StringSlice(flagParams, []string{}, "add default module parameters")
c.Flags().StringSlice(flagModuleConfigs, []string{}, "add module configs")

Check warning on line 83 in ignite/cmd/scaffold_chain.go

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/scaffold_chain.go#L83

Added line #L83 was not covered by tests
c.Flags().Bool(flagSkipGit, false, "skip Git repository initialization")
c.Flags().Bool(flagMinimal, false, "create a minimal blockchain (with the minimum required Cosmos SDK modules)")

Expand All @@ -98,10 +99,15 @@
skipGit, _ = cmd.Flags().GetBool(flagSkipGit)
minimal, _ = cmd.Flags().GetBool(flagMinimal)
params, _ = cmd.Flags().GetStringSlice(flagParams)
moduleConfigs, _ = cmd.Flags().GetStringSlice(flagModuleConfigs)

Check warning on line 102 in ignite/cmd/scaffold_chain.go

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/scaffold_chain.go#L102

Added line #L102 was not covered by tests
)

if noDefaultModule && len(params) > 0 {
return errors.New("params flag is only supported if the default module is enabled")
if noDefaultModule {
if len(params) > 0 {
return errors.New("params flag is only supported if the default module is enabled")
} else if len(moduleConfigs) > 0 {
return errors.New("module configs flag is only supported if the default module is enabled")
}

Check warning on line 110 in ignite/cmd/scaffold_chain.go

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/scaffold_chain.go#L105-L110

Added lines #L105 - L110 were not covered by tests
}

cacheStorage, err := newCache(cmd)
Expand All @@ -120,6 +126,7 @@
skipGit,
minimal,
params,
moduleConfigs,

Check warning on line 129 in ignite/cmd/scaffold_chain.go

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/scaffold_chain.go#L129

Added line #L129 was not covered by tests
)
if err != nil {
return err
Expand Down
79 changes: 79 additions & 0 deletions ignite/cmd/scaffold_configs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package ignitecmd

import (
"strings"

"github.com/spf13/cobra"

"github.com/ignite/cli/v28/ignite/pkg/cliui"
"github.com/ignite/cli/v28/ignite/pkg/placeholder"
"github.com/ignite/cli/v28/ignite/services/scaffolder"
)

// NewScaffoldConfigs returns the command to scaffold a Cosmos SDK configs into a module.
func NewScaffoldConfigs() *cobra.Command {
c := &cobra.Command{
Use: "configs [configs]...",
Short: "Configs for a custom Cosmos SDK module",
Long: `Scaffold a new config for a Cosmos SDK module.

A Cosmos SDK module can have configurations. An example of a config is "address prefix" of the
"auth" module. A config can be scaffolded into a module using the "--module-configs" into
the scaffold module command or using the "scaffold configs" command. By default
configs are of type "string", but you can specify a type for each config. For example:

ignite scaffold configs foo baz:uint bar:bool

Refer to Cosmos SDK documentation to learn more about modules, dependencies and
configs.
`,
Args: cobra.MinimumNArgs(1),
PreRunE: migrationPreRunHandler,
RunE: scaffoldConfigsHandler,
}

flagSetPath(c)
flagSetClearCache(c)

c.Flags().AddFlagSet(flagSetYes())

c.Flags().String(flagModule, "", "module to add the query into (default: app's main module)")

return c

Check warning on line 42 in ignite/cmd/scaffold_configs.go

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/scaffold_configs.go#L14-L42

Added lines #L14 - L42 were not covered by tests
}

func scaffoldConfigsHandler(cmd *cobra.Command, args []string) error {
var (
configs = args[0:]
appPath = flagGetPath(cmd)
moduleName = flagGetModule(cmd)
)

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

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

Check warning on line 58 in ignite/cmd/scaffold_configs.go

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/scaffold_configs.go#L45-L58

Added lines #L45 - L58 were not covered by tests

sc, err := scaffolder.New(appPath)
if err != nil {
return err
}

Check warning on line 63 in ignite/cmd/scaffold_configs.go

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/scaffold_configs.go#L60-L63

Added lines #L60 - L63 were not covered by tests

sm, err := sc.CreateConfigs(cmd.Context(), cacheStorage, placeholder.New(), moduleName, configs...)
if err != nil {
return err
}

Check warning on line 68 in ignite/cmd/scaffold_configs.go

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/scaffold_configs.go#L65-L68

Added lines #L65 - L68 were not covered by tests

modificationsStr, err := sourceModificationToString(sm)
if err != nil {
return err
}

Check warning on line 73 in ignite/cmd/scaffold_configs.go

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/scaffold_configs.go#L70-L73

Added lines #L70 - L73 were not covered by tests

session.Println(modificationsStr)
session.Printf("\n🎉 New configs added to the module:\n\n- %s\n\n", strings.Join(configs, "\n- "))

return nil

Check warning on line 78 in ignite/cmd/scaffold_configs.go

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/scaffold_configs.go#L75-L78

Added lines #L75 - L78 were not covered by tests
}
8 changes: 8 additions & 0 deletions ignite/cmd/scaffold_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
flagDep = "dep"
flagIBC = "ibc"
flagParams = "params"
flagModuleConfigs = "module-configs"
flagIBCOrdering = "ordering"
flagRequireRegistration = "require-registration"

Expand Down Expand Up @@ -115,6 +116,7 @@
c.Flags().String(flagIBCOrdering, "none", "channel ordering of the IBC module [none|ordered|unordered]")
c.Flags().Bool(flagRequireRegistration, false, "fail if module can't be registered")
c.Flags().StringSlice(flagParams, []string{}, "add module parameters")
c.Flags().StringSlice(flagModuleConfigs, []string{}, "add module configs")

Check warning on line 119 in ignite/cmd/scaffold_module.go

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/scaffold_module.go#L119

Added line #L119 was not covered by tests

return c
}
Expand All @@ -133,13 +135,19 @@
requireRegistration, _ := cmd.Flags().GetBool(flagRequireRegistration)
params, _ := cmd.Flags().GetStringSlice(flagParams)

moduleConfigs, err := cmd.Flags().GetStringSlice(flagModuleConfigs)
if err != nil {
return err
}

Check warning on line 141 in ignite/cmd/scaffold_module.go

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/scaffold_module.go#L138-L141

Added lines #L138 - L141 were not covered by tests

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

options := []scaffolder.ModuleCreationOption{
scaffolder.WithParams(params),
scaffolder.WithModuleConfigs(moduleConfigs),

Check warning on line 150 in ignite/cmd/scaffold_module.go

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/scaffold_module.go#L150

Added line #L150 was not covered by tests
}

// Check if the module must be an IBC module
Expand Down
81 changes: 81 additions & 0 deletions ignite/cmd/scaffold_params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package ignitecmd

import (
"strings"

"github.com/spf13/cobra"

"github.com/ignite/cli/v28/ignite/pkg/cliui"
"github.com/ignite/cli/v28/ignite/pkg/placeholder"
"github.com/ignite/cli/v28/ignite/services/scaffolder"
)

// NewScaffoldParams returns the command to scaffold a Cosmos SDK parameters into a module.
func NewScaffoldParams() *cobra.Command {
c := &cobra.Command{
Use: "params [param]...",
Short: "Parameters for a custom Cosmos SDK module",
Long: `Scaffold a new parameter for a Cosmos SDK module.

A Cosmos SDK module can have parameters (or "params"). Params are values that
can be set at the genesis of the blockchain and can be modified while the
blockchain is running. An example of a param is "Inflation rate change" of the
"mint" module. A params can be scaffolded into a module using the "--params" into
the scaffold module command or using the "scaffold params" command. By default
params are of type "string", but you can specify a type for each param. For example:

ignite scaffold params foo baz:uint bar:bool

Refer to Cosmos SDK documentation to learn more about modules, dependencies and
params.
`,
Args: cobra.MinimumNArgs(1),
PreRunE: migrationPreRunHandler,
RunE: scaffoldParamsHandler,
}

flagSetPath(c)
flagSetClearCache(c)

c.Flags().AddFlagSet(flagSetYes())

c.Flags().String(flagModule, "", "module to add the query into. Default: app's main module")
Pantani marked this conversation as resolved.
Show resolved Hide resolved

return c

Check warning on line 44 in ignite/cmd/scaffold_params.go

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/scaffold_params.go#L14-L44

Added lines #L14 - L44 were not covered by tests
}

func scaffoldParamsHandler(cmd *cobra.Command, args []string) error {
var (
params = args[0:]
appPath = flagGetPath(cmd)
moduleName = flagGetModule(cmd)
)

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

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

Check warning on line 60 in ignite/cmd/scaffold_params.go

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/scaffold_params.go#L47-L60

Added lines #L47 - L60 were not covered by tests

sc, err := scaffolder.New(appPath)
if err != nil {
return err
}

Check warning on line 65 in ignite/cmd/scaffold_params.go

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/scaffold_params.go#L62-L65

Added lines #L62 - L65 were not covered by tests

sm, err := sc.CreateParams(cmd.Context(), cacheStorage, placeholder.New(), moduleName, params...)
if err != nil {
return err
}

Check warning on line 70 in ignite/cmd/scaffold_params.go

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/scaffold_params.go#L67-L70

Added lines #L67 - L70 were not covered by tests

modificationsStr, err := sourceModificationToString(sm)
if err != nil {
return err
}

Check warning on line 75 in ignite/cmd/scaffold_params.go

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/scaffold_params.go#L72-L75

Added lines #L72 - L75 were not covered by tests

session.Println(modificationsStr)
session.Printf("\n🎉 New parameters added to the module:\n\n- %s\n\n", strings.Join(params, "\n- "))

return nil

Check warning on line 80 in ignite/cmd/scaffold_params.go

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/scaffold_params.go#L77-L80

Added lines #L77 - L80 were not covered by tests
}
56 changes: 56 additions & 0 deletions ignite/pkg/goanalysis/goanalysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,3 +338,59 @@
}
return nil
}

// HasAnyStructFieldsInPkg finds the struct within a package folder and checks
// if any of the fields are defined in the struct.
func HasAnyStructFieldsInPkg(pkgPath, structName string, fields []string) (bool, error) {
absPath, err := filepath.Abs(pkgPath)
if err != nil {
return false, err
}

Check warning on line 348 in ignite/pkg/goanalysis/goanalysis.go

View check run for this annotation

Codecov / codecov/patch

ignite/pkg/goanalysis/goanalysis.go#L347-L348

Added lines #L347 - L348 were not covered by tests
fileSet := token.NewFileSet()
all, err := parser.ParseDir(fileSet, absPath, nil, parser.ParseComments)
if err != nil {
return false, err
}

fieldsNames := make(map[string]struct{})
for _, field := range fields {
fieldsNames[strings.ToLower(field)] = struct{}{}
}

exist := false
for _, pkg := range all {
for _, f := range pkg.Files {
ast.Inspect(f, func(x ast.Node) bool {
typeSpec, ok := x.(*ast.TypeSpec)
if !ok {
return true
}

if _, ok := typeSpec.Type.(*ast.StructType); !ok ||
typeSpec.Name.Name != structName ||
typeSpec.Type == nil {
return true
}

// Check if the struct has fields.
structType, ok := typeSpec.Type.(*ast.StructType)
if !ok {
return true
}

Check warning on line 379 in ignite/pkg/goanalysis/goanalysis.go

View check run for this annotation

Codecov / codecov/patch

ignite/pkg/goanalysis/goanalysis.go#L378-L379

Added lines #L378 - L379 were not covered by tests

// Iterate through the fields of the struct.
for _, field := range structType.Fields.List {
for _, fieldName := range field.Names {
if _, ok := fieldsNames[strings.ToLower(fieldName.Name)]; !ok {
continue
}
exist = true
return false
}
}
return true
})
}
}
return exist, nil
}
Loading
Loading