-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
158 lines (142 loc) · 2.94 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
package main
import (
"bytes"
_ "embed"
"log"
"os"
"os/exec"
"strings"
)
const (
Unsupport int8 = iota
CentOS
Ubuntu
Debian
)
//go:embed update.sh
var script []byte
func main() {
log.SetPrefix("distUpdater: ")
OSType := checkOS()
OSName := ""
switch OSType {
case Unsupport:
log.Println("不支持的操作系统,请更换为CentOS, Ubuntu, Debian")
return
case CentOS:
OSName = "CentOS"
case Ubuntu:
OSName = "Ubuntu"
case Debian:
OSName = "Debian"
}
log.Println("检测到您的系统为:", OSName)
err := installDep(OSType)
if err != nil {
log.Fatalf("构建工具安装失败: %v,请手动安装仓库构建工具", err)
}
err = updateRepo()
if err != nil {
log.Fatalf("仓库更新失败: %v", err)
}
err = push()
if err != nil {
log.Fatalf("仓库推送失败: %v", err)
}
}
func checkOS() int8 {
file, err := os.ReadFile("/etc/os-release")
if err != nil {
return Unsupport
}
if bytes.Contains(file, []byte("CentOS")) {
return CentOS
}
if bytes.Contains(file, []byte("Ubuntu")) {
return Ubuntu
}
if bytes.Contains(file, []byte("Debian")) {
return Debian
}
return Unsupport
}
func installDep(OSType int8) error {
pm := ""
switch OSType {
case Ubuntu, Debian:
pm = "apt"
case CentOS:
pm = "dnf"
}
log.Println("正在安装仓库构建工具...")
log.Printf("Running sudo %s install reprepro createrepo -y\n", pm)
c := exec.Command("sudo", pm, "install", "reprepro", "createrepo", "-y")
c.Stderr = os.Stderr
err := c.Run()
if err != nil {
return err
}
log.Println("构建工具安装成功")
return nil
}
func updateRepo() error {
downDir, err := os.ReadDir("./download")
if err != nil {
return err
}
repreproArgs := []string{"-C", "main", "-b", "./deb", "includedeb", "nini"}
for i := range downDir {
fName := downDir[i].Name()
if !downDir[i].IsDir() {
if strings.HasSuffix(fName, ".rpm") {
err = os.Rename("./download/"+fName, "./rpm/dist/"+fName)
if err != nil {
return err
}
continue
}
if strings.HasSuffix(fName, ".deb") {
repreproArgs = append(repreproArgs, "./download/"+fName)
continue
}
}
}
log.Println("正在更新APT仓库...")
log.Println("Running reprepro -C main -b ./deb includedeb nini ./download/*.deb")
c := exec.Command("reprepro", repreproArgs...)
c.Stderr = os.Stderr
err = c.Run()
if err != nil {
return err
}
log.Println("APT仓库更新完成")
log.Println("正在更新RPM仓库...")
c = exec.Command("createrepo", "--update", "./rpm/dist")
c.Stderr = os.Stderr
err = c.Run()
if err != nil {
return err
}
log.Println("RPM仓库更新完成")
return nil
}
func push() error {
log.Println("正在推送仓库...")
f, err := os.Create("__tmp.sh")
defer os.Remove("__tmp.sh")
if err != nil {
return err
}
_, err = f.Write(script)
if err != nil {
return err
}
f.Close()
c := exec.Command("bash", "__tmp.sh")
c.Stderr = os.Stderr
err = c.Run()
if err != nil {
return err
}
return nil
}