Some checks failed
Docs Deploy / build_and_deploy (push) Has been cancelled
Generate Docs / cli (push) Has been cancelled
Generate Config Doc / cli (push) Has been cancelled
Go formatting / go-formatting (push) Has been cancelled
Check links / markdown-link-check (push) Has been cancelled
Integration / pre-test (push) Has been cancelled
Integration / test on (push) Has been cancelled
Integration / status (push) Has been cancelled
Lint / Lint Go code (push) Has been cancelled
Test / test (ubuntu-latest) (push) Has been cancelled
80 lines
2 KiB
Go
80 lines
2 KiB
Go
package scaffolder
|
|
|
|
import (
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"git.cw.tr/mukan-network/mukan-ignite/ignite/pkg/errors"
|
|
"git.cw.tr/mukan-network/mukan-ignite/ignite/pkg/goanalysis"
|
|
"git.cw.tr/mukan-network/mukan-ignite/ignite/pkg/multiformatname"
|
|
"git.cw.tr/mukan-network/mukan-ignite/ignite/templates/field"
|
|
modulecreate "git.cw.tr/mukan-network/mukan-ignite/ignite/templates/module/create"
|
|
)
|
|
|
|
// CreateParams creates a new params in the scaffolded module.
|
|
func (s Scaffolder) CreateParams(
|
|
moduleName string,
|
|
params ...string,
|
|
) error {
|
|
// If no module is provided, we add the type to the app's module
|
|
if moduleName == "" {
|
|
moduleName = s.modpath.Package
|
|
}
|
|
mfName, err := multiformatname.NewName(moduleName, multiformatname.NoNumber)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
moduleName = mfName.LowerCase
|
|
|
|
// Check if the module already exist
|
|
ok, err := moduleExists(s.appPath, moduleName)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !ok {
|
|
return errors.Errorf("the module %v not exist", moduleName)
|
|
}
|
|
|
|
if err := checkParamCreated(s.appPath, moduleName, params); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Parse params with the associated type
|
|
paramsFields, err := field.ParseFields(params, checkForbiddenTypeIndex)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
opts := modulecreate.ParamsOptions{
|
|
ModuleName: moduleName,
|
|
Params: paramsFields,
|
|
AppName: s.modpath.Package,
|
|
ProtoDir: s.protoDir,
|
|
ProtoVer: "v1", // TODO(@julienrbrt): possibly in the future add flag to specify custom proto version.
|
|
}
|
|
|
|
g, err := modulecreate.NewModuleParam(opts)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return s.Run(g)
|
|
}
|
|
|
|
// checkParamCreated checks if the parameter has been already created.
|
|
func checkParamCreated(appPath, moduleName string, params []string) error {
|
|
path := filepath.Join(appPath, "x", moduleName, "types")
|
|
ok, err := goanalysis.HasAnyStructFieldsInPkg(path, "Params", params)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if ok {
|
|
return errors.Errorf(
|
|
"duplicated params (%s) module %s",
|
|
strings.Join(params, " "),
|
|
moduleName,
|
|
)
|
|
}
|
|
return nil
|
|
}
|