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"
|
2021-06-03 19:36:48 +09:30
|
|
|
"fmt"
|
2021-06-02 23:42:29 +09:30
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
|
2021-06-03 19:36:48 +09:30
|
|
|
daulog "github.com/tardisx/discord-auto-upload/log"
|
|
|
|
|
2021-06-02 23:42:29 +09:30
|
|
|
"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
|
2021-10-04 15:38:49 +10:30
|
|
|
type ConfigV1 struct {
|
2021-01-31 17:53:32 +10:30
|
|
|
WebHookURL string
|
|
|
|
Path string
|
|
|
|
Watch int
|
|
|
|
Username string
|
|
|
|
NoWatermark bool
|
|
|
|
Exclude string
|
|
|
|
}
|
|
|
|
|
2021-10-04 15:38:49 +10:30
|
|
|
type ConfigV2Watcher struct {
|
|
|
|
WebHookURL string
|
|
|
|
Path string
|
|
|
|
Username string
|
|
|
|
NoWatermark bool
|
|
|
|
Exclude string
|
|
|
|
}
|
|
|
|
|
|
|
|
type ConfigV2 struct {
|
|
|
|
WatchInterval int
|
|
|
|
Version int
|
|
|
|
Watchers []ConfigV2Watcher
|
|
|
|
}
|
|
|
|
|
|
|
|
var Config ConfigV2
|
|
|
|
var configPath string
|
|
|
|
|
|
|
|
func Init() {
|
|
|
|
configPath = defaultConfigPath()
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadOrInit loads the current configuration from the config file, or creates
|
|
|
|
// a new config file if none exists.
|
|
|
|
func LoadOrInit() error {
|
2021-06-03 19:36:48 +09:30
|
|
|
daulog.SendLog(fmt.Sprintf("Trying to load config from %s", configPath), daulog.LogTypeDebug)
|
2021-06-02 23:42:29 +09:30
|
|
|
_, err := os.Stat(configPath)
|
|
|
|
if os.IsNotExist(err) {
|
2021-06-03 19:36:48 +09:30
|
|
|
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)
|
2021-10-04 15:38:49 +10:30
|
|
|
Config.Version = 2
|
|
|
|
Config.WatchInterval = 10
|
|
|
|
return SaveConfig()
|
2021-06-02 23:42:29 +09:30
|
|
|
} else {
|
2021-10-04 15:38:49 +10:30
|
|
|
return LoadConfig()
|
2021-06-02 23:42:29 +09:30
|
|
|
}
|
2021-02-07 11:42:13 +10:30
|
|
|
}
|
|
|
|
|
2021-10-04 15:38:49 +10:30
|
|
|
// LoadConfig will load the configuration from a known-to-exist config file.
|
|
|
|
func LoadConfig() error {
|
|
|
|
data, err := ioutil.ReadFile(configPath)
|
2021-06-02 23:42:29 +09:30
|
|
|
if err != nil {
|
2021-10-04 15:38:49 +10:30
|
|
|
return fmt.Errorf("cannot read config file %s: %s", configPath, err.Error())
|
2021-06-02 23:42:29 +09:30
|
|
|
}
|
|
|
|
err = json.Unmarshal([]byte(data), &Config)
|
|
|
|
if err != nil {
|
2021-10-04 15:38:49 +10:30
|
|
|
return fmt.Errorf("cannot decode config file %s: %s", configPath, err.Error())
|
2021-06-02 23:42:29 +09:30
|
|
|
}
|
2021-10-04 15:38:49 +10:30
|
|
|
return nil
|
2021-02-07 11:42:13 +10:30
|
|
|
}
|
|
|
|
|
2021-10-04 15:38:49 +10:30
|
|
|
func SaveConfig() error {
|
2021-06-03 19:36:48 +09:30
|
|
|
daulog.SendLog("saving configuration", daulog.LogTypeInfo)
|
2021-06-02 23:42:29 +09:30
|
|
|
jsonString, _ := json.Marshal(Config)
|
2021-10-04 15:38:49 +10:30
|
|
|
err := ioutil.WriteFile(configPath, jsonString, os.ModePerm)
|
2021-06-02 23:42:29 +09:30
|
|
|
if err != nil {
|
2021-10-04 15:38:49 +10:30
|
|
|
return fmt.Errorf("cannot save config %s: %s", configPath, err.Error())
|
2021-06-02 23:42:29 +09:30
|
|
|
}
|
2021-10-04 15:38:49 +10:30
|
|
|
return nil
|
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
|
|
|
}
|
|
|
|
|
2021-10-04 15:38:49 +10:30
|
|
|
func defaultConfigPath() 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
|
|
|
}
|