2023-04-24 20:52:12 +02:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strconv"
|
2023-06-13 03:57:13 +02:00
|
|
|
"time"
|
2023-04-24 20:52:12 +02:00
|
|
|
|
2023-07-10 16:59:00 +02:00
|
|
|
"github.com/drone/envsubst"
|
2023-04-24 20:52:12 +02:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-06-27 14:59:28 +02:00
|
|
|
str, err := envsubst.EvalEnv(str)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
2023-04-24 20:52:12 +02:00
|
|
|
}
|
|
|
|
|
2024-06-27 14:59:28 +02:00
|
|
|
*is = InterpolatedString(str)
|
|
|
|
|
2023-04-24 20:52:12 +02: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)
|
|
|
|
}
|
|
|
|
|
2024-06-27 14:59:28 +02:00
|
|
|
str, err := envsubst.EvalEnv(str)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
2023-04-24 20:52:12 +02: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
|
|
|
|
}
|
|
|
|
|
2023-07-05 20:05:30 +02:00
|
|
|
type InterpolatedFloat float64
|
|
|
|
|
|
|
|
func (ifl *InterpolatedFloat) 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)
|
|
|
|
}
|
|
|
|
|
2024-06-27 14:59:28 +02:00
|
|
|
str, err := envsubst.EvalEnv(str)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
2023-07-05 20:05:30 +02:00
|
|
|
}
|
|
|
|
|
2024-06-27 15:27:14 +02:00
|
|
|
floatVal, err := strconv.ParseFloat(str, 32)
|
2023-07-05 20:05:30 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "could not parse float '%v', line '%d'", str, value.Line)
|
|
|
|
}
|
|
|
|
|
|
|
|
*ifl = InterpolatedFloat(floatVal)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-24 20:52:12 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-06-27 14:59:28 +02:00
|
|
|
str, err := envsubst.EvalEnv(str)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
2023-04-24 20:52:12 +02: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
|
|
|
|
}
|
|
|
|
|
2024-05-24 12:31:09 +02:00
|
|
|
type InterpolatedMap struct {
|
|
|
|
Data map[string]any
|
|
|
|
getEnv func(string) string
|
|
|
|
}
|
2023-04-24 20:52:12 +02:00
|
|
|
|
|
|
|
func (im *InterpolatedMap) UnmarshalYAML(value *yaml.Node) error {
|
2024-05-24 12:31:09 +02:00
|
|
|
var data map[string]any
|
2023-04-24 20:52:12 +02:00
|
|
|
|
|
|
|
if err := value.Decode(&data); err != nil {
|
|
|
|
return errors.Wrapf(err, "could not decode value '%v' (line '%d') into map", value.Value, value.Line)
|
|
|
|
}
|
|
|
|
|
2024-05-24 12:31:09 +02:00
|
|
|
if im.getEnv == nil {
|
|
|
|
im.getEnv = os.Getenv
|
|
|
|
}
|
|
|
|
|
|
|
|
interpolated, err := im.interpolateRecursive(data)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
im.Data = interpolated.(map[string]any)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-06-28 17:44:51 +02:00
|
|
|
func (im InterpolatedMap) interpolateRecursive(data any) (any, error) {
|
2024-05-24 12:31:09 +02:00
|
|
|
switch typ := data.(type) {
|
|
|
|
case map[string]any:
|
|
|
|
for key, value := range typ {
|
|
|
|
value, err := im.interpolateRecursive(value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
typ[key] = value
|
2023-04-24 20:52:12 +02:00
|
|
|
}
|
|
|
|
|
2024-05-24 12:31:09 +02:00
|
|
|
case string:
|
|
|
|
value, err := envsubst.Eval(typ, im.getEnv)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
2023-04-24 20:52:12 +02:00
|
|
|
}
|
|
|
|
|
2024-05-24 12:31:09 +02:00
|
|
|
data = value
|
2023-04-24 20:52:12 +02:00
|
|
|
|
2024-05-24 12:31:09 +02:00
|
|
|
case []any:
|
|
|
|
for idx := range typ {
|
|
|
|
value, err := im.interpolateRecursive(typ[idx])
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
2023-04-24 20:52:12 +02:00
|
|
|
|
2024-05-24 12:31:09 +02:00
|
|
|
typ[idx] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return data, nil
|
2023-04-24 20:52:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2024-06-27 14:59:28 +02:00
|
|
|
value, err := envsubst.EvalEnv(value)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
2023-04-24 20:52:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
data[index] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
*iss = data
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-06-13 03:57:13 +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)
|
|
|
|
}
|
|
|
|
|
2024-06-27 14:59:28 +02:00
|
|
|
str, err := envsubst.EvalEnv(str)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
2023-06-13 03:57:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
duration, err := time.ParseDuration(str)
|
|
|
|
if err != nil {
|
2024-05-29 16:49:05 +02:00
|
|
|
nanoseconds, err := strconv.ParseInt(str, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "could not parse duration '%v', line '%d'", str, value.Line)
|
|
|
|
}
|
|
|
|
|
|
|
|
duration = time.Duration(nanoseconds)
|
2023-06-13 03:57:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
*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
|
|
|
|
}
|