Skip to content

Commit

Permalink
Abort on errors in configuration file
Browse files Browse the repository at this point in the history
It exits with error code 2.

Closes #24
  • Loading branch information
boivie committed May 24, 2016
1 parent c0fffab commit 2a5d5d2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
26 changes: 19 additions & 7 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,19 @@ func exists(path string) (bool, error) {
return false, err
}

func readFile(conf *Config, fname string) {
func readFile(conf *Config, fname string) error {
if e, _ := exists(fname); e {
log.Infof("Reading configuration file %s", fname)
if _, err := toml.DecodeFile(fname, conf); err != nil {
log.Errorf("Failed to parse configuration file '%s': %v", fname, err)
return err
}
}
return nil
}

func ReadConfig(fname string, dirname string) Config {
var conf = Config{
func ReadConfig(fname string, dirname string) (conf Config, err error) {
conf = Config{
General: ConfigGeneral{
PublicUrl: "http://lovebeat.example.com/",
},
Expand Down Expand Up @@ -134,17 +136,27 @@ func ReadConfig(fname string, dirname string) Config {
Mode: 644, // Reinterpreted as octal below
},
}
readFile(&conf, fname)
err = readFile(&conf, fname)
if err != nil {
return
}
if dirname != "" {
files, err := ioutil.ReadDir(dirname)
var files []os.FileInfo
files, err = ioutil.ReadDir(dirname)
if err == nil {
for _, f := range files {
path := filepath.Join(dirname, f.Name())
readFile(&conf, path)
err = readFile(&conf, path)
if err != nil {
return
}
}
} else {
// This directory is optional
err = nil
}
}
mode, _ := strconv.ParseInt(strconv.FormatInt(int64(conf.Eventlog.Mode), 10), 8, 64)
conf.Eventlog.Mode = os.FileMode(mode)
return conf
return
}
5 changes: 4 additions & 1 deletion lovebeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ func main() {
log.Info(versionStr)
log.Infof("Started on %s, PID %d, running from %s", myName, os.Getpid(), wd)

cfg := config.ReadConfig(*cfgFile, *cfgDir)
cfg, err := config.ReadConfig(*cfgFile, *cfgDir)
if err != nil {
os.Exit(2)
}

notifier := notify.Init(myName, cfg.Notify)

Expand Down

0 comments on commit 2a5d5d2

Please sign in to comment.