From 99e6acf2bb90d050ac605c351fab53d1900313fb Mon Sep 17 00:00:00 2001 From: Nick Date: Fri, 7 Feb 2020 22:21:15 -0500 Subject: [PATCH] Write default configuration file if one is not found. (#298) --- d2core/d2config/d2config.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/d2core/d2config/d2config.go b/d2core/d2config/d2config.go index 76b52f431..15fdbadf8 100644 --- a/d2core/d2config/d2config.go +++ b/d2core/d2config/d2config.go @@ -6,6 +6,7 @@ import ( "log" "os" "path" + "path/filepath" ) var ( @@ -53,6 +54,17 @@ func Initialize() error { singleton = &config return nil } + } else { + log.Printf("configuration file not found, writing default") + os.MkdirAll(filepath.Dir(configPath), os.ModePerm) + configFile, err := os.Create(configPath) + if err == nil { + encoder := json.NewEncoder(configFile) + defer configFile.Close() + encoder.Encode(getDefaultConfiguration()) + } else { + log.Printf("failed to write default configuration (%s)", err) + } } singleton = getDefaultConfiguration()