-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig.go
75 lines (61 loc) · 1.38 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
package main
import (
"encoding/json"
"regexp"
)
type Config struct {
Browsers []Browser `json:"browsers"`
DefaultBrowserName string `json:"defaultBrowser"`
Matchers []Matcher `json:"matchers"`
}
type Browser struct {
Name string `json:"name"`
Path string `json:"path"`
}
type Matcher struct {
Regexp string `json:"regexp"`
BrowserName string `json:"browser"`
}
func ParseConfig(configJson string) Config {
var config Config
err := json.Unmarshal([]byte(configJson), &config)
if err != nil {
ShowError(
"Couldn't parse config file",
err.Error(),
)
panic(err)
}
return config
}
func (config Config) GetBrowser(name string) Browser {
for _, browser := range config.Browsers {
if browser.Name == name {
return browser
}
}
ShowError(
"Couldn't find browser",
"Couldn't find browser with name: "+name,
)
panic("couldn't find browser with name " + name)
}
func (config Config) GetDefaultBrowser() Browser {
return config.GetBrowser(config.DefaultBrowserName)
}
func (config Config) GetBrowserForUrl(url string) Browser {
for _, matcher := range config.Matchers {
regex, err := regexp.Compile(matcher.Regexp)
if err != nil {
ShowError(
"Couldn't compile regex",
matcher.Regexp,
)
panic(err)
}
if regex.MatchString(url) {
return config.GetBrowser(matcher.BrowserName)
}
}
return config.GetDefaultBrowser()
}