-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
49 lines (43 loc) · 1.08 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
package main
import (
"io"
"log"
"github.com/BurntSushi/toml"
)
// Config is the configuration for go-talks
type Config struct {
// Addresses & ports to listen on
Listen []string `toml:"listen"`
// Meeting password
Password string `toml:"password"`
// Database file name
Database string `toml:"database"`
// Trusted subnets
Subnets []string `toml:"subnets"`
}
// ParseConfig parses a TOML config file and returns a Config struct
func ParseConfig(r io.Reader) Config {
var config Config
_, err := toml.NewDecoder(r).Decode(&config)
if err != nil {
log.Fatal("[FATAL] Could not parse config file:", err)
}
return config
}
// Validate checks a Config for some common errors
func (c *Config) Validate() {
if len(c.Listen) == 0 {
log.Fatal("[FATAL] No listen addresses specified")
}
if c.Password == "" {
log.Fatal("[FATAL] No password specified")
}
if c.Database == "" {
log.Fatal("[FATAL] No database specified")
}
}
// Network creates a Networks struct from the []string
// of subnets in the Config
func (c *Config) Network() Networks {
return NewNetworks(c.Subnets)
}