-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatch.go
127 lines (108 loc) · 3.56 KB
/
watch.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
/*
watch.go
Sayed Khader [email protected]
when invoked, external file is read that maps a remote github repository
to a local active directory. If the remote commit hash mismatches the hash
in the local repository, perform a pull to the active directory
external file format is a valid json:
{
"repo_name": "local_directory",
. . .
}
*/
package main
import "os"
import "os/exec"
import "fmt"
import "bytes"
import "encoding/json"
import "io/ioutil"
import "net/http"
import _ "crypto/sha256"
type RepoResponse struct {
Commit struct {
Sha string
}
}
func main() {
GITHUB_HOST := "https://api.github.com/"
home := os.Getenv("HOME")
confdir := home + "/.gowatch/"
repoFilePath := confdir + "repo_map.json"
fileBuffer, err := ioutil.ReadFile(repoFilePath)
branch := os.Getenv("ENV")
if branch == "prod" {
branch = "master"
}
token_path := confdir + "github_creds"
gitToken, tokenErr := ioutil.ReadFile(token_path)
if tokenErr != nil {
panic(tokenErr) // problem reading github OAuth token
}
gitToken = gitToken[:len(gitToken)-1]
// perform git auth
if err != nil {
panic(err) // problem creating auth request
}
client := &http.Client{}
if err != nil {
panic(err)
}
//var githubConfig map[string]interface{}
var githubConfig map[string]map[string]string
err = json.Unmarshal(fileBuffer, &githubConfig)
if err != nil {
panic(err)
}
for gitUser, repoMap := range githubConfig {
for repoName, activeDir := range repoMap {
// determine the local commit hash
masterHashPath := activeDir +"/.git/refs/heads/" + branch
masterHashBuffer, masterError := ioutil.ReadFile(masterHashPath)
var localHash string
if masterError == nil {
localHash = string(masterHashBuffer)
localHash = localHash[:len(localHash)-1]
} else {
panic(masterError)
}
repoPath := "repos/"+gitUser+"/"+repoName+"/branches/" + branch
repoUrl := GITHUB_HOST + repoPath
//fmt.Printf("repo url: %s\n", repoUrl)
branchReq, err := http.NewRequest("GET", repoUrl, nil)
if err != nil {
panic(err)
}
branchReq.Header.Add("Authorization","token "+string(gitToken))
resp, err := client.Do(branchReq)
if err != nil {
panic(err)
}
respBody, err := ioutil.ReadAll(resp.Body)
// determine remote commit hash, perform auth
rr := &RepoResponse{}
err = json.Unmarshal(respBody, &rr)
if err != nil {
panic(err)
}
if localHash != rr.Commit.Sha {
// issue a pull inside the local active directory
err = os.Chdir(activeDir)
fmt.Printf("chdr(%s)\n", activeDir)
if err != nil {
panic(err)
}
var out bytes.Buffer
pullCmd := exec.Command("ssh-agent", "/bin/sh", "-c", "ssh-add "+ home+"/.ssh/gowatch_id_rsa; git pull origin " + branch)
fmt.Println(pullCmd)
pullCmd.Stdout = &out
err = pullCmd.Run()
if err != nil {
fmt.Println(err)
panic(err) // err running git pull
}
fmt.Println(out.String())
}
}
}
}