/
config.go
41 lines (32 loc) · 920 Bytes
/
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
package config
import (
"errors"
"io/ioutil"
"gopkg.in/yaml.v2"
"github.com/ivanilves/lstags/util/fix"
)
// Config holds repository list (e.g. loadable from YAML file)
// NB!
// There are no mapping between main.Options and config.Config!
// We could implement it, but we need to see an explicit demand
// for this feature from our stakeholders.
type Config struct {
Repositories []string `yaml:"repositories"`
}
// LoadYAMLFile loads YAML file into Config structure
func LoadYAMLFile(path string) (*Config, error) {
data, err := ioutil.ReadFile(fix.Path(path))
if err != nil {
return nil, err
}
structure := struct {
ConfigRoot Config `yaml:"lstags"`
}{}
if err := yaml.Unmarshal([]byte(data), &structure); err != nil {
return nil, err
}
if structure.ConfigRoot.Repositories == nil {
return nil, errors.New("no repos could be loaded from: " + path)
}
return &structure.ConfigRoot, nil
}