-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.go
More file actions
95 lines (83 loc) · 1.96 KB
/
config.go
File metadata and controls
95 lines (83 loc) · 1.96 KB
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
package main
import (
"encoding/json"
"io"
"log"
"os"
"path/filepath"
)
type Config struct {
Addr string
Secret string
Repositories []RepositoryConfig
}
type RepositoryConfig struct {
Name string
URL string
Branch string
Command []string
Dir string
}
func (c *Config) FindRepositoryConfig(name string, n Notification) (RepositoryConfig, bool) {
for _, repositoryConfig := range c.Repositories {
if repositoryConfig.URL != "" && repositoryConfig.URL != n.RepositoryURL() {
continue
}
if repositoryConfig.Name != "" && repositoryConfig.Name != name {
continue
}
if repositoryConfig.Branch != "" {
if _, found := n.Branches()[repositoryConfig.Branch]; !found {
continue
}
}
return repositoryConfig, true
}
return RepositoryConfig{}, false
}
func appendConfig(config *Config, reader io.Reader) error {
var currentConfig Config
err := json.NewDecoder(reader).Decode(¤tConfig)
if err != nil {
return err
}
if currentConfig.Addr != "" {
config.Addr = currentConfig.Addr
}
if currentConfig.Secret != "" {
config.Secret = currentConfig.Secret
}
config.Repositories = append(config.Repositories, currentConfig.Repositories...)
return nil
}
func makeConfigPathWalkFunc(config *Config) func(path string, f os.FileInfo, err error) error {
return func(path string, f os.FileInfo, err error) error {
if err != nil {
return err
}
if f.Mode().IsRegular() {
var file *os.File
file, err = os.Open(path)
if err != nil {
return err
}
defer file.Close()
err = appendConfig(config, file)
if err != nil {
log.Printf("Can't parse config file %q: %v", path, err)
return nil
}
}
return nil
}
}
func ReadConfig(filename string) (Config, error) {
config := Config{}
config.Addr = ":8080"
err := filepath.Walk(filename, makeConfigPathWalkFunc(&config))
if err != nil {
return config, err
}
log.Printf("Config loaded from %q: %#v", filename, config)
return config, nil
}