-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathlgh_git_util.go
168 lines (146 loc) · 3.02 KB
/
lgh_git_util.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
package main
import (
"net/http"
"os/exec"
"fmt"
"flag"
"os"
)
type my struct {
fuck func()
}
func (h *my) ServeHTTP(w http.ResponseWriter,r *http.Request) {
h.fuck()
}
var cmd *exec.Cmd
func main() {
arg := flag.String(
"arg",
"",
"更新书使用 --arg=\"update\" \n 第一次上传 --arg=\"upload\"")
flag.Parse()
if arg==nil{
fmt.Println("命令参数不能为 null,输入: -help 查看帮助")
return
}
if *arg==""{
fmt.Println("命令参数不能为 null,输入: -help 查看帮助")
return
}
create_gitignore()
if *arg=="upload" {
fmt.Println("第一次上传")
// 删除一次 .git
cmd = exec.Command("rm","-rf", ".git")
err := cmd.Run()
if err != nil {
fmt.Println(err)
}
if git_init() {
if git_add_all() {
if git_commit("第一次提交") {
if git_remote() {
if git_push() {
echoJson("success","成功")
}
}
}
}
}
}
if *arg=="update" {
fmt.Println("更新书")
if git_add_all() {
if git_commit("更新提交") {
if git_push() {
echoJson("success","成功")
}
}
}
}
}
func git_init() bool {
cmd = exec.Command("git", "init")
return exeCmd(func(err error) {
fmt.Println(err)
echoFailed("git_init "+err.Error())
}, func() {
fmt.Println("git init 成功")
})
}
func git_add_all() bool {
cmd = exec.Command("git", "add",".")
return exeCmd(func(err error) {
fmt.Println(err)
echoFailed("git_add_all "+err.Error())
}, func() {
fmt.Println("git add . 成功")
})
}
func git_commit(str string) bool {
cmd = exec.Command("git", "commit","-m",str)
return exeCmd(func(err error) {
fmt.Println(err)
echoFailed("git_commit "+err.Error())
}, func() {
fmt.Println("git commit -m \"...\" 成功")
})
}
const (
user = "lgh"
pw = "123aaa"
name = "lgh"
git = "test3"
footer = ".git"
host = "localhost"
port = "3000"
url = "http://"+user+":"+pw+"@"+host+":"+port+"/"+name+"/"+git+footer
)
func git_remote() bool {
cmd = exec.Command("git", "remote","add","origin",url)
return exeCmd(func(err error) {
fmt.Println(err.Error()+" ---> "+url)
echoFailed("git_remote "+err.Error())
}, func() {
fmt.Println("git remote add origin \""+url+"\" 成功")
})
}
func git_push() bool {
cmd = exec.Command("git", "push","-u","origin","master")
return exeCmd(func(err error) {
fmt.Println(err)
echoFailed("git_push "+err.Error())
}, func() {
fmt.Println("git push -u origin master \"...\" 成功")
})
}
func exeCmd(failed func(err error),success func()) bool {
err := cmd.Run()
if err != nil {
failed(err)
return false
}
success()
return true
}
func echoFailed(msg string) {
echoJson("failed",msg)
}
func echoJson(status,msg string) {
fmt.Println("{\"ret\":\""+status+"\",\"msg\":\""+msg+"\"}")
}
func create_gitignore() {
fileName := ".gitignore"
_,err := os.Stat(fileName)
if err == nil {
fmt.Println("文件:.gitignore 已经存在")
return
}
file,err := os.Create(fileName)
defer file.Close()
if err!=nil {
fmt.Println(err)
return
}
file.WriteString("lgh_git_util")
}