forked from zeromicro/go-zero
/
file.go
153 lines (125 loc) · 3.25 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
package util
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/logrusorgru/aurora"
)
// NL defines a new line
const (
NL = "\n"
goctlDir = ".goctl"
)
// 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 home where Join $HOME with .goctl
func GetGoctlHome() (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(home, goctlDir), nil
}
// GetTemplateDir returns the category path value in GoctlHome where could get it by GetGoctlHome
func GetTemplateDir(category string) (string, error) {
goctlHome, err := GetGoctlHome()
if err != nil {
return "", err
}
return filepath.Join(goctlHome, 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 := ioutil.ReadFile(file)
if err != nil {
return "", err
}
return string(content), 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
}