forked from fulldump/goconfig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goconfig.go
42 lines (32 loc) · 857 Bytes
/
goconfig.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
package goconfig
import (
"errors"
"flag"
"io/ioutil"
"os"
)
func Read(c interface{}) {
if err := readWithError(c); err != nil {
os.Stderr.WriteString(err.Error())
os.Exit(1)
}
}
func readWithError(c interface{}) error {
f := flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
f.SetOutput(ioutil.Discard)
filename := f.String("config", "", "-usage-")
f.Parse(os.Args[1:])
// Read from file JSON
if err := FillJson(c, *filename); err != nil {
return errors.New("Config file error: " + err.Error())
}
// Overwrite configuration with environment vars:
if err := FillEnvironments(c); err != nil {
return errors.New("Config env error: " + err.Error())
}
// Overwrite configuration with command line args:
if err := FillArgs(c, os.Args[1:]); err != nil {
return errors.New("Config arg error: " + err.Error())
}
return nil
}