-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
82 lines (72 loc) · 1.53 KB
/
config.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
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"os"
)
type Config struct {
Port int
Rules []Rule
CIDROverride []string `json:"cidr_override"`
CIDRs []*net.IPNet
}
type Rule struct {
Command string
Criteria []Criteria
}
type Criteria struct {
Event string
Owner string
Repository string
PushParams struct {
Branch string
} `json:"push_params"`
ReleaseParams struct {
Prerelease *bool
} `json:"release_params"`
}
func LoadConfig(fileName string) Config {
file, err := os.Open(fileName)
defer file.Close()
if err != nil {
log.Fatal("Error loading config file: ", err)
}
return getConfigFromReader(file)
}
func getConfigFromReader(r io.Reader) Config {
decoder := json.NewDecoder(r)
config := Config{}
err := decoder.Decode(&config)
if err != nil {
log.Fatal("Invalid config file: ", err)
}
//get valid CIDRs from Github
if len(config.CIDROverride) != 0 {
config.CIDRs = parseCIDRs(config.CIDROverride)
} else {
config.CIDRs = getGithubCIDRs()
}
fmt.Println("CIDRs: ", config.CIDRs)
return config
}
func getGithubCIDRs() []*net.IPNet {
//request CIDRs from Github
resp, err := http.Get("https://api.github.com/meta")
if err != nil {
log.Fatal("Could not load Github CIDRs")
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
var data struct {
Hooks []string //we only really care about the Hooks
}
json.Unmarshal(body, &data)
//convert the response into net.IPNet slice
cidrs := parseCIDRs(data.Hooks)
return cidrs
}