-
Notifications
You must be signed in to change notification settings - Fork 19
/
helper.go
174 lines (142 loc) · 3.5 KB
/
helper.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
package main
import (
"bytes"
"embed"
"errors"
"fmt"
"go/format"
"io"
"io/fs"
"net/http"
"os"
"strings"
"text/template"
"time"
"github.com/GoAdminGroup/go-admin/modules/utils"
"github.com/mgutz/ansi"
)
const version = "v1.2.30"
func cliInfo() {
fmt.Println("GoAdmin CLI " + version + compareVersion(version))
fmt.Println()
}
func checkError(err error) {
if err != nil {
panic(err)
}
}
func getLatestVersion() string {
http.DefaultClient.Timeout = 3 * time.Second
res, err := http.Get("https://goproxy.cn/github.com/!go!admin!group/adm/@v/list")
if err != nil || res.Body == nil {
return ""
}
defer func() {
_ = res.Body.Close()
}()
if res.StatusCode != http.StatusOK {
return ""
}
body, err := io.ReadAll(res.Body)
if err != nil || body == nil {
return ""
}
versionsArr := strings.Split(string(body), "\n")
return versionsArr[len(versionsArr)-1]
}
func compareVersion(srcVersion string) string {
toCompareVersion := getLatestVersion()
if utils.CompareVersion(srcVersion, toCompareVersion) {
return ", the latest version is " + toCompareVersion + " now."
}
return ""
}
func printSuccessInfo(msg string) {
fmt.Println()
fmt.Println()
fmt.Println(ansi.Color(getWord(msg), "green"))
fmt.Println()
fmt.Println()
}
func newError(msg string) error {
return errors.New(getWord(msg))
}
func readFileFromFS(fs *embed.FS, name string) string {
f, err := fs.ReadFile(name)
checkError(err)
return string(f)
}
func mkdirs(dirs []string) {
for _, dir := range dirs {
checkError(os.Mkdir(dir, os.ModePerm))
}
}
func mkEmptyFiles(names []string) {
for _, name := range names {
checkError(os.WriteFile(name, []byte{}, os.ModePerm))
}
}
func parseFile(content string, data interface{}) []byte {
t, err := template.New("project").Funcs(map[string]interface{}{
"title": strings.Title,
}).Parse(content)
checkError(err)
buf := new(bytes.Buffer)
checkError(t.Execute(buf, data))
return buf.Bytes()
}
func parseFileWithFormat(content string, data interface{}) []byte {
c, err := format.Source(parseFile(content, data))
checkError(err)
return c
}
func readFileOfInstallation(fs *embed.FS, name string) string {
return readFileFromFS(fs, "templates/installation/"+name)
}
type WriteFileConfig struct {
Path string
File string
Data interface{}
Perm fs.FileMode
IsFormat bool
Parse bool
}
type WriteFilesConfig []WriteFileConfig
func newWriteFilesConfig() *WriteFilesConfig {
cfg := make(WriteFilesConfig, 0)
return &cfg
}
func (cfgs *WriteFilesConfig) Add(path, file string, data interface{}, perm fs.FileMode) {
*cfgs = append(*cfgs, WriteFileConfig{
Path: path,
File: file,
Perm: perm,
Data: data,
IsFormat: path[len(path)-3:] == ".go",
Parse: true,
})
}
func (cfgs *WriteFilesConfig) AddRaw(path, file string, perm fs.FileMode) {
*cfgs = append(*cfgs, WriteFileConfig{
Path: path,
File: file,
Perm: perm,
Parse: false,
})
}
func writeFileOfInstallation(cfg WriteFileConfig) {
if cfg.IsFormat {
checkError(os.WriteFile(cfg.Path, parseFileWithFormat(readFileOfInstallation(&installationTmplFS, cfg.File), cfg.Data), cfg.Perm))
return
}
checkError(os.WriteFile(cfg.Path, parseFile(readFileOfInstallation(&installationTmplFS, cfg.File), cfg.Data), cfg.Perm))
}
func writeFilesOfInstallation(cfgs WriteFilesConfig) {
for _, cfg := range cfgs {
if !cfg.Parse {
checkError(os.WriteFile(cfg.Path, []byte(readFileOfInstallation(&installationTmplFS, cfg.File)), cfg.Perm))
continue
}
writeFileOfInstallation(cfg)
}
}