-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
63 lines (50 loc) · 1.05 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
package main
import (
"encoding/json"
"errors"
"io/ioutil"
"os"
"path/filepath"
)
type CiConfg struct {
Port string
Triggers []Trigger
}
type Trigger struct {
Issuer string
Repository string
Branch string
Type string
SshUser string
SshHost string
SshKey string
Cmd string
}
func getConfigContent() ([]byte, error) {
cfgFilepath := ""
appPath, err := filepath.Abs(os.Args[0])
if err != nil {
return []byte{}, err
}
cfgFilepath = appPath + ".json"
return ioutil.ReadFile(cfgFilepath)
}
func LoadConfig() (*CiConfg, error) {
cfg := &CiConfg{}
cfgContent, err := getConfigContent()
if err != nil {
return cfg, err
}
err = json.Unmarshal(cfgContent, &cfg)
return cfg, err
}
func GetMatchedTrigger(cfg CiConfg, notify Notify, issuer string) (Trigger, error) {
for _, trigger := range cfg.Triggers {
if trigger.Repository == notify.Repository &&
trigger.Issuer == issuer &&
trigger.Branch == notify.Branch {
return trigger, nil
}
}
return Trigger{}, errors.New("no matched trigger found")
}