/
new_service.go
129 lines (109 loc) · 3.39 KB
/
new_service.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package cmd
import (
"bytes"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"text/template"
"github.com/rogeecn/atomctl/templates/service"
"github.com/rogeecn/atomctl/utils"
"github.com/iancoleman/strcase"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
var addService = &ServiceGenerator{}
// serviceCmd represents the service command
var serviceCmd = &cobra.Command{
Use: "service",
Short: "create service in target module path",
Long: `new service file. support chain module moduleA.moduleB.moduleC`,
Example: "atomctl new service [module] [name]",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 2 {
return errors.New("invalid params")
}
// a => modules/a/service
// a.b => modules/a/modules/b/services
file := fmt.Sprintf("modules/%s/service", strings.ReplaceAll(args[0], ".", "/modules/"))
if !utils.IsDir(file) {
return errors.New("module not exists")
}
addService.Path = file
addService.Package = getPackage()
addService.Name = args[1]
addService.PascalName = strcase.ToCamel(addService.Name)
addService.CamelName = strcase.ToLowerCamel(addService.Name)
generateFiles, err := addService.prepareFiles(service.Files)
if err != nil {
return err
}
if err := utils.Generate(generateFiles, service.Templates, addService); err != nil {
return err
}
// register controller provider
providerFile := filepath.Join(addService.Path, "provider.go")
if !utils.IsFile(providerFile) {
log.Println("[Warn] " + providerFile + " not exists, please add new provider manually")
return err
}
providerContent, err := os.ReadFile(providerFile)
if err != nil {
log.Println("[Warn] read " + providerFile + " failed, please add new provider manually")
return err
}
providerFunc := fmt.Sprintf("New%sService", addService.PascalName)
content := string(providerContent)
if !strings.Contains(content, providerFunc) {
provider := fmt.Sprintf("\n\tif err := container.Container.Provide(%s); err!=nil {\n\t\treturn err\n\t}\n\treturn nil", providerFunc)
content = strings.Replace(content, "return nil", provider, 1)
}
containerPackage := `"github.com/rogeecn/atom/container"`
if !strings.Contains(content, containerPackage) {
content = strings.Replace(content, "import (", "import (\n\t"+containerPackage, 1)
}
return os.WriteFile(providerFile, []byte(content), os.ModePerm)
},
}
func init() {
newCmd.AddCommand(serviceCmd)
}
type ServiceGenerator struct {
Name string
Package string
Path string
PascalName string
CamelName string
PkgName string
}
func (m *ServiceGenerator) prepareFiles(files map[string]string) (map[string]string, error) {
result := make(map[string]string)
for tpl, target := range files {
// get target file name
if len(target) == 0 {
if utils.IsTplFile(tpl) {
target = utils.TplToGo(tpl)
} else {
target = tpl
}
} else {
newName := bytes.NewBuffer(nil)
t, err := template.New("name").Parse(target)
if err != nil {
return nil, errors.Wrap(err, "init template failed")
}
if err := t.Execute(newName, m); err != nil {
return nil, errors.Wrapf(err, "generate target file failed, tpl: %s", tpl)
}
target = newName.String()
}
tplFilePath := "tpl/" + tpl
target = filepath.Join(m.Path, target)
result[tplFilePath] = target
if utils.IsFile(target) {
return nil, errors.New(target + " file exists")
}
}
return result, nil
}