Commit 4c5e0818 authored by Sebastian Stark's avatar Sebastian Stark

make loadConfig() return an error value instead of using log.Fatal()

  - most other situation where log.Fatal() is used should be converted
  to this style
  - overall goal is to make the deferred pid unlock work
parent 7be1dc1b
......@@ -7,6 +7,7 @@ package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
......@@ -116,12 +117,12 @@ Examples:
`, myName)
}
func loadConfig() *Config {
func loadConfig() (*Config, error) {
config := new(Config)
if len(os.Args) > 1 {
subcmd = os.Args[1]
} else {
log.Fatal("no subcommand given")
return nil, errors.New("no subcommand given")
}
switch subcmd {
case "run":
......@@ -172,19 +173,19 @@ func loadConfig() *Config {
schedules.addFromFile(config.SchedFile)
}
if _, ok := schedules[config.Schedule]; ok == false {
log.Fatalln("no such schedule:", config.Schedule)
return nil, fmt.Errorf("no such schedule: %s\n", config.Schedule)
}
path := filepath.Join(config.repository, dataSubdir)
debugf("creating repository: %s", path)
err := os.MkdirAll(path, 00755)
if err != nil {
log.Fatal(err)
return nil, err
}
err = config.WriteCache()
if err != nil {
log.Print("could not write settings cache file:", err)
}
return config
return config, nil
}
case "list":
{
......@@ -213,11 +214,10 @@ func loadConfig() *Config {
}
err := config.ReadCache()
if err != nil {
log.Println("error reading cached settings:", err)
log.Fatalf("this does not look like a %s repository\n", myName)
return nil, fmt.Errorf("error reading repository settings: %s\n", err)
}
debugf("cached config: %v", config)
return config
return config, nil
}
case "help", "-h", "--help":
{
......@@ -234,13 +234,12 @@ func loadConfig() *Config {
if config.SchedFile != "" {
schedules.addFromFile(config.SchedFile)
}
return config
return config, nil
}
default:
{
log.Println("unknown subcommand:", subcmd)
log.Fatalln("try \"help\"")
return nil, fmt.Errorf("unknown subcommand: \"%s\". Try \"help\".", subcmd)
}
}
return nil
return nil, nil
}
......@@ -236,9 +236,9 @@ func subcmdList(cl clock) {
func main() {
logger = log.New(os.Stderr, "", log.Ldate|log.Ltime|log.Lshortfile)
config = loadConfig()
if config == nil {
log.Fatal("no config, don't know what to do!")
var err error
if config, err = loadConfig(); err != nil || config == nil {
log.Fatal(err)
}
if config.NoLogDate {
log.SetFlags(logger.Flags() - log.Ldate - log.Ltime)
......@@ -248,7 +248,7 @@ func main() {
case "run":
log.Printf("%s %s started with pid %d\n", myName, version, os.Getpid())
log.Printf("### Repository: %s, Origin: %s, Schedule: %s\n", config.repository, config.Origin, config.Schedule)
err := subcmdRun()
err = subcmdRun()
if err != nil {
log.Println(err)
os.Exit(1)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment