-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.go
54 lines (44 loc) · 1.09 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
package main
import (
"errors"
"log"
"net/url"
"github.com/kelseyhightower/envconfig"
)
// Config holds the herald's configuration
type Config struct {
SlackToken string `envconfig:"SLACK_TOKEN"`
MongoURI string `envconfig:"MONGOLAB_URI"`
MongoDB string
DestChan string `envconfig:"DEST_CHAN"`
DiffChan string `envconfig:"DIFF_CHAN"`
}
// NewConfig parses a Config from the environment.
func NewConfig() (Config, error) {
var c Config
if err := envconfig.Process("", &c); err != nil {
return Config{}, err
}
if c.SlackToken == "" {
return Config{}, errors.New("Missing env var SLACK_TOKEN")
}
if c.MongoURI == "" {
return Config{}, errors.New("Missing env var MONGOLAB_URI")
}
u, err := url.Parse(c.MongoURI)
if err != nil {
return Config{}, err
}
if len(u.Path) < 2 {
return Config{}, errors.New("Missing DB at end of MONGOLAB_URI")
}
c.MongoDB = u.Path[1:]
log.Printf("%+v", c)
if c.DestChan == "" {
return Config{}, errors.New("Missing env var DEST_CHAN")
}
if c.DiffChan == "" {
return Config{}, errors.New("Missing env var DIFF_CHAN")
}
return c, nil
}