-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
212 lines (178 loc) · 4.76 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package main
import (
"archive/zip"
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/widget"
"github.com/go-rod/rod"
"github.com/go-rod/rod/lib/launcher"
"healtool/tool"
"image/color"
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
)
var writeLog = tool.WriteLog
var writeLogFail = tool.WriteLogFail
func main() {
//os.Setenv("FYNE_FONT", "MSYHL.TTC")
defer func() {
if err := recover(); err != nil {
s := err.(string)
writeLogFail(s)
}
}()
myApp := app.New()
tool.FyneWindow = myApp.NewWindow("整合平台工具")
tool.LogWidget = widget.NewLabel("log info")
btn1 := widget.NewButton("basic install", func() {
taskStart()
})
btn2 := widget.NewButton("full install", func() {
writeLog("start execute full install task")
})
tool.LogContainer = container.NewScroll(tool.LogWidget)
tool.LogContainer.SetMinSize(fyne.Size{Height: 200})
btnContainer := container.New(layout.NewHBoxLayout(), layout.NewSpacer(), btn1, layout.NewSpacer(), btn2, layout.NewSpacer())
ine := canvas.NewLine(color.White)
ine.StrokeWidth = 5
// 使用说明
explanLabel := widget.NewLabel(`
instructions
1. dsaffdasfds
2. safsdfsdfsafas
3. sfsdafasdfsd`)
tool.FyneWindow.SetContent(container.NewVBox(tool.LogContainer, ine, btnContainer, explanLabel))
tool.FyneWindow.Resize(fyne.Size{Width: 500, Height: 400})
tool.FyneWindow.ShowAndRun()
}
func taskStart() {
//downloadZip()
//downloadChromeInstall()
openChrome()
}
func openChrome() {
path, _ := launcher.LookPath()
u := launcher.New().Bin(path).Delete("--headless").MustLaunch()
browser := rod.New().ControlURL(u).MustConnect()
browser.MustPage("https://health.cd120.info")
browser.MustPage("https://health-zhis.cd120.info")
dialog.NewConfirm("网址收藏", "请收藏网址", func(b bool) {
}, tool.FyneWindow).Show()
}
func downloadChromeInstall() {
var cmd *exec.Cmd
if runtime.GOOS == "windows" {
cmd = exec.Command("where", "chrome")
} else {
cmd = exec.Command("which", "google-chrome")
}
err := cmd.Run()
if err != nil {
fmt.Println("Chrome浏览器未安装")
} else {
fmt.Println("Chrome浏览器已安装")
}
}
func downloadZip() {
writeLog("start download install zip file")
zipName := "install.zip"
zipUrl := "https://health.cd120.info/health-web/f/f.zip"
writeLog("开始下载压缩包文件")
resp, err := http.Get(zipUrl)
if err != nil {
writeLog(err.Error())
return
}
writeLog("压缩包文件下载完成")
// 创建本地zip文件
zipFile, err := os.Create(zipName)
if err != nil {
writeLog(err.Error())
return
}
writeLog("压缩包文件拷贝到实体文件")
_, err = io.Copy(zipFile, resp.Body)
if err != nil {
writeLog(err.Error())
return
}
// 压缩包
var src = zipName
// 获取程序运行目录
workDir, err := os.Getwd()
if err != nil {
writeLog(err.Error())
}
var dst = filepath.Join(workDir, "output")
writeLog("当前工作目录:" + dst)
writeLog("开始解压")
if err := UnZip(dst, src); err != nil {
writeLog(err.Error())
return
}
writeLog("解压完成")
}
func UnZip(dst, src string) (err error) {
// 打开压缩文件,这个 zip 包有个方便的 ReadCloser 类型
// 这个里面有个方便的 OpenReader 函数,可以比 tar 的时候省去一个打开文件的步骤
zr, err := zip.OpenReader(src)
defer zr.Close()
if err != nil {
return
}
// 如果解压后不是放在当前目录就按照保存目录去创建目录
if dst != "" {
if err := os.MkdirAll(dst, 0755); err != nil {
return err
}
}
// 遍历 zr ,将文件写入到磁盘
for _, file := range zr.File {
//continue
if strings.HasPrefix(filepath.Base(file.Name), ".") {
//fmt.Println(file.Name + ": 包含")
continue
}
path := filepath.Join(dst, file.Name)
// 如果是目录,就创建目录
if file.FileInfo().IsDir() {
if err := os.MkdirAll(path, file.Mode()); err != nil {
return err
}
// 因为是目录,跳过当前循环,因为后面都是文件的处理
continue
}
// 获取到 Reader
fr, err := file.Open()
if err != nil {
return err
}
// 创建要写出的文件对应的 Write
fw, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR|os.O_TRUNC, file.Mode())
if err != nil {
return err
}
_, err = io.Copy(fw, fr)
if err != nil {
return err
}
// 将解压的结果输出
//fmt.Printf("成功解压 %s ,共写入了 %d 个字符的数据\n", path, n)
// 因为是在循环中,无法使用 defer ,直接放在最后
// 不过这样也有问题,当出现 err 的时候就不会执行这个了,
// 可以把它单独放在一个函数中,这里是个实验,就这样了
//fw.Close()
fr.Close()
}
return nil
}