feat: substitute environment variables in config file (#305)

* feat: use envsubst to substitute env variables in config file

* Remove output config to log

* Update readme
This commit is contained in:
Dmytro Bondar
2024-09-23 21:48:11 +02:00
committed by GitHub
parent 3196010a58
commit 2c01f42369
4 changed files with 18 additions and 55 deletions

View File

@@ -2,10 +2,12 @@ package config
import (
"fmt"
"github.com/sirupsen/logrus"
"os"
"time"
"github.com/a8m/envsubst"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
@@ -153,20 +155,14 @@ func GetConfig() (*Config, error) {
}
func loadConfigFile(cfg any, filename string) error {
f, err := os.Open(filename)
data, err := envsubst.ReadFile(filename)
if err != nil {
return err
return fmt.Errorf("envsubst error: %v", err)
}
defer func(f *os.File) {
if err := f.Close(); err != nil {
logrus.Errorf("failed to close configuration file %s: %v", filename, err)
}
}(f)
decoder := yaml.NewDecoder(f)
err = decoder.Decode(cfg)
err = yaml.Unmarshal(data, cfg)
if err != nil {
return err
return fmt.Errorf("yaml error: %v", err)
}
return nil