Skip to content

Commit

Permalink
feat(config): add environment variable support (#4)
Browse files Browse the repository at this point in the history
* feat(config): add environment variable support

* fix(config): use double underscore in prefix
  • Loading branch information
s0up4200 authored Jan 4, 2025
1 parent dcd7c8c commit 8c7bd5d
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package config

import (
"fmt"
"strings"

"github.com/autobrr/tqm/logger"
"github.com/autobrr/tqm/stringutils"
"github.com/autobrr/tqm/tracker"

"github.com/knadh/koanf"
"github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/env"
"github.com/knadh/koanf/providers/file"
)

Expand Down Expand Up @@ -40,7 +42,15 @@ func Init(configFilePath string) error {

// load config
if err := K.Load(file.Provider(configFilePath), yaml.Parser()); err != nil {
return fmt.Errorf("load: %w", err)
return fmt.Errorf("load file: %w", err)
}

// load environment variables
if err := K.Load(env.Provider("TQM__", ".", func(s string) string {
return strings.Replace(strings.ToLower(
strings.TrimPrefix(s, "TQM__")), "_", ".", -1)
}), nil); err != nil {
return fmt.Errorf("load env: %w", err)
}

// unmarshal config
Expand Down

0 comments on commit 8c7bd5d

Please # to comment.