-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
79 lines (60 loc) · 1.45 KB
/
main.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
package main
import (
"flag"
"fmt"
"os"
"runtime"
"github.com/chuckpreslar/emission"
)
type BindFlags []string
type App struct {
emitter *emission.Emitter
records map[string]string
hostsFile *string
dnsBinds BindFlags
}
func (i *BindFlags) String() string {
return "my string representation"
}
func (i *BindFlags) Set(value string) error {
*i = append(*i, value)
return nil
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU() * 4)
app := new(App)
app.emitter = emission.NewEmitter()
app.records = make(map[string]string)
parseFlags(app)
containerStart := func(domains []string, ip string) {
fmt.Printf("ContainerStart %s\n%s\n\n", domains, ip)
}
containerStop := func(domains []string) {
fmt.Printf("ContainerStop %s\n\n", domains)
}
app.emitter.On("container-start", containerStart)
app.emitter.On("container-stop", containerStop)
go app.startDockerListener()
app.startHostsWriter()
app.startDNSServer()
}
func parseFlags(app *App) {
app.hostsFile = flag.String("hosts-file", "/etc/hosts", "Host file location")
flag.Var(&app.dnsBinds, "dns-bind", "Dns server bind address")
help := flag.Bool("help", false, "Show usage")
flag.Parse()
if *help {
flag.Usage()
os.Exit(1)
}
}
func (app *App) registerDomains(domains []string, ip string) {
for _, domain := range domains {
app.records[domain] = ip
}
}
func (app *App) removeDomains(domains []string) {
for _, domain := range domains {
delete(app.records, domain)
}
}