-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen.go
170 lines (147 loc) · 3.84 KB
/
gen.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
package gogen
import (
"errors"
"fmt"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
"github.com/gookit/color"
"github.com/spf13/cobra"
"github.com/wuntsong-org/go-zero-plus/core/logx"
apiformat "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/api/format"
"github.com/wuntsong-org/go-zero-plus/tools/goctlwt/api/parser"
apiutil "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/api/util"
"github.com/wuntsong-org/go-zero-plus/tools/goctlwt/config"
"github.com/wuntsong-org/go-zero-plus/tools/goctlwt/pkg/golang"
"github.com/wuntsong-org/go-zero-plus/tools/goctlwt/util"
"github.com/wuntsong-org/go-zero-plus/tools/goctlwt/util/pathx"
)
const tmpFile = "%s-%d"
var (
tmpDir = path.Join(os.TempDir(), "goctlwt")
// VarStringDir describes the directory.
VarStringDir string
// VarStringAPI describes the API.
VarStringAPI string
// VarStringHome describes the go home.
VarStringHome string
// VarStringRemote describes the remote git repository.
VarStringRemote string
// VarStringBranch describes the branch.
VarStringBranch string
// VarStringStyle describes the style of output files.
VarStringStyle string
)
// GoCommand gen go project files from command line
func GoCommand(_ *cobra.Command, _ []string) error {
apiFile := VarStringAPI
dir := VarStringDir
namingStyle := VarStringStyle
home := VarStringHome
remote := VarStringRemote
branch := VarStringBranch
if len(remote) > 0 {
repo, _ := util.CloneIntoGitHome(remote, branch)
if len(repo) > 0 {
home = repo
}
}
if len(home) > 0 {
pathx.RegisterGoctlHome(home)
}
if len(apiFile) == 0 {
return errors.New("missing -api")
}
if len(dir) == 0 {
return errors.New("missing -dir")
}
return DoGenProject(apiFile, dir, namingStyle)
}
// DoGenProject gen go project files with api file
func DoGenProject(apiFile, dir, style string) error {
api, err := parser.Parse(apiFile)
if err != nil {
return err
}
if err := api.Validate(); err != nil {
return err
}
cfg, err := config.NewConfig(style)
if err != nil {
return err
}
logx.Must(pathx.MkdirIfNotExist(dir))
rootPkg, err := golang.GetParentPackage(dir)
if err != nil {
return err
}
logx.Must(genEtc(dir, cfg, api))
logx.Must(genConfig(dir, cfg, api))
logx.Must(genMain(dir, rootPkg, cfg, api))
logx.Must(genServiceContext(dir, rootPkg, cfg, api))
logx.Must(genTypes(dir, cfg, api))
logx.Must(genRoutes(dir, rootPkg, cfg, api))
logx.Must(genHandlers(dir, rootPkg, cfg, api))
logx.Must(genLogic(dir, rootPkg, cfg, api))
logx.Must(genMiddleware(dir, cfg, api))
if err := backupAndSweep(apiFile); err != nil {
return err
}
if err := apiformat.ApiFormatByPath(apiFile, false); err != nil {
return err
}
fmt.Println(color.Green.Render("Done."))
return nil
}
func backupAndSweep(apiFile string) error {
var err error
var wg sync.WaitGroup
wg.Add(2)
_ = os.MkdirAll(tmpDir, os.ModePerm)
go func() {
_, fileName := filepath.Split(apiFile)
_, e := apiutil.Copy(apiFile, fmt.Sprintf(path.Join(tmpDir, tmpFile), fileName, time.Now().Unix()))
if e != nil {
err = e
}
wg.Done()
}()
go func() {
if e := sweep(); e != nil {
err = e
}
wg.Done()
}()
wg.Wait()
return err
}
func sweep() error {
keepTime := time.Now().AddDate(0, 0, -7)
return filepath.Walk(tmpDir, func(fpath string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
pos := strings.LastIndexByte(info.Name(), '-')
if pos > 0 {
timestamp := info.Name()[pos+1:]
seconds, err := strconv.ParseInt(timestamp, 10, 64)
if err != nil {
// print error and ignore
fmt.Println(color.Red.Sprintf("sweep ignored file: %s", fpath))
return nil
}
tm := time.Unix(seconds, 0)
if tm.Before(keepTime) {
if err := os.RemoveAll(fpath); err != nil {
fmt.Println(color.Red.Sprintf("failed to remove file: %s", fpath))
return err
}
}
}
return nil
})
}