2023-02-02 10:55:24 +01:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
2023-07-26 15:14:49 +02:00
|
|
|
"time"
|
2023-02-02 10:55:24 +01:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
2023-10-30 20:05:00 +01:00
|
|
|
var (
|
|
|
|
interpolationRegExp = regexp.MustCompile(`^\${((?P<varName>\w+)|((?P<varNameWithDefault>\w+):-(?P<defaultValue>[^}]+)))}$`)
|
|
|
|
varNameGroupIndex = interpolationRegExp.SubexpIndex("varName")
|
|
|
|
varNameWithDefaultGroupIndex = interpolationRegExp.SubexpIndex("varNameWithDefault")
|
|
|
|
defaultValueGroupIndex = interpolationRegExp.SubexpIndex("defaultValue")
|
|
|
|
)
|
|
|
|
|
|
|
|
func interpolate(str string, getValueFunc func(name string) string) string {
|
|
|
|
for _, match := range interpolationRegExp.FindAllStringSubmatch(str, -1) {
|
|
|
|
varName := match[varNameWithDefaultGroupIndex]
|
|
|
|
if varName == "" {
|
|
|
|
varName = match[varNameGroupIndex]
|
|
|
|
}
|
|
|
|
|
|
|
|
if varName == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
defaultValue := ""
|
|
|
|
if defaultValueGroupIndex < len(match) {
|
|
|
|
defaultValue = match[defaultValueGroupIndex]
|
|
|
|
}
|
|
|
|
|
|
|
|
str = getValueFunc(varName)
|
|
|
|
if str == "" {
|
|
|
|
str = defaultValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
|
|
|
func interpolateEnv(str string) string {
|
|
|
|
return interpolate(str, os.Getenv)
|
|
|
|
}
|
2023-02-02 10:55:24 +01:00
|
|
|
|
|
|
|
type InterpolatedString string
|
|
|
|
|
|
|
|
func (is *InterpolatedString) UnmarshalYAML(value *yaml.Node) error {
|
|
|
|
var str string
|
|
|
|
|
|
|
|
if err := value.Decode(&str); err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2023-10-30 20:05:00 +01:00
|
|
|
*is = InterpolatedString(interpolateEnv(str))
|
2023-02-02 10:55:24 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type InterpolatedInt int
|
|
|
|
|
|
|
|
func (ii *InterpolatedInt) UnmarshalYAML(value *yaml.Node) error {
|
|
|
|
var str string
|
|
|
|
|
|
|
|
if err := value.Decode(&str); err != nil {
|
|
|
|
return errors.Wrapf(err, "could not decode value '%v' (line '%d') into string", value.Value, value.Line)
|
|
|
|
}
|
|
|
|
|
2023-10-30 20:05:00 +01:00
|
|
|
str = interpolateEnv(str)
|
2023-02-02 10:55:24 +01:00
|
|
|
|
|
|
|
intVal, err := strconv.ParseInt(str, 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "could not parse int '%v', line '%d'", str, value.Line)
|
|
|
|
}
|
|
|
|
|
|
|
|
*ii = InterpolatedInt(int(intVal))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type InterpolatedBool bool
|
|
|
|
|
|
|
|
func (ib *InterpolatedBool) UnmarshalYAML(value *yaml.Node) error {
|
|
|
|
var str string
|
|
|
|
|
|
|
|
if err := value.Decode(&str); err != nil {
|
|
|
|
return errors.Wrapf(err, "could not decode value '%v' (line '%d') into string", value.Value, value.Line)
|
|
|
|
}
|
|
|
|
|
2023-10-30 20:05:00 +01:00
|
|
|
str = interpolateEnv(str)
|
2023-02-02 10:55:24 +01:00
|
|
|
|
|
|
|
boolVal, err := strconv.ParseBool(str)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "could not parse bool '%v', line '%d'", str, value.Line)
|
|
|
|
}
|
|
|
|
|
|
|
|
*ib = InterpolatedBool(boolVal)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type InterpolatedMap map[string]interface{}
|
|
|
|
|
|
|
|
func (im *InterpolatedMap) UnmarshalYAML(value *yaml.Node) error {
|
|
|
|
var data map[string]interface{}
|
|
|
|
|
|
|
|
if err := value.Decode(&data); err != nil {
|
|
|
|
return errors.Wrapf(err, "could not decode value '%v' (line '%d') into map", value.Value, value.Line)
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, value := range data {
|
|
|
|
strVal, ok := value.(string)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-10-30 20:05:00 +01:00
|
|
|
strVal = interpolateEnv(strVal)
|
2023-02-02 10:55:24 +01:00
|
|
|
|
|
|
|
data[key] = strVal
|
|
|
|
}
|
|
|
|
|
|
|
|
*im = data
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type InterpolatedStringSlice []string
|
|
|
|
|
|
|
|
func (iss *InterpolatedStringSlice) UnmarshalYAML(value *yaml.Node) error {
|
|
|
|
var data []string
|
|
|
|
|
|
|
|
if err := value.Decode(&data); err != nil {
|
|
|
|
return errors.Wrapf(err, "could not decode value '%v' (line '%d') into map", value.Value, value.Line)
|
|
|
|
}
|
|
|
|
|
|
|
|
for index, value := range data {
|
2023-10-30 20:05:00 +01:00
|
|
|
value = interpolateEnv(value)
|
2023-02-02 10:55:24 +01:00
|
|
|
|
|
|
|
data[index] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
*iss = data
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-07-26 15:14:49 +02:00
|
|
|
|
|
|
|
type InterpolatedDuration time.Duration
|
|
|
|
|
|
|
|
func (id *InterpolatedDuration) UnmarshalYAML(value *yaml.Node) error {
|
|
|
|
var str string
|
|
|
|
|
|
|
|
if err := value.Decode(&str); err != nil {
|
|
|
|
return errors.Wrapf(err, "could not decode value '%v' (line '%d') into string", value.Value, value.Line)
|
|
|
|
}
|
|
|
|
|
2023-10-30 20:05:00 +01:00
|
|
|
str = interpolateEnv(str)
|
2023-07-26 15:14:49 +02:00
|
|
|
|
|
|
|
duration, err := time.ParseDuration(str)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "could not parse duration '%v', line '%d'", str, value.Line)
|
|
|
|
}
|
|
|
|
|
|
|
|
*id = InterpolatedDuration(duration)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (id *InterpolatedDuration) MarshalYAML() (interface{}, error) {
|
|
|
|
duration := time.Duration(*id)
|
|
|
|
|
|
|
|
return duration.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewInterpolatedDuration(d time.Duration) *InterpolatedDuration {
|
|
|
|
id := InterpolatedDuration(d)
|
|
|
|
return &id
|
|
|
|
}
|