improved command line argument handling, migrated json to config at higher debug levels

This commit is contained in:
Christopher Ramey 2021-01-11 06:28:14 -09:00
parent 233aec7010
commit 951bee15fd
2 changed files with 57 additions and 47 deletions

View File

@ -195,11 +195,11 @@ func (p *Parser) setState(state int) {
p.lastcheck = nil p.lastcheck = nil
} }
if p.DebugLevel > 0 { if p.DebugLevel > 1 {
fmt.Printf("Parser state: %s", p.stateName()) fmt.Printf("Parser state: %s", p.stateName())
} }
p.states = append(p.states, state) p.states = append(p.states, state)
if p.DebugLevel > 0 { if p.DebugLevel > 1 {
fmt.Printf(" -> %s\n", p.stateName()) fmt.Printf(" -> %s\n", p.stateName())
} }
} }

100
main.go
View File

@ -1,18 +1,19 @@
package main package main
import ( import (
"alrm/config"
"encoding/json" "encoding/json"
"fmt"
"flag" "flag"
"fmt"
"os" "os"
"strings" "strings"
"alrm/config"
) )
func main() { func main() {
cfgPath := flag.String("c", "", "path to configuration file") cfgPath := flag.String("c", "", "path to configuration file")
debuglvl := flag.Int("d", 0, "debug level") debuglvl := flag.Int("d", 0, "debug level")
flag.Usage = printUsage
flag.Parse() flag.Parse()
if *cfgPath == "" { if *cfgPath == "" {
@ -23,63 +24,72 @@ func main() {
*cfgPath = "/etc/alrmrc" *cfgPath = "/etc/alrmrc"
} }
if *cfgPath == "" { if *cfgPath == "" {
fmt.Fprintf(os.Stderr, "Cannot find configuration\n") fmt.Fprintf(os.Stderr, "cannot find configuration\n")
os.Exit(1) os.Exit(1)
} }
} }
command := strings.ToLower(flag.Arg(0)) command := strings.ToLower(flag.Arg(0))
switch command { switch command {
case "json": case "config":
cfg, err := config.ReadConfig(*cfgPath, *debuglvl) cfg, err := config.ReadConfig(*cfgPath, *debuglvl)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error()) fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(1) os.Exit(1)
} }
if *debuglvl > 0 {
o, err := json.MarshalIndent(cfg, "", " ") o, err := json.MarshalIndent(cfg, "", " ")
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "JSON error: %s\n", err.Error()) fmt.Fprintf(os.Stderr, "JSON error: %s\n", err.Error())
os.Exit(1) os.Exit(1)
} }
fmt.Fprintf(os.Stdout, "%s\n", string(o)) fmt.Printf("%s\n", string(o))
}
case "config", "": fmt.Printf("config is OK\n")
_, err := config.ReadConfig(*cfgPath, *debuglvl)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(1)
}
fmt.Fprintf(os.Stdout, "Config is OK.\n")
case "check": case "check":
tn := flag.Arg(1) tn := flag.Arg(1)
if tn == "" { if tn == "" {
fmt.Fprintf(os.Stderr, "test requires a host or group\n") fmt.Fprintf(os.Stderr, "check requires a host or group\n")
os.Exit(1)
}
cfg, err := config.ReadConfig(*cfgPath, 0)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(1)
}
group, exists := cfg.Groups[tn]
if !exists {
fmt.Fprintf(os.Stderr, "group or host is not defined\n")
os.Exit(1)
}
err = group.Check(*debuglvl)
if err != nil {
fmt.Fprintf(os.Stderr, "Check failed: %s\n", err.Error())
os.Exit(1)
}
fmt.Fprintf(os.Stdout, "Check successful\n")
default:
fmt.Fprintf(os.Stderr, "Unknown command: %s\n", command)
os.Exit(1) os.Exit(1)
}
cfg, err := config.ReadConfig(*cfgPath, 0)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(1)
}
group, exists := cfg.Groups[tn]
if !exists {
fmt.Fprintf(os.Stderr, "group or host is not defined\n")
os.Exit(1)
}
err = group.Check(*debuglvl)
if err != nil {
fmt.Fprintf(os.Stderr, "check failed: %s\n", err.Error())
os.Exit(1)
}
fmt.Printf("check successful\n")
case "":
printUsage()
default:
fmt.Fprintf(os.Stderr, "unknown command: %s\n", command)
os.Exit(1)
} }
} }
func printUsage() {
fmt.Printf("Usage: %s [args] <action> ...\n", os.Args[0])
fmt.Printf("Arguments:\n")
fmt.Printf(" -c <path> : path to configuration file\n")
fmt.Printf(" -d <level> : debug level (0-9, higher for more debugging)\n")
fmt.Printf("Actions:\n")
fmt.Printf(" verify configuration: %s [args] config\n", os.Args[0])
fmt.Printf(" run a check manually: %s [args] check <host/group>\n", os.Args[0])
}