forked from karasz/gnocco
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
67 lines (57 loc) · 1.18 KB
/
config.go
File metadata and controls
67 lines (57 loc) · 1.18 KB
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
package gnocco
import (
"bytes"
"io"
"github.com/BurntSushi/toml"
"darvaza.org/darvaza/shared/config"
"darvaza.org/darvaza/shared/config/expand"
)
// Config represents the configuration file for setting up
// the wilee Server
type Config struct {
Listen ListenS
Log LogS
}
// ListenS is the Listen config struct
type ListenS struct {
Host string
Port string
}
// LogS is the logging config struct
type LogS struct {
Stdout bool
File string
}
// Prepare attempts to validate and fill the gaps on a
// Config object
func (c *Config) Prepare() error {
return config.Prepare(c)
}
// ReadInFile loads a TOML config file into a Config object,
// validates and fills the gaps
func (c *Config) ReadInFile(filename string) error {
// read and expand
s, err := expand.FromFile(filename, nil)
if err != nil {
return err
}
// decode
_, err = toml.Decode(s, c)
if err != nil {
return err
}
// and validate
return c.Prepare()
}
// WriteTo writes the Config in TOML format
func (c *Config) WriteTo(w io.Writer) (int64, error) {
var buf bytes.Buffer
// encode
enc := toml.NewEncoder(&buf)
err := enc.Encode(c)
if err != nil {
return 0, err
}
// write
return buf.WriteTo(w)
}