forked from hound-search/hound
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
158 lines (133 loc) · 3.72 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package config
import (
"encoding/json"
"errors"
"os"
"path/filepath"
)
const (
defaultMsBetweenPoll = 30000
defaultMaxConcurrentIndexers = 2
defaultPushEnabled = false
defaultPollEnabled = true
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"`
VcsConfigMessage *SecretMessage `json:"vcs-config"`
UrlPattern *UrlPattern `json:"url-pattern"`
ExcludeDotFiles bool `json:"exclude-dot-files"`
EnablePollUpdates *bool `json:"enable-poll-updates"`
EnablePushUpdates *bool `json:"enable-push-updates"`
}
// Used for interpreting the config value for fields that use *bool. If a value
// is present, that value is returned. Otherwise, the default is returned.
func optionToBool(val *bool, def bool) bool {
if val == nil {
return def
}
return *val
}
// Are polling based updates enabled on this repo?
func (r *Repo) PollUpdatesEnabled() bool {
return optionToBool(r.EnablePollUpdates, defaultPollEnabled)
}
// Are push based updates enabled on this repo?
func (r *Repo) PushUpdatesEnabled() bool {
return optionToBool(r.EnablePushUpdates, defaultPushEnabled)
}
type Config struct {
DbPath string `json:"dbpath"`
Repos map[string]*Repo `json:"repos"`
MaxConcurrentIndexers int `json:"max-concurrent-indexers"`
}
// SecretMessage is just like json.RawMessage but it will not
// marshal its value as JSON. This is to ensure that vcs-config
// is not marshalled into JSON and send to the UI.
type SecretMessage []byte
// This always marshals to an empty object.
func (s *SecretMessage) MarshalJSON() ([]byte, error) {
return []byte("{}"), nil
}
// See http://golang.org/pkg/encoding/json/#RawMessage.UnmarshalJSON
func (s *SecretMessage) UnmarshalJSON(b []byte) error {
if b == nil {
return errors.New("SecretMessage: UnmarshalJSON on nil pointer")
}
*s = append((*s)[0:0], b...)
return nil
}
// Get the JSON encode vcs-config for this repo. This returns nil if
// the repo doesn't declare a vcs-config.
func (r *Repo) VcsConfig() []byte {
if r.VcsConfigMessage == nil {
return nil
}
return *r.VcsConfigMessage
}
// 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
}
}
}
// Populate missing config values with default values.
func initConfig(c *Config) {
if c.MaxConcurrentIndexers == 0 {
c.MaxConcurrentIndexers = defaultMaxConcurrentIndexers
}
}
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)
}
initConfig(c)
return nil
}
func (c *Config) ToJsonString() (string, error) {
b, err := json.Marshal(c.Repos)
if err != nil {
return "", err
}
return string(b), nil
}