-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
107 lines (89 loc) · 2 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 (
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/mickep76/log"
"github.com/imc-trading/ifwatch/command/history"
"github.com/imc-trading/ifwatch/command/print"
"github.com/imc-trading/ifwatch/command/publish"
"github.com/imc-trading/ifwatch/command/subscribe"
"github.com/imc-trading/ifwatch/config"
)
var version string
func main() {
// Define usage.
flag.Usage = func() {
fmt.Print(`Usage: ifwatch [OPTIONS] COMMAND
Commands:
print Print network interfaces.
publish Publish events to Kafka.
subscribe Subscribe to and print events from Kafka.
history Show events history from Kafka.
Options:
`)
flag.PrintDefaults()
os.Exit(0)
}
// Define flags.
var printVers, printConf bool
var conf string
flag.BoolVar(&printVers, "version", false, "Print application version.")
flag.StringVar(&conf, "conf", "/etc/ifwatch.toml", "Configuration file.")
flag.BoolVar(&printConf, "print-conf", false, "Print configuration.")
flag.Parse()
if len(os.Args) < 2 {
flag.Usage()
}
// Print version.
if printVers {
fmt.Println(version)
os.Exit(0)
}
// Expand home folder prefix.
if strings.HasPrefix(conf, "~") {
conf = filepath.Join(os.Getenv("HOME"), strings.TrimPrefix(conf, "~"))
}
// New config.
c := config.NewConfig()
// Read config file.
if err := c.Load(conf); err != nil {
log.Fatal(err)
}
// Print config.
if printConf {
c.Print()
os.Exit(0)
}
// Log no color.
if c.LogNoColor {
log.NoColor()
}
// Log no date/time.
if c.LogNoDate {
log.SetFlags(0)
}
// Set log level.
if c.LogLevel != "" {
if err := log.SetLogLevelString(c.LogLevel); err != nil {
log.Fatal(err)
}
}
// Execute command.
var err error
switch flag.Args()[0] {
case "print":
err = print.Print(c, flag.Args()[1:])
case "publish":
err = publish.Publish(c, flag.Args()[1:])
case "subscribe":
subscribe.Subscribe(c, flag.Args()[1:])
case "history":
history.History(c, flag.Args()[1:])
}
if err != nil {
log.Fatal(err)
}
}