-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
115 lines (99 loc) · 2.39 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package esc
import (
"flag"
"github.com/ghodss/yaml"
"io/ioutil"
"log"
"net"
"os"
)
var config *EscConfig
func defaultConfig() EscConfig {
host, err := os.Hostname()
if err != nil {
panic(err)
}
config := EscConfig{
Node: host,
Host: host,
Port: 8901,
Discovery: 8902,
IFace: "eth0",
Directory: "files",
Extras: make(map[string]string),
}
return config
}
func Config() *EscConfig {
if config != nil {
return config
}
config = &EscConfig{}
defaultConfig := defaultConfig()
configFile := "config.yml"
if len(os.Args) > 1 && len(os.Args)%2 == 0 {
configFile = os.Args[len(os.Args)-1]
}
_, err := os.Stat(configFile)
var fileConfig EscConfig
if err == nil {
content, err0 := ioutil.ReadFile(configFile)
if err0 != nil {
panic(err0)
}
err1 := yaml.Unmarshal(content, &fileConfig)
if err1 != nil {
panic(err1)
}
if fileConfig.Node != "" {
defaultConfig.Node = fileConfig.Node
}
if fileConfig.Join != "" {
defaultConfig.Join = fileConfig.Join
}
if fileConfig.IFace != "" {
defaultConfig.IFace = fileConfig.IFace
}
if fileConfig.Discovery != 0 {
defaultConfig.Discovery = fileConfig.Discovery
}
if fileConfig.Port != 0 {
defaultConfig.Port = fileConfig.Port
}
if fileConfig.Directory != "" {
defaultConfig.Directory = fileConfig.Directory
}
if fileConfig.Extras != nil {
config.Extras = fileConfig.Extras
}
} else {
log.Println(err)
}
flag.StringVar(&config.Node, "node", defaultConfig.Node, "Name of this node")
flag.StringVar(&config.Join, "join", defaultConfig.Join, "Address of node to join")
flag.StringVar(&config.IFace, "iface", defaultConfig.IFace, "Network Interface to bind to")
flag.StringVar(&config.Directory, "dir", defaultConfig.Directory, "Plugin/Directory directory")
flag.IntVar(&config.Discovery, "discovery", defaultConfig.Discovery, "Port for network discovery")
flag.IntVar(&config.Port, "port", defaultConfig.Port, "Port for cluster conns")
flag.Parse()
iface, errFace := net.InterfaceByName(config.IFace)
if errFace != nil {
panic(errFace)
}
config.Net = iface
addrs, aerr := config.Net.Addrs()
if aerr != nil {
panic(aerr)
}
for _, addr := range addrs {
ip, _, iperr := net.ParseCIDR(addr.String())
if iperr != nil {
log.Println(iperr)
} else {
log.Println(ip)
config.IPs = append(config.IPs, ip)
}
}
log.Printf("%v\n", config)
return config
}