cmd: simplify default-config-file logic
This commit is contained in:
parent
adbe71525a
commit
9aa80bf772
@ -19,28 +19,31 @@ func main() {
|
||||
|
||||
opts := yatwiki.ServerOptions{}
|
||||
|
||||
cfg, err := ioutil.ReadFile(*configPath)
|
||||
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
opts = *yatwiki.DefaultOptions()
|
||||
if cfg, err := json.MarshalIndent(opts, "", "\t"); err == nil {
|
||||
err := ioutil.WriteFile(*configPath, cfg, 0644)
|
||||
if err != nil {
|
||||
log.Printf("Failed to save default configuration file: %s", err.Error())
|
||||
}
|
||||
// Create default configuration file if necessary
|
||||
if _, err := os.Stat(*configPath); os.IsNotExist(err) {
|
||||
log.Printf("Creating default configuration file at '%s'...\n", *configPath)
|
||||
opts = *yatwiki.DefaultOptions()
|
||||
if cfg, err := json.MarshalIndent(opts, "", "\t"); err == nil {
|
||||
err := ioutil.WriteFile(*configPath, cfg, 0644)
|
||||
if err != nil {
|
||||
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)
|
||||
if err != nil {
|
||||
log.Fatalln(err.Error())
|
||||
@ -52,6 +55,4 @@ func main() {
|
||||
if err != nil {
|
||||
log.Fatalln(err.Error())
|
||||
}
|
||||
|
||||
os.Exit(0)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user