2021-01-31 17:53:32 +10:30
|
|
|
package config
|
|
|
|
|
2021-02-07 11:42:13 +10:30
|
|
|
import (
|
2021-06-02 23:42:29 +09:30
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/mitchellh/go-homedir"
|
2021-02-07 11:42:13 +10:30
|
|
|
)
|
|
|
|
|
2021-01-31 17:53:32 +10:30
|
|
|
// Config for the application
|
|
|
|
var Config struct {
|
|
|
|
WebHookURL string
|
|
|
|
Path string
|
|
|
|
Watch int
|
|
|
|
Username string
|
|
|
|
NoWatermark bool
|
|
|
|
Exclude string
|
|
|
|
}
|
|
|
|
|
2021-06-02 23:42:29 +09:30
|
|
|
const CurrentVersion string = "0.8"
|
2021-02-07 11:42:13 +10:30
|
|
|
|
|
|
|
// Load the current config or initialise with defaults
|
|
|
|
func LoadOrInit() {
|
2021-06-02 23:42:29 +09:30
|
|
|
configPath := configPath()
|
|
|
|
log.Printf("Trying to load from %s\n", configPath)
|
|
|
|
_, err := os.Stat(configPath)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
log.Printf("NOTE: No config file, writing out sample configuration")
|
|
|
|
log.Printf("You need to set the configuration via the web interface")
|
2021-02-07 22:06:19 +10:30
|
|
|
|
2021-06-02 23:42:29 +09:30
|
|
|
Config.WebHookURL = ""
|
|
|
|
Config.Path = homeDir() + string(os.PathSeparator) + "screenshots"
|
|
|
|
Config.Watch = 10
|
|
|
|
SaveConfig()
|
|
|
|
} else {
|
|
|
|
LoadConfig()
|
|
|
|
}
|
2021-02-07 11:42:13 +10:30
|
|
|
}
|
|
|
|
|
|
|
|
func LoadConfig() {
|
2021-06-02 23:42:29 +09:30
|
|
|
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())
|
|
|
|
}
|
2021-02-07 11:42:13 +10:30
|
|
|
}
|
|
|
|
|
|
|
|
func SaveConfig() {
|
2021-06-02 23:42:29 +09:30
|
|
|
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())
|
|
|
|
}
|
2021-02-07 11:42:13 +10:30
|
|
|
}
|
|
|
|
|
|
|
|
func homeDir() string {
|
2021-06-02 23:42:29 +09:30
|
|
|
dir, err := homedir.Dir()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return dir
|
2021-02-07 11:42:13 +10:30
|
|
|
}
|
|
|
|
|
|
|
|
func configPath() string {
|
2021-06-02 23:42:29 +09:30
|
|
|
homeDir := homeDir()
|
|
|
|
return homeDir + string(os.PathSeparator) + ".dau.json"
|
2021-02-07 11:42:13 +10:30
|
|
|
}
|