forked from zeromicro/go-zero
/
file.go
303 lines (257 loc) · 6.39 KB
/
file.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package pathx
import (
"bufio"
"crypto/md5"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"github.com/logrusorgru/aurora"
"github.com/zeromicro/go-zero/tools/goctl/internal/version"
)
// NL defines a new line.
const (
NL = "\n"
goctlDir = ".goctl"
gitDir = ".git"
autoCompleteDir = ".auto_complete"
cacheDir = "cache"
)
var goctlHome string
// RegisterGoctlHome register goctl home path.
func RegisterGoctlHome(home string) {
goctlHome = home
}
// CreateIfNotExist creates a file if it is not exists.
func CreateIfNotExist(file string) (*os.File, error) {
_, err := os.Stat(file)
if !os.IsNotExist(err) {
return nil, fmt.Errorf("%s already exist", file)
}
return os.Create(file)
}
// RemoveIfExist deletes the specified file if it is exists.
func RemoveIfExist(filename string) error {
if !FileExists(filename) {
return nil
}
return os.Remove(filename)
}
// RemoveOrQuit deletes the specified file if read a permit command from stdin.
func RemoveOrQuit(filename string) error {
if !FileExists(filename) {
return nil
}
fmt.Printf("%s exists, overwrite it?\nEnter to overwrite or Ctrl-C to cancel...",
aurora.BgRed(aurora.Bold(filename)))
bufio.NewReader(os.Stdin).ReadBytes('\n')
return os.Remove(filename)
}
// FileExists returns true if the specified file is exists.
func FileExists(file string) bool {
_, err := os.Stat(file)
return err == nil
}
// FileNameWithoutExt returns a file name without suffix.
func FileNameWithoutExt(file string) string {
return strings.TrimSuffix(file, filepath.Ext(file))
}
// GetGoctlHome returns the path value of the goctl, the default path is ~/.goctl, if the path has
// been set by calling the RegisterGoctlHome method, the user-defined path refers to.
func GetGoctlHome() (home string, err error) {
defer func() {
if err != nil {
return
}
info, err := os.Stat(home)
if err == nil && !info.IsDir() {
os.Rename(home, home+".old")
MkdirIfNotExist(home)
}
}()
if len(goctlHome) != 0 {
home = goctlHome
return
}
home, err = GetDefaultGoctlHome()
return
}
// GetDefaultGoctlHome returns the path value of the goctl home where Join $HOME with .goctl.
func GetDefaultGoctlHome() (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(home, goctlDir), nil
}
// GetGitHome returns the git home of goctl.
func GetGitHome() (string, error) {
goctlH, err := GetGoctlHome()
if err != nil {
return "", err
}
return filepath.Join(goctlH, gitDir), nil
}
// GetAutoCompleteHome returns the auto_complete home of goctl.
func GetAutoCompleteHome() (string, error) {
goctlH, err := GetGoctlHome()
if err != nil {
return "", err
}
return filepath.Join(goctlH, autoCompleteDir), nil
}
// GetCacheDir returns the cache dit of goctl.
func GetCacheDir() (string, error) {
goctlH, err := GetGoctlHome()
if err != nil {
return "", err
}
return filepath.Join(goctlH, cacheDir), nil
}
// GetTemplateDir returns the category path value in GoctlHome where could get it by GetGoctlHome.
func GetTemplateDir(category string) (string, error) {
home, err := GetGoctlHome()
if err != nil {
return "", err
}
if home == goctlHome {
// backward compatible, it will be removed in the feature
// backward compatible start.
beforeTemplateDir := filepath.Join(home, version.GetGoctlVersion(), category)
fs, _ := ioutil.ReadDir(beforeTemplateDir)
var hasContent bool
for _, e := range fs {
if e.Size() > 0 {
hasContent = true
}
}
if hasContent {
return beforeTemplateDir, nil
}
// backward compatible end.
return filepath.Join(home, category), nil
}
return filepath.Join(home, version.GetGoctlVersion(), category), nil
}
// InitTemplates creates template files GoctlHome where could get it by GetGoctlHome.
func InitTemplates(category string, templates map[string]string) error {
dir, err := GetTemplateDir(category)
if err != nil {
return err
}
if err := MkdirIfNotExist(dir); err != nil {
return err
}
for k, v := range templates {
if err := createTemplate(filepath.Join(dir, k), v, false); err != nil {
return err
}
}
return nil
}
// CreateTemplate writes template into file even it is exists.
func CreateTemplate(category, name, content string) error {
dir, err := GetTemplateDir(category)
if err != nil {
return err
}
return createTemplate(filepath.Join(dir, name), content, true)
}
// Clean deletes all templates and removes the parent directory.
func Clean(category string) error {
dir, err := GetTemplateDir(category)
if err != nil {
return err
}
return os.RemoveAll(dir)
}
// LoadTemplate gets template content by the specified file.
func LoadTemplate(category, file, builtin string) (string, error) {
dir, err := GetTemplateDir(category)
if err != nil {
return "", err
}
file = filepath.Join(dir, file)
if !FileExists(file) {
return builtin, nil
}
content, err := os.ReadFile(file)
if err != nil {
return "", err
}
return string(content), nil
}
// SameFile compares the between path if the same path,
// it maybe the same path in case case-ignore, such as:
// /Users/go_zero and /Users/Go_zero, as far as we know,
// this case maybe appear on macOS and Windows.
func SameFile(path1, path2 string) (bool, error) {
stat1, err := os.Stat(path1)
if err != nil {
return false, err
}
stat2, err := os.Stat(path2)
if err != nil {
return false, err
}
return os.SameFile(stat1, stat2), nil
}
func createTemplate(file, content string, force bool) error {
if FileExists(file) && !force {
return nil
}
f, err := os.Create(file)
if err != nil {
return err
}
defer f.Close()
_, err = f.WriteString(content)
return err
}
// MustTempDir creates a temporary directory.
func MustTempDir() string {
dir, err := ioutil.TempDir("", "")
if err != nil {
log.Fatalln(err)
}
return dir
}
func Copy(src, dest string) error {
f, err := os.Open(src)
if err != nil {
return err
}
defer f.Close()
dir := filepath.Dir(dest)
err = MkdirIfNotExist(dir)
if err != nil {
return err
}
w, err := os.Create(dest)
if err != nil {
return err
}
w.Chmod(os.ModePerm)
defer w.Close()
_, err = io.Copy(w, f)
return err
}
func Hash(file string) (string, error) {
f, err := os.Open(file)
if err != nil {
return "", err
}
defer func() {
_ = f.Close()
}()
hash := md5.New()
_, err = io.Copy(hash, f)
if err != nil {
return "", err
}
return hex.EncodeToString(hash.Sum(nil)), nil
}