-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
197 lines (180 loc) · 5.55 KB
/
main.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
//go:generate goversioninfo -icon=ico/icon.ico -manifest=main.exe.manifest -arm=true
package main
import (
"encoding/json"
"io"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"
"gopkg.in/yaml.v2"
)
var (
osName string
osExecFile [4]string = [4]string{} // 0: dir, 1: name, 2: ext, 3: realDir
noChLog []BackupItem = []BackupItem{}
totalIO uint = 0
totalCMD uint = 0
totalEXE uint = 0
totalReplace uint = 0
customVariables map[string]string = map[string]string{}
)
const backupExtension = "StaticDeploymentBackup"
func main() {
var startTime time.Time = time.Now()
var solutions []Solution
// var projects []Project
osName = runtime.GOOS
getPaths()
if len(os.Args) >= 2 && os.Args[1] == "-r" {
rollback()
return
}
log.Println("StaticDeployment v1.0.0 for", osName)
log.Println("https://github.com/MasaeProject/StaticDeployment")
var configPath = osExecFile[1] + ".yaml"
if len(os.Args) > 1 {
configPath = os.Args[1]
} else if !Exists(configPath) {
log.Println("错误: 必须指定一个配置文件。")
os.Exit(1)
return
}
var noRollback bool = false
if len(os.Args) > 2 && os.Args[2] == "-nr" {
noRollback = true
}
if !Exists(configPath) {
log.Println("错误: 配置文件路径不正确", configPath)
os.Exit(1)
return
}
file, err := os.Open(configPath)
if err != nil {
log.Println("错误: 打开配置文件", configPath, "失败:", err)
os.Exit(1)
return
}
defer file.Close()
// buf := make([]byte, 512)
// _, err = file.Read(buf)
// if err != nil {
// log.Println("错误: 读取配置文件失败:", err)
// return
// }
var fileType byte = fileType(file)
if fileType == 'j' {
var decoder *json.Decoder = json.NewDecoder(file)
err = decoder.Decode(&solutions)
if err != nil {
log.Println("错误: JSON 配置文件解析失败:", err)
os.Exit(1)
return
}
} else if fileType == 'y' {
content, err := io.ReadAll(file)
if err != nil {
log.Println("错误: YAML 配置文件读取失败:", err)
os.Exit(1)
return
}
yaml.Unmarshal(content, &solutions)
} else {
log.Printf("错误: 未知的配置文件类型。")
os.Exit(1)
return
}
var solutionLen = len(solutions)
if solutionLen == 0 {
log.Printf("错误: 配置文件格式不正确。")
os.Exit(1)
return
}
for i, solution := range solutions {
log.Printf("开始处理: 解决方案 %d / %d : %s\n", i+1, solutionLen, solution.Name)
var sTime time.Time = time.Now()
if runSolution(solution) {
log.Printf("解决方案 %d / %d : %s 处理完毕,用时 %.2f 秒。\n", i+1, solutionLen, solution.Name, time.Since(sTime).Seconds())
} else {
log.Printf("解决方案 %d / %d : %s 处理失败!用时 %.2f 秒。\n", i+1, solutionLen, solution.Name, time.Since(sTime).Seconds())
if !noRollback {
log.Println("自动进入恢复模式还原文件。")
var cmdPath string = osExecFile[3] + string(filepath.Separator) + osExecFile[1] + osExecFile[2]
var ex *exec.Cmd = exec.Command(cmdPath, "-r")
ex.Stdout = os.Stdout
ex.Stderr = os.Stderr
err = ex.Run()
if err != nil {
log.Println("错误: 未能进入恢复模式:", err)
os.Exit(1)
}
}
log.Println("任务失败。")
os.Exit(1)
return
}
}
if len(backupCache) > 0 {
log.Println("警告: 不是所有备份文件都得到还原,请检查配置文件中备份和还原命令是否成对出现!未还原文件:")
for i, bakInfo := range backupCache {
var jobName Names = bakInfo.JobName
log.Printf("%d 解决方案: %s 项目: %s 作业: %s 文件: %s\n", i+1, jobName.Solution, jobName.Project, jobName.Replace, bakInfo.SourceFile)
}
}
if len(noChLog) > 0 {
log.Println("警告: 替换前和替换后内容一样的文件有:")
for i, bakInfo := range noChLog {
var jobName Names = bakInfo.JobName
log.Printf("%d 解决方案: %s 项目: %s 作业: %s 文件: %s\n", i+1, jobName.Solution, jobName.Project, jobName.Replace, bakInfo.SourceFile)
}
}
duration := time.Since(startTime)
log.Printf("处理成功,用时 %.2f 秒。替换项: %d ; 文件操作: %d ; 执行命令: %d (外部命令: %d )\n", duration.Seconds(), totalReplace, totalIO, totalCMD, totalEXE)
}
func getPaths() {
var osFileNameArr []string = strings.Split(os.Args[0], string(filepath.Separator))
osExecFile[0] = strings.Join(osFileNameArr[:len(osFileNameArr)-1], string(filepath.Separator))
if len(osExecFile[0]) == 0 {
osExecFile[0] = "."
}
osExecFile[1] = osFileNameArr[len(osFileNameArr)-1]
osFileNameArr = strings.Split(osExecFile[1], ".")
if len(osFileNameArr) > 1 {
if len(osFileNameArr[1]) == 0 && osName == "windows" {
osFileNameArr[1] = ".exe"
} else if len(osFileNameArr[1]) > 0 {
osFileNameArr[1] = "." + osFileNameArr[1]
}
osExecFile[1] = osFileNameArr[0]
osExecFile[2] = osFileNameArr[1]
}
execPath, err := os.Executable()
if err == nil {
osExecFile[3] = execPath
realPath, err := filepath.EvalSymlinks(execPath)
if err == nil {
osExecFile[3] = realPath
}
}
osFileNameArr = strings.Split(osExecFile[3], string(filepath.Separator))
osExecFile[3] = strings.Join(osFileNameArr[:len(osFileNameArr)-1], string(filepath.Separator))
}
func fileType(file *os.File) byte {
var mime map[byte][]string = map[byte][]string{
'j': {"json"},
'y': {"yaml", "yml"},
}
filename := file.Name()
extension := filepath.Ext(filename)
for key, types := range mime {
for _, vtype := range types {
if extension == "."+vtype {
return key
}
}
}
return 0
}