99 lines
2.2 KiB
Go
Raw Normal View History

package config
2021-02-07 11:42:13 +10:30
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
daulog "github.com/tardisx/discord-auto-upload/log"
"github.com/mitchellh/go-homedir"
2021-06-17 18:47:59 +09:30
"golang.org/x/mod/semver"
2021-02-07 11:42:13 +10:30
)
// Config for the application
var Config struct {
WebHookURL string
Path string
Watch int
Username string
NoWatermark bool
Exclude string
}
2021-06-17 18:47:59 +09:30
const CurrentVersion string = "v1.0.0"
2021-02-07 11:42:13 +10:30
// Load the current config or initialise with defaults
func LoadOrInit() {
configPath := configPath()
daulog.SendLog(fmt.Sprintf("Trying to load config from %s", configPath), daulog.LogTypeDebug)
_, err := os.Stat(configPath)
if os.IsNotExist(err) {
daulog.SendLog("NOTE: No config file, writing out sample configuration", daulog.LogTypeInfo)
daulog.SendLog("You need to set the configuration via the web interface", daulog.LogTypeInfo)
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() {
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() {
daulog.SendLog("saving configuration", daulog.LogTypeInfo)
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 {
dir, err := homedir.Dir()
if err != nil {
panic(err)
}
return dir
2021-02-07 11:42:13 +10:30
}
func configPath() string {
homeDir := homeDir()
return homeDir + string(os.PathSeparator) + ".dau.json"
2021-02-07 11:42:13 +10:30
}
2021-06-17 18:47:59 +09:30
func NewVersionAvailable(v string) bool {
if !semver.IsValid(CurrentVersion) {
panic(fmt.Sprintf("my current version '%s' is not valid", CurrentVersion))
}
if !semver.IsValid(v) {
// maybe this should just be a warning
log.Printf("passed in version '%s' is not valid - assuming no new version", v)
return false
}
comp := semver.Compare(v, CurrentVersion)
if comp == 0 {
return false
}
if comp == -1 {
return true
}
return false // they are using a newer one than exists?
}