-
Notifications
You must be signed in to change notification settings - Fork 532
/
configs.go
82 lines (71 loc) · 2.05 KB
/
configs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package scaffolder
import (
"path/filepath"
"strings"
"github.com/ignite/cli/v29/ignite/pkg/errors"
"github.com/ignite/cli/v29/ignite/pkg/goanalysis"
"github.com/ignite/cli/v29/ignite/pkg/multiformatname"
"github.com/ignite/cli/v29/ignite/templates/field"
modulecreate "github.com/ignite/cli/v29/ignite/templates/module/create"
)
// CreateConfigs creates a new configs in the scaffolded module.
func (s Scaffolder) CreateConfigs(
moduleName string,
configs ...string,
) error {
appName := s.modpath.Package
// 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 := checkConfigCreated(s.appPath, appName, moduleName, configs); err != nil {
return err
}
// Parse config with the associated type
configsFields, err := field.ParseFields(configs, checkForbiddenTypeIndex)
if err != nil {
return err
}
opts := modulecreate.ConfigsOptions{
ModuleName: moduleName,
Configs: configsFields,
AppName: s.modpath.Package,
AppPath: s.appPath,
ProtoDir: s.protoDir,
ProtoVer: "v1", // TODO(@julienrbrt): possibly in the future add flag to specify custom proto version.
}
g, err := modulecreate.NewModuleConfigs(opts)
if err != nil {
return err
}
return s.Run(g)
}
// checkConfigCreated checks if the config has been already created.
func checkConfigCreated(appPath, appName, moduleName string, configs []string) (err error) {
path := filepath.Join(appPath, "api", appName, moduleName, "module")
ok, err := goanalysis.HasAnyStructFieldsInPkg(path, "Module", configs)
if err != nil {
return err
}
if ok {
return errors.Errorf(
"duplicated configs (%s) module %s",
strings.Join(configs, " "),
moduleName,
)
}
return nil
}