forked from juju/charmstore
/
config.go
71 lines (65 loc) · 1.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
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
// The config package defines configuration parameters for
// the charm store.
package config
import (
"fmt"
"io/ioutil"
"os"
"strings"
"gopkg.in/errgo.v1"
"gopkg.in/yaml.v1"
)
type Config struct {
// TODO(rog) rename this to MongoAddr - it's not a URL.
MongoURL string `yaml:"mongo-url"`
APIAddr string `yaml:"api-addr"`
AuthUsername string `yaml:"auth-username"`
AuthPassword string `yaml:"auth-password"`
ESAddr string `yaml:"elasticsearch-addr"` // elasticsearch is optional
}
func (c *Config) validate() error {
var missing []string
if c.MongoURL == "" {
missing = append(missing, "mongo-url")
}
if c.APIAddr == "" {
missing = append(missing, "api-addr")
}
if c.AuthUsername == "" {
missing = append(missing, "auth-username")
}
if strings.Contains(c.AuthUsername, ":") {
return fmt.Errorf("invalid user name %q (contains ':')", c.AuthUsername)
}
if c.AuthPassword == "" {
missing = append(missing, "auth-password")
}
if len(missing) != 0 {
return fmt.Errorf("missing fields %s in config file", strings.Join(missing, ", "))
}
return nil
}
// Read reads a charm store configuration file from the
// given path.
func Read(path string) (*Config, error) {
f, err := os.Open(path)
if err != nil {
return nil, errgo.Notef(err, "cannot open config file")
}
defer f.Close()
data, err := ioutil.ReadAll(f)
if err != nil {
return nil, errgo.Notef(err, "cannot read %q", path)
}
var conf Config
err = yaml.Unmarshal(data, &conf)
if err != nil {
return nil, errgo.Notef(err, "cannot parse %q", path)
}
if err := conf.validate(); err != nil {
return nil, errgo.Mask(err)
}
return &conf, nil
}