From a778b0fa8cf0fb7f57b741dd07d574178d6f4bfa Mon Sep 17 00:00:00 2001 From: krau <71133316+krau@users.noreply.github.com> Date: Mon, 20 Jan 2025 12:06:47 +0800 Subject: [PATCH] feat: support env vars configuration --- config/viper.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/config/viper.go b/config/viper.go index 42b5622..fa1e31b 100644 --- a/config/viper.go +++ b/config/viper.go @@ -3,13 +3,14 @@ package config import ( "fmt" "os" + "strings" "github.com/spf13/viper" ) type Config struct { Workers int `toml:"workers" mapstructure:"workers"` - Retry int `toml:"retry" mapstructure:"retry"` // Retry times for failed tasks + Retry int `toml:"retry" mapstructure:"retry"` Temp tempConfig `toml:"temp" mapstructure:"temp"` Log logConfig `toml:"log" mapstructure:"log"` @@ -79,7 +80,12 @@ var Cfg *Config func Init() { viper.SetConfigName("config") viper.AddConfigPath(".") + viper.AddConfigPath("/etc/saveany/") viper.SetConfigType("toml") + viper.SetEnvPrefix("SAVEANY") + viper.AutomaticEnv() + replacer := strings.NewReplacer(".", "_") + viper.SetEnvKeyReplacer(replacer) viper.SetDefault("workers", 3) viper.SetDefault("retry", 3)