From 070e352912cafeb7329f2410cd010132d162fbe6 Mon Sep 17 00:00:00 2001 From: Elara Musayelyan Date: Mon, 4 Dec 2023 16:51:55 -0800 Subject: [PATCH] Add support for TOML configuration --- config.go | 26 ++++++++++++++++++++------ go.mod | 2 +- go.sum | 6 ++++-- main.go | 4 ++-- 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/config.go b/config.go index cdcc96c..298bf39 100644 --- a/config.go +++ b/config.go @@ -19,22 +19,36 @@ package main import ( + "os" + "github.com/bwmarrin/discordgo" "github.com/caarlos0/env/v10" + "github.com/pelletier/go-toml/v2" ) type Config struct { - Token string `env:"TOKEN,notEmpty"` - DBPath string `env:"DB_PATH" envDefault:"owobot.db"` - Activity Activity `envPrefix:"ACTIVITY_"` + Token string `env:"TOKEN,notEmpty" toml:"token"` + DBPath string `env:"DB_PATH" envDefault:"owobot.db" toml:"db_path"` + Activity Activity `envPrefix:"ACTIVITY_" toml:"activity"` } type Activity struct { - Type discordgo.ActivityType `env:"TYPE" envDefault:"-1"` - Name string `env:"NAME" envDefault:""` + Type discordgo.ActivityType `env:"TYPE" envDefault:"-1" toml:"type"` + Name string `env:"NAME" envDefault:"" toml:"name"` } -func loadEnv() (*Config, error) { +func loadConfig() (*Config, error) { cfg := &Config{} + + fl, err := os.Open("/etc/owobot.toml") + if err != nil { + return nil, err + } + defer fl.Close() + err = toml.NewDecoder(fl).Decode(cfg) + if err != nil { + return nil, err + } + return cfg, env.ParseWithOptions(cfg, env.Options{Prefix: "OWOBOT_"}) } diff --git a/go.mod b/go.mod index 1fd5aa1..9bae0bd 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/bwmarrin/discordgo v0.27.1 github.com/caarlos0/env/v10 v10.0.0 github.com/jmoiron/sqlx v1.3.5 + github.com/pelletier/go-toml/v2 v2.1.0 github.com/rivo/uniseg v0.4.4 github.com/valyala/fasttemplate v1.2.2 go.elara.ws/logger v0.0.0-20230928062203-85e135cf02ae @@ -22,7 +23,6 @@ require ( github.com/mattn/go-isatty v0.0.17 // indirect github.com/mvdan/xurls v1.1.0 // indirect github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect - github.com/stretchr/testify v1.8.1 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect golang.org/x/crypto v0.5.0 // indirect diff --git a/go.sum b/go.sum index 6dae775..a8cae5d 100644 --- a/go.sum +++ b/go.sum @@ -32,6 +32,8 @@ github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwp github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= github.com/mvdan/xurls v1.1.0 h1:OpuDelGQ1R1ueQ6sSryzi6P+1RtBpfQHM8fJwlE45ww= github.com/mvdan/xurls v1.1.0/go.mod h1:tQlNn3BED8bE/15hnSL2HLkDeLWpNPAwtw7wkEq44oU= +github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4= +github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= @@ -44,8 +46,8 @@ github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpE github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= -github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo= diff --git a/main.go b/main.go index c11e8b6..448bb29 100644 --- a/main.go +++ b/main.go @@ -49,9 +49,9 @@ func main() { ctx, cancel := signal.NotifyContext(ctx, os.Interrupt, syscall.SIGTERM) defer cancel() - cfg, err := loadEnv() + cfg, err := loadConfig() if err != nil { - log.Fatal("Error loading environment variables").Err(err).Send() + log.Fatal("Error loading configuration").Err(err).Send() } err = db.Init(ctx, cfg.DBPath+"?_pragma=busy_timeout(30000)")