-
Notifications
You must be signed in to change notification settings - Fork 15
/
gokc.go
70 lines (58 loc) · 1.41 KB
/
gokc.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
package main
import (
"flag"
"fmt"
"os"
"strconv"
"github.com/mitchellh/colorstring"
"github.com/yuuki/gokc/log"
"github.com/yuuki/gokc/parser"
)
func setDebugOutputLevel() {
for _, f := range os.Args {
if f == "-D" || f == "--debug" || f == "-debug" {
log.IsDebug = true
}
}
debugEnv := os.Getenv("GOKC_DEBUG")
if debugEnv != "" {
showDebug, err := strconv.ParseBool(debugEnv)
if err != nil {
fmt.Fprintf(os.Stderr, "Error parsing boolean value from GRABENI_DEBUG: %s\n", err)
os.Exit(1)
}
log.IsDebug = showDebug
}
}
func init() {
setDebugOutputLevel()
}
func main() {
var filepath string
var isVersion bool
flag.StringVar(&filepath, "f", "/etc/keepalived/keepalived.conf", "keepalived.conf file path")
flag.BoolVar(&isVersion, "v", false, "print the version")
flag.BoolVar(&log.IsVerbose, "V", false, "verbose log mode")
flag.Parse()
if isVersion {
log.Infof("gokc version %s", version)
os.Exit(0)
}
if filepath == "" {
log.Error("filepath required")
}
file, err := os.Open(filepath)
if err != nil {
log.Error(err)
}
defer file.Close()
if err := parser.Parse(file, filepath); err != nil {
if e, ok := err.(*parser.Error); ok {
msg := colorstring.Color(fmt.Sprintf("[white]%s:%d:%d: [red]%s[reset]", e.Filename, e.Line, e.Column, e.Message))
log.Error(msg)
} else {
log.Error(err)
}
}
log.Infof("gokc: the configuration file %s syntax is ok", filepath)
}