diff --git a/config/config.go b/config/config.go index 1e8e82a..fad5885 100644 --- a/config/config.go +++ b/config/config.go @@ -1,5 +1,13 @@ package config +import ( + "github.com/mitchellh/go-homedir" + "log" + "os" + "encoding/json" + "io/ioutil" +) + // Config for the application var Config struct { WebHookURL string @@ -11,3 +19,52 @@ var Config struct { } const CurrentVersion string = "0.6" + +// Load the current config or initialise with defaults +func LoadOrInit() { + configPath := configPath() + log.Printf("Trying to load from %s\n", configPath) + _, err := os.Stat(configPath) + if os.IsNotExist(err) { + log.Printf("Initialising empty config") + Config.WebHookURL = "" + Config.Path = homeDir() + string(os.PathSeparator) + "screenshots" + Config.Watch = 10 + SaveConfig() + } else { + LoadConfig() + } +} + +func LoadConfig() { + path := configPath() + data, err := ioutil.ReadFile(path) + if err != nil { + log.Fatalf("cannot read config file %s: %s", path, err.Error()) + } + err = json.Unmarshal([]byte(data), &Config) + if err != nil { + log.Fatalf("cannot decode config file %s: %s", path, err.Error()) + } +} + +func SaveConfig() { + log.Print("saving configuration") + path := configPath() + jsonString, _ := json.Marshal(Config) + err := ioutil.WriteFile(path, jsonString, os.ModePerm) + if (err != nil) { + log.Fatalf("Cannot save config %s: %s", path, err.Error()) + } +} + +func homeDir() string { + dir, err := homedir.Dir() + if (err != nil) { panic (err) } + return dir; +} + +func configPath() string { + homeDir := homeDir() + return homeDir + string(os.PathSeparator) + ".dau.json" +} diff --git a/data/config.html b/data/config.html index 2bbddc5..5cc2579 100644 --- a/data/config.html +++ b/data/config.html @@ -70,8 +70,9 @@
- - + + + 😭
@@ -87,7 +88,18 @@