/
main.go
107 lines (89 loc) · 2.05 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
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
package main
import (
"os"
"strconv"
"time"
"golang.org/x/crypto/ssh/terminal"
"github.com/codegangsta/cli"
"github.com/ian-kent/go-log/layout"
"github.com/ian-kent/go-log/levels"
"github.com/ian-kent/go-log/log"
)
var version string
func init() {
logger := log.Logger()
if terminal.IsTerminal(int(os.Stdout.Fd())) {
logger.Appender().SetLayout(layout.Pattern("%p: %m"))
} else {
logger.Appender().SetLayout(layout.Pattern("%d %p: %m"))
}
logger.SetLevel(levels.DEBUG)
}
func main() {
app := cli.NewApp()
app.Name = "necd"
app.Usage = "Network Environment Change Detector"
app.Version = version
app.Action = actionMain
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "daemon, d",
EnvVar: "DAEMON",
Usage: "Daemonize",
},
}
app.RunAndExitOnError()
}
func actionMain(c *cli.Context) {
if c.Bool("daemon") {
if err := Daemonize("necd"); err != nil {
log.Fatalf("Failed to daemonize: %v", err)
}
} else {
KeepAlive(runLoop)
}
}
func runLoop() {
conf, err := readConf()
AssertNoErr(err, "Failed to read config file")
log.Debug("Global conf: %#v", conf)
nif := conf["config"]["if"]
interval := atoi(conf["config"]["interval"], 5)
for name := range notifyOnChange(nif, interval) {
log.Info("Network changed: %s", name)
if section, ok := conf["ssid:"+name]; ok {
log.Debug("Found section: %v", section)
ApplyCmds(section)
} else {
log.Debug("Undefined section for: %s", name)
}
}
}
func notifyOnChange(nif string, interval int) <-chan (string) {
previousName := NetworkName(nif)
c := make(chan string, 1)
if previousName != "" {
c <- previousName
}
go func() {
for _ = range time.Tick(time.Duration(interval) * time.Second) {
currentName := NetworkName(nif)
log.Debug("Current name: %s", currentName)
if previousName != currentName && currentName != "" {
previousName = currentName
c <- currentName
}
}
}()
return c
}
func atoi(a string, def int) int {
if a == "" {
return def
}
i, err := strconv.Atoi(a)
if err != nil {
log.Fatalf("Failed to parse value: %s", a)
}
return i
}