rsslair/config.go

65 lines
1.4 KiB
Go

package main
import (
"os"
"github.com/17xande/configdir"
"github.com/pelletier/go-toml"
)
type AppConfig struct {
SyncInterval int // Sync time in minutes
ShouldCache bool // Should we cache the rss feeds
Scripts []string // Scripts to execute
}
func createDefaultConfig(configPath string) {
config := AppConfig{
SyncInterval: 60,
ShouldCache: true,
Scripts: []string{"all"},
}
file, err := os.Create(configPath + "/config.toml")
if err != nil {
logger.Fatal(err)
}
defer file.Close()
encoder := toml.NewEncoder(file)
err = encoder.Encode(config)
if err != nil {
logger.Fatal(err)
}
}
func printConfig() {
logger.Debug("Loaded config:")
logger.Debug("Sync time: ", appConfig.SyncInterval)
logger.Debug("Should cache: ", appConfig.ShouldCache)
logger.Debug("Scripts: ", appConfig.Scripts)
}
func loadAppConfig() {
configPath := configdir.LocalConfig("rsslair")
err := configdir.MakePath(configPath)
if err != nil {
logger.Fatal(err)
}
if _, err := os.Stat(configPath + "/config.toml"); os.IsNotExist(err) {
logger.Info("Config file not found, creating default config")
createDefaultConfig(configPath)
}
file, err := os.Open(configPath + "/config.toml")
if err != nil {
logger.Fatal(err)
}
defer file.Close()
decoder := toml.NewDecoder(file)
err = decoder.Decode(&appConfig)
if err != nil {
logger.Fatal(err)
}
printConfig()
}