mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-25 09:48:32 +02:00
feat(config): implement environment variable substitution in configuration file reading
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -215,10 +216,23 @@ func (cfg *Config) StartServers(opts ...*StartServersOptions) {
|
||||
}
|
||||
}
|
||||
|
||||
var envRegex = regexp.MustCompile(`\$\{([^}]+)\}`) // e.g. ${CLOUDFLARE_API_KEY}
|
||||
var readFile = os.ReadFile
|
||||
|
||||
func (cfg *Config) readConfigFile() ([]byte, error) {
|
||||
data, err := readFile(common.ConfigPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return envRegex.ReplaceAllFunc(data, func(match []byte) []byte {
|
||||
return strconv.AppendQuote(nil, os.Getenv(string(match[2:len(match)-1])))
|
||||
}), nil
|
||||
}
|
||||
|
||||
func (cfg *Config) load() gperr.Error {
|
||||
const errMsg = "config load error"
|
||||
|
||||
data, err := os.ReadFile(common.ConfigPath)
|
||||
data, err := cfg.readConfigFile()
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
log.Warn().Msg("config file not found, using default config")
|
||||
|
||||
Reference in New Issue
Block a user