-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGitAutoDeploy.go
195 lines (154 loc) · 3.83 KB
/
GitAutoDeploy.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"os/exec"
"strconv"
//"reflect"
)
const configFilePath = "conf.json"
// hold config infomation
// when get from configFile
type config struct {
port float64
repositories []map[string]interface{}
deployPath string
}
// objective: replace interface{} in repositories
// not done yet
type repo struct {
url string
path string
}
// must delete this
type testStruct struct {
test string
}
// serve func
// flow: req -> parse request to get param (urls) ->
// -> loop all urls -> get path of each url -> pull -> deploy
func gitAutoDeploy(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
fmt.Println(r)
var t testStruct
for key := range r.Form {
log.Println(key)
//LOG: {"test": "that"}
err := json.Unmarshal([]byte(key), &t)
if err != nil {
log.Println(err.Error())
}
}
log.Println(t)
// var result map[string]interface{}
// data := json.Unmarshal(r, &result);
// repository := data["repository"](map[string]string)
// url := repository["url"](string)
// fmt.Println("url")
// get all urls
// for url := range urls {
// path, err = getMatchingPath(url)
// pull(path)
// deploy(path)
// }
}
func parseRequest(r *http.Request) {
}
func getMatchingPath(url string, repositories []map[string]interface{}) (map[string]interface{}, error) {
for _, repository := range repositories {
if repository["url"] == url {
return repository, nil
}
}
return nil, nil
}
func check(e error) {
if e != nil {
panic(e)
}
}
// func get config from Config file
// given: config file represent by configPath
// return: config
func getConfig(configPath string) config {
var result config // hold result
var fileContent map[string]interface{} // hold file content when get read file
data := make([]byte, 1000) /* hold data in fileConfig
value 1000 is len of slice (going to change it)
config file have length unknown and must loop to read all data */
// open file
file, err := os.Open(configPath)
check(err)
// open stream and read from file to data
count, err := file.Read(data)
if err != nil {
log.Fatal(err)
}
// fmt.Printf("read %d bytes: %q\n", count, data[:count])
// convert data to fileContent
err = json.Unmarshal(data[:count], &fileContent)
check(err)
result.port = fileContent["port"].(float64)
repositories := fileContent["repositories"].([]interface{})
for _, value := range repositories {
element := value.(map[string]interface{})
result.repositories = append(result.repositories, element)
}
return result
}
//change directory and pull from origin
//if pull failed , ...
func pull(path string, cfg config) error {
changeDir := exec.Command("cd", path)
err := changeDir.Run()
if err != nil {
log.Fatal(err)
return err
}
cmd := exec.Command("git", "pull")
err = cmd.Run()
if err != nil {
log.Fatal(err)
return err
}
fmt.Println("Pull success")
return nil
}
func deploy(path string, cfg config) error {
changeDir := exec.Command("cd", path)
changeDir.CombinedOutput()
cmd := exec.Command(cfg.deployPath)
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
fmt.Println(fmt.Sprint(err) + ": " + stderr.String())
}
return nil
}
func respond() error {
return nil
}
func main() {
fmt.Println("WebService - GitAutoDeploy Starting ... ")
var cfg config
cfg = getConfig(configFilePath)
http.HandleFunc("/", gitAutoDeploy)
// fmt.Println(cfg)
fmt.Printf("GitAutoDeploy Listening At Port %v ...", cfg.port)
// pass port into string
// string need to pass to ListenandServe
strPort := strconv.Itoa(int(cfg.port))
strPort = ":" + strPort
err := http.ListenAndServe(strPort, nil)
// err := http.ListenAndServe(":80", nil)
if err == nil {
log.Fatal("Error: ", err)
}
}