2020-08-11 03:48:40 -08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-08-15 17:16:52 -08:00
|
|
|
"encoding/json"
|
2020-08-11 03:48:40 -08:00
|
|
|
"fmt"
|
2020-12-26 11:44:53 -09:00
|
|
|
"flag"
|
2020-08-11 03:48:40 -08:00
|
|
|
"os"
|
2020-12-26 11:44:53 -09:00
|
|
|
"strings"
|
2020-08-11 03:48:40 -08:00
|
|
|
)
|
|
|
|
|
2020-08-12 18:16:15 -08:00
|
|
|
func main() {
|
2020-12-26 11:44:53 -09:00
|
|
|
configPath := flag.String("config", "", "path to configuration file")
|
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if *configPath == "" {
|
|
|
|
if _, err := os.Stat("./alrmrc"); err == nil {
|
|
|
|
*configPath = "./alrmrc"
|
|
|
|
}
|
|
|
|
if _, err := os.Stat("/etc/alrmrc"); err == nil {
|
|
|
|
*configPath = "/etc/alrmrc"
|
|
|
|
}
|
|
|
|
if *configPath == "" {
|
|
|
|
fmt.Fprintf(os.Stderr, "Cannot find configuration\n")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2020-08-11 03:48:40 -08:00
|
|
|
}
|
|
|
|
|
2020-12-26 11:44:53 -09:00
|
|
|
config, err := ReadConfig(*configPath)
|
2020-08-11 03:48:40 -08:00
|
|
|
if err != nil {
|
2020-08-15 17:16:52 -08:00
|
|
|
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
|
2020-08-11 03:48:40 -08:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2020-12-26 11:44:53 -09:00
|
|
|
command := strings.ToLower(flag.Arg(0))
|
|
|
|
switch command {
|
|
|
|
case "json":
|
|
|
|
o, err := json.MarshalIndent(config, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "JSON error: %s\n", err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
fmt.Fprintf(os.Stdout, "%s", string(o))
|
2020-08-11 03:48:40 -08:00
|
|
|
|
2020-12-26 11:44:53 -09:00
|
|
|
case "", "config":
|
|
|
|
fmt.Fprintf(os.Stdout, "Config is OK.\n")
|
|
|
|
os.Exit(0)
|
|
|
|
|
|
|
|
default:
|
|
|
|
fmt.Fprintf(os.Stderr, "Unknown command: %s\n", command)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2020-08-11 03:48:40 -08:00
|
|
|
}
|