cmd: simplify default-config-file logic

This commit is contained in:
mappu 2018-04-02 16:53:36 +12:00
parent adbe71525a
commit 9aa80bf772

View File

@ -19,28 +19,31 @@ func main() {
opts := yatwiki.ServerOptions{} opts := yatwiki.ServerOptions{}
cfg, err := ioutil.ReadFile(*configPath) // Create default configuration file if necessary
if _, err := os.Stat(*configPath); os.IsNotExist(err) {
if err != nil { log.Printf("Creating default configuration file at '%s'...\n", *configPath)
if os.IsNotExist(err) { opts = *yatwiki.DefaultOptions()
opts = *yatwiki.DefaultOptions() if cfg, err := json.MarshalIndent(opts, "", "\t"); err == nil {
if cfg, err := json.MarshalIndent(opts, "", "\t"); err == nil { err := ioutil.WriteFile(*configPath, cfg, 0644)
err := ioutil.WriteFile(*configPath, cfg, 0644) if err != nil {
if err != nil { log.Printf("Failed to save default configuration file: %s", err.Error())
log.Printf("Failed to save default configuration file: %s", err.Error())
}
} }
} else {
log.Fatalf("Failed to load configuration file '%s': %s\n", *configPath, err.Error())
}
} else {
log.Printf("Loading configuration from '%s'...\n", *configPath)
err = json.Unmarshal(cfg, &opts)
if err != nil {
log.Fatalf("Failed to parse configuration file: %s\n", err.Error())
} }
} }
// Load configuration
log.Printf("Loading configuration from '%s'...\n", *configPath)
cfg, err := ioutil.ReadFile(*configPath)
if err != nil {
log.Fatalf("Failed to load configuration file '%s': %s\n", *configPath, err.Error())
}
err = json.Unmarshal(cfg, &opts)
if err != nil {
log.Fatalf("Failed to parse configuration file: %s\n", err.Error())
}
//
ws, err := yatwiki.NewWikiServer(&opts) ws, err := yatwiki.NewWikiServer(&opts)
if err != nil { if err != nil {
log.Fatalln(err.Error()) log.Fatalln(err.Error())
@ -52,6 +55,4 @@ func main() {
if err != nil { if err != nil {
log.Fatalln(err.Error()) log.Fatalln(err.Error())
} }
os.Exit(0)
} }