diff --git a/internal/config/config.go b/internal/config/config.go index 163541dc..5f950189 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -11,6 +11,7 @@ import ( "regexp" "github.com/crazy-max/diun/internal/model" + "github.com/crazy-max/diun/internal/utl" "github.com/imdario/mergo" "github.com/rs/zerolog/log" "gopkg.in/yaml.v2" @@ -88,6 +89,7 @@ func (cfg *Config) validate() error { cfg.Db.Path = "/data/diun.db" } + cfg.Db.Path = utl.GetEnv("DIUN_DB", cfg.Db.Path) if cfg.Db.Path == "" { return errors.New("database path is required") } diff --git a/internal/utl/utl.go b/internal/utl/utl.go index f8f64ac7..9f1baa02 100644 --- a/internal/utl/utl.go +++ b/internal/utl/utl.go @@ -1,6 +1,7 @@ package utl import ( + "os" "regexp" ) @@ -41,3 +42,12 @@ func IsExcluded(s string, excludes []string) bool { } return false } + +// GetEnv retrieves the value of the environment variable named by the key +// or fallback if not found +func GetEnv(key, fallback string) string { + if value, ok := os.LookupEnv(key); ok { + return value + } + return fallback +}