fix: create config folder if not exists

This commit is contained in:
Markus Pesch 2019-03-01 14:56:49 +01:00
parent 82e82952ed
commit 48b18b61ec
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982

View File

@ -3,6 +3,7 @@ package config
import (
"fmt"
"os"
"path/filepath"
"regexp"
)
@ -31,9 +32,17 @@ func Read(configFile string) (*FluckyConfig, error) {
// Write the configuration into a file, specified by the configuration filepath
func Write(cfg *FluckyConfig, configFile string) error {
if _, err := os.Stat(configFile); os.IsNotExist(err) {
configDir := filepath.Dir(configFile)
err := os.MkdirAll(configDir, os.ModeDir)
if err != nil {
return fmt.Errorf("Can not create config directory %v: %v", configDir, err)
}
}
f, err := os.Create(configFile)
if err != nil {
return err
return fmt.Errorf("Can not write config file: %v", err)
}
defer f.Close()