update
This commit is contained in:
111
internal/config/config.go
Normal file
111
internal/config/config.go
Normal file
@ -0,0 +1,111 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"git.rouggy.com/rouggy/RaceBot/internal/domain"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"text/template"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var configTemplate = `# config.toml
|
||||
host = "127.0.0.1"
|
||||
port = "3000"
|
||||
# TMDbApiKey is required
|
||||
tmdbApiKey = ""
|
||||
dbName = "racer.db"
|
||||
uploadFolder = "/home/rouggy/torrents/rtorrent/Race"
|
||||
`
|
||||
|
||||
func writeConfig(configPath string, configFile string) error {
|
||||
cfgPath := filepath.Join(configPath, configFile)
|
||||
|
||||
// check if configPath exists, if not create it
|
||||
if _, err := os.Stat(configPath); errors.Is(err, os.ErrNotExist) {
|
||||
err := os.MkdirAll(configPath, os.ModePerm)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// check if config exists, if not create it
|
||||
if _, err := os.Stat(cfgPath); errors.Is(err, os.ErrNotExist) {
|
||||
|
||||
f, err := os.Create(cfgPath)
|
||||
if err != nil { // perm 0666
|
||||
// handle failed create
|
||||
log.Printf("error creating file: %q", err)
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
// setup text template to inject variables into
|
||||
_, err = template.New("config").Parse(configTemplate)
|
||||
if err != nil {
|
||||
log.Println("Could not create config file:", err)
|
||||
}
|
||||
|
||||
return f.Sync()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type AppConfig struct {
|
||||
Config *domain.Config
|
||||
}
|
||||
|
||||
func New(configPath string) *AppConfig {
|
||||
c := &AppConfig{}
|
||||
c.defaults()
|
||||
c.Config.ConfigPath = configPath
|
||||
|
||||
c.load(configPath)
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *AppConfig) defaults() {
|
||||
c.Config = &domain.Config{
|
||||
Host: "127.0.0.1",
|
||||
Port: "3000",
|
||||
TMDbApiKey: "",
|
||||
DbName: "racer.db",
|
||||
UploadFolder: "/home/rouggy/torrents/rtorrent/Race",
|
||||
}
|
||||
}
|
||||
|
||||
func (c *AppConfig) load(configPath string) {
|
||||
|
||||
viper.SetConfigType("toml")
|
||||
|
||||
// clean trailing slash from configPath
|
||||
configPath = path.Clean(configPath)
|
||||
|
||||
if configPath != "" {
|
||||
if err := writeConfig(configPath, "config.toml"); err != nil {
|
||||
log.Printf("write error: %q", err)
|
||||
}
|
||||
|
||||
viper.SetConfigFile(path.Join(configPath, "config.toml"))
|
||||
} else {
|
||||
viper.SetConfigName("config")
|
||||
|
||||
// Search config in directories
|
||||
viper.AddConfigPath("./config")
|
||||
}
|
||||
|
||||
// read config
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
log.Printf("config read error: %q", err)
|
||||
}
|
||||
|
||||
if err := viper.Unmarshal(&c.Config); err != nil {
|
||||
log.Fatalf("Could not unmarshal config file: %v", viper.ConfigFileUsed())
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user