use env if necessary in loading config (#409)

This commit is contained in:
Kevin Wan
2021-01-21 19:33:34 +08:00
committed by GitHub
parent 572b32729f
commit ebec5aafab
6 changed files with 120 additions and 10 deletions

View File

@@ -2,6 +2,7 @@ package conf
import (
"fmt"
"os"
"strconv"
"strings"
"sync"
@@ -32,12 +33,17 @@ type mapBasedProperties struct {
// Loads the properties into a properties configuration instance.
// Returns an error that indicates if there was a problem loading the configuration.
func LoadProperties(filename string) (Properties, error) {
func LoadProperties(filename string, opts ...Option) (Properties, error) {
lines, err := iox.ReadTextLines(filename, iox.WithoutBlank(), iox.OmitWithPrefix("#"))
if err != nil {
return nil, err
}
var opt options
for _, o := range opts {
o(&opt)
}
raw := make(map[string]string)
for i := range lines {
pair := strings.Split(lines[i], "=")
@@ -50,7 +56,11 @@ func LoadProperties(filename string) (Properties, error) {
key := strings.TrimSpace(pair[0])
value := strings.TrimSpace(pair[1])
raw[key] = value
if opt.env {
raw[key] = os.ExpandEnv(value)
} else {
raw[key] = value
}
}
return &mapBasedProperties{