-
Notifications
You must be signed in to change notification settings - Fork 49
/
remove.go
177 lines (150 loc) · 4.26 KB
/
remove.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package actions
import (
"context"
"fmt"
"os"
"path/filepath"
"github.com/gin-admin/gin-admin-cli/v10/internal/parser"
"github.com/gin-admin/gin-admin-cli/v10/internal/schema"
"github.com/gin-admin/gin-admin-cli/v10/internal/utils"
"go.uber.org/zap"
)
type RemoveConfig struct {
Dir string
ModuleName string
ModulePath string
WirePath string
SwaggerPath string
}
func Remove(cfg RemoveConfig) *RemoveAction {
return &RemoveAction{
logger: zap.S().Named("[REMOVE]"),
cfg: &cfg,
}
}
type RemoveAction struct {
logger *zap.SugaredLogger
cfg *RemoveConfig
}
func (a *RemoveAction) RunWithConfig(ctx context.Context, configFile string) error {
var data []*schema.S
switch filepath.Ext(configFile) {
case ".json":
if err := utils.ParseJSONFile(configFile, &data); err != nil {
return err
}
case ".yaml", "yml":
if err := utils.ParseYAMLFile(configFile, &data); err != nil {
return err
}
default:
return fmt.Errorf("unsupported config file type: %s", configFile)
}
var structs []string
for _, item := range data {
structs = append(structs, item.Name)
}
a.logger.Infof("Remove structs: %v", structs)
return a.Run(ctx, structs)
}
func (a *RemoveAction) Run(ctx context.Context, structs []string) error {
for _, name := range structs {
for _, pkgName := range parser.StructPackages {
err := a.modify(ctx, a.cfg.ModuleName, name, parser.StructPackageTplPaths[pkgName], nil, true)
if err != nil {
return err
}
}
basicArgs := parser.BasicArgs{
Dir: a.cfg.Dir,
ModuleName: a.cfg.ModuleName,
ModulePath: a.cfg.ModulePath,
StructName: name,
Flag: parser.AstFlagRem,
}
moduleMainTplData, err := parser.ModifyModuleMainFile(ctx, basicArgs)
if err != nil {
a.logger.Errorf("Failed to modify module main file, err: %s, #struct %s", err, name)
return err
}
err = a.modify(ctx, a.cfg.ModuleName, name, parser.FileForModuleMain, moduleMainTplData, false)
if err != nil {
return err
}
moduleWireTplData, err := parser.ModifyModuleWireFile(ctx, basicArgs)
if err != nil {
a.logger.Errorf("Failed to modify module wire file, err: %s, #struct %s", err, name)
return err
}
err = a.modify(ctx, a.cfg.ModuleName, name, parser.FileForModuleWire, moduleWireTplData, false)
if err != nil {
return err
}
}
return a.execWireAndSwag(ctx)
}
func (a RemoveAction) getAbsPath(file string) (string, error) {
modPath := a.cfg.ModulePath
file = filepath.Join(a.cfg.Dir, modPath, file)
fullpath, err := filepath.Abs(file)
if err != nil {
a.logger.Errorf("Failed to get abs path, err: %s, #file %s", err, file)
return "", err
}
return fullpath, nil
}
func (a *RemoveAction) modify(ctx context.Context, moduleName, structName, tpl string, data []byte, deleted bool) error {
file, err := parser.ParseFilePathFromTpl(moduleName, structName, tpl)
if err != nil {
a.logger.Errorf("Failed to parse file path from tpl, err: %s, #struct %s, #tpl %s", err, structName, tpl)
return err
}
file, err = a.getAbsPath(file)
if err != nil {
return err
}
exists, err := utils.ExistsFile(file)
if err != nil {
return err
}
if exists {
if err := os.Remove(file); err != nil {
a.logger.Errorf("Failed to remove file, err: %s, #file %s", err, file)
return err
}
}
if deleted {
a.logger.Infof("Delete file: %s", file)
return nil
}
if !exists {
return nil
}
a.logger.Infof("Write file: %s", file)
if err := utils.WriteFile(file, data); err != nil {
a.logger.Errorf("Failed to write file, err: %s, #file %s", err, file)
return err
}
if err := utils.ExecGoFormat(file); err != nil {
a.logger.Errorf("Failed to exec go format, err: %s, #file %s", err, file)
return nil
}
if err := utils.ExecGoImports(a.cfg.Dir, file); err != nil {
a.logger.Errorf("Failed to exec go imports, err: %s, #file %s", err, file)
return nil
}
return nil
}
func (a *RemoveAction) execWireAndSwag(ctx context.Context) error {
if p := a.cfg.WirePath; p != "" {
if err := utils.ExecWireGen(a.cfg.Dir, p); err != nil {
a.logger.Errorf("Failed to exec wire, err: %s, #wirePath %s", err, p)
}
}
if p := a.cfg.SwaggerPath; p != "" {
if err := utils.ExecSwagGen(a.cfg.Dir, "main.go", p); err != nil {
a.logger.Errorf("Failed to exec swag, err: %s, #swaggerPath %s", err, p)
}
}
return nil
}