name-dyndns/main.go

61 lines
1.4 KiB
Go
Raw Normal View History

2015-04-30 00:18:08 -04:00
package main
import (
"flag"
2015-04-30 00:18:08 -04:00
"fmt"
2024-06-24 22:29:06 -04:00
"git.darkcloud.ca/kevin/name-dyndns/api"
"git.darkcloud.ca/kevin/name-dyndns/dyndns"
"git.darkcloud.ca/kevin/name-dyndns/log"
2015-04-30 00:18:08 -04:00
"os"
)
2015-05-04 14:52:55 -04:00
func filterConfigs(configs []api.Config, dev bool) []api.Config {
for i := 0; i < len(configs); i++ {
if configs[i].Dev != dev {
configs = append(configs[:i], configs[i+1:]...)
}
}
return configs
}
2015-04-30 00:18:08 -04:00
func main() {
configPath := flag.String("config", "./config.json", "Specify the configuration file")
2015-05-04 14:52:55 -04:00
daemon := flag.Bool("daemon", false, "Operate in daemon mode.")
dev := flag.Bool("dev", false, "Use development configurations instead.")
2015-05-04 20:03:17 -04:00
logFile := flag.String("log", "", "Specify a logfile. If no file is provided, uses stdout.")
flag.Parse()
2015-05-04 20:03:17 -04:00
var file *os.File
defer file.Close()
if *logFile == "" {
file = os.Stdout
} else {
var err error
file, err = os.OpenFile(*logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
fmt.Println("Could not open log for reading")
os.Exit(1)
}
}
log.Init(file)
configs, err := api.LoadConfigs(*configPath)
2015-04-30 00:18:08 -04:00
if err != nil {
2015-05-04 20:03:17 -04:00
log.Logger.Fatalln("Error loading config:", err)
}
for _, config := range configs {
if config.Domain == "" || len(config.Hostnames) == 0 {
log.Logger.Fatalf("Empty configuration detected. Exiting.")
}
2015-04-30 00:18:08 -04:00
}
2015-05-04 14:52:55 -04:00
configs = filterConfigs(configs, *dev)
2015-05-04 20:03:17 -04:00
log.Logger.Printf("Successfully loaded %d configs\n", len(configs))
dyndns.Run(configs, *daemon)
2015-04-30 00:18:08 -04:00
}