forked from hound-search/hound
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
93 lines (77 loc) · 1.67 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
83
84
85
86
87
88
89
90
91
92
93
package config
import (
"encoding/json"
"os"
"path/filepath"
)
const (
defaultMsBetweenPoll = 30000
defaultVcs = "git"
defaultBaseUrl = "{url}/blob/master/{path}{anchor}"
defaultAnchor = "#L{line}"
)
type UrlPattern struct {
BaseUrl string `json:"base-url"`
Anchor string `json:"anchor"`
}
type Repo struct {
Url string `json:"url"`
MsBetweenPolls int `json:"ms-between-poll"`
Vcs string `json:"vcs"`
UrlPattern *UrlPattern `json:"url-pattern"`
}
type Config struct {
DbPath string `json:"dbpath"`
Repos map[string]*Repo `json:"repos"`
}
// Populate missing config values with default values.
func initRepo(r *Repo) {
if r.MsBetweenPolls == 0 {
r.MsBetweenPolls = defaultMsBetweenPoll
}
if r.Vcs == "" {
r.Vcs = defaultVcs
}
if r.UrlPattern == nil {
r.UrlPattern = &UrlPattern{
BaseUrl: defaultBaseUrl,
Anchor: defaultAnchor,
}
} else {
if r.UrlPattern.BaseUrl == "" {
r.UrlPattern.BaseUrl = defaultBaseUrl
}
if r.UrlPattern.Anchor == "" {
r.UrlPattern.Anchor = defaultAnchor
}
}
}
func (c *Config) LoadFromFile(filename string) error {
r, err := os.Open(filename)
if err != nil {
return err
}
defer r.Close()
if err := json.NewDecoder(r).Decode(c); err != nil {
return err
}
if !filepath.IsAbs(c.DbPath) {
path, err := filepath.Abs(
filepath.Join(filepath.Dir(filename), c.DbPath))
if err != nil {
return err
}
c.DbPath = path
}
for _, repo := range c.Repos {
initRepo(repo)
}
return nil
}
func (c *Config) ToJsonString() (string, error) {
b, err := json.Marshal(c.Repos)
if err != nil {
return "", err
}
return string(b), nil
}