/
config.go
60 lines (49 loc) · 1.14 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
package server
import (
"path/filepath"
"io/ioutil"
"strings"
"encoding/json"
"gopkg.in/yaml.v2"
"github.com/BurntSushi/toml"
)
type Config struct {
PluginsDir string `yaml:"pluginsDir"`
Defaults Repository
Repositories Repositories
Plugins map[string]string
}
func NewConfig() *Config {
return new(Config)
}
func (config *Config) SetDefaults(defaults Repository) {
config.PluginsDir = "plugins"
config.Defaults = defaults
}
func (config *Config) Load(filename string) error {
// Select configuration file name and type
var format = strings.TrimPrefix(filepath.Ext(filename), ".")
// Read configuration file
var file, err = ioutil.ReadFile(filename)
if err != nil {
return err
}
// Read configuration
switch format {
case "json":
err = json.Unmarshal(file, config)
case "yaml", "yml":
err = yaml.Unmarshal(file, config)
case "toml":
err = toml.Unmarshal(file, config)
}
if err != nil {
return err
}
config.Repositories.Sort()
config.Repositories.SetDefaults(config.Defaults)
return nil
}
func (config *Config) FindRepo(reponame string) *Repository {
return config.Repositories.Search(reponame)
}