2024-03-06 17:26:17 +03:00
|
|
|
package env
|
2020-12-04 02:37:48 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-05-08 19:38:03 +03:00
|
|
|
"fmt"
|
2020-12-04 02:37:48 +03:00
|
|
|
"os"
|
2020-12-13 13:19:24 +03:00
|
|
|
"reflect"
|
2020-12-04 02:37:48 +03:00
|
|
|
"strconv"
|
2020-12-13 13:19:24 +03:00
|
|
|
"strings"
|
|
|
|
|
2024-03-06 17:26:17 +03:00
|
|
|
"dario.cat/mergo"
|
2021-10-26 23:04:03 +03:00
|
|
|
"go.unistack.org/micro/v3/config"
|
|
|
|
rutil "go.unistack.org/micro/v3/util/reflect"
|
2020-12-04 02:37:48 +03:00
|
|
|
)
|
|
|
|
|
2021-09-30 01:40:49 +03:00
|
|
|
var DefaultStructTag = "env"
|
2020-12-04 02:37:48 +03:00
|
|
|
|
|
|
|
type envConfig struct {
|
|
|
|
opts config.Options
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *envConfig) Options() config.Options {
|
|
|
|
return c.opts
|
|
|
|
}
|
|
|
|
|
2020-12-13 13:19:24 +03:00
|
|
|
func (c *envConfig) Init(opts ...config.Option) error {
|
2020-12-04 02:37:48 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&c.opts)
|
|
|
|
}
|
2023-03-14 10:59:06 +03:00
|
|
|
|
2023-03-14 13:58:03 +03:00
|
|
|
if err := config.DefaultBeforeInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
|
2023-03-14 10:59:06 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-14 13:58:03 +03:00
|
|
|
if err := config.DefaultAfterInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
|
2023-03-14 10:59:06 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-04 02:37:48 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-21 16:09:28 +03:00
|
|
|
func (c *envConfig) Load(ctx context.Context, opts ...config.LoadOption) error {
|
2024-01-15 01:58:06 +03:00
|
|
|
if c.opts.SkipLoad != nil && c.opts.SkipLoad(ctx, c) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-14 13:58:03 +03:00
|
|
|
if err := config.DefaultBeforeLoad(ctx, c); err != nil && !c.opts.AllowFail {
|
2021-09-30 01:40:49 +03:00
|
|
|
return err
|
2020-12-18 03:40:34 +03:00
|
|
|
}
|
|
|
|
|
2021-06-21 16:09:28 +03:00
|
|
|
options := config.NewLoadOptions(opts...)
|
|
|
|
mopts := []func(*mergo.Config){mergo.WithTypeCheck}
|
|
|
|
if options.Override {
|
|
|
|
mopts = append(mopts, mergo.WithOverride)
|
|
|
|
}
|
|
|
|
if options.Append {
|
|
|
|
mopts = append(mopts, mergo.WithAppendSlice)
|
|
|
|
}
|
|
|
|
|
2023-03-15 00:06:46 +03:00
|
|
|
dst := c.opts.Struct
|
|
|
|
if options.Struct != nil {
|
|
|
|
dst = options.Struct
|
|
|
|
}
|
|
|
|
|
|
|
|
src, err := rutil.Zero(dst)
|
2020-12-20 00:26:18 +03:00
|
|
|
if err == nil {
|
2021-09-30 01:40:49 +03:00
|
|
|
if err = fillValues(ctx, reflect.ValueOf(src), c.opts.StructTag); err == nil {
|
2023-03-15 00:06:46 +03:00
|
|
|
err = mergo.Merge(dst, src, mopts...)
|
2021-09-30 01:40:49 +03:00
|
|
|
}
|
2020-12-04 21:52:21 +03:00
|
|
|
}
|
|
|
|
|
2023-03-14 13:58:03 +03:00
|
|
|
if err != nil && !c.opts.AllowFail {
|
|
|
|
return err
|
2020-12-18 03:40:34 +03:00
|
|
|
}
|
|
|
|
|
2023-03-14 13:58:03 +03:00
|
|
|
if err := config.DefaultAfterLoad(ctx, c); err != nil && !c.opts.AllowFail {
|
2021-09-30 01:40:49 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-04 21:52:21 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-04 14:00:49 +03:00
|
|
|
func fillValue(ctx context.Context, value reflect.Value, val string) error {
|
2020-12-04 21:52:21 +03:00
|
|
|
switch value.Kind() {
|
2020-12-04 22:41:46 +03:00
|
|
|
case reflect.Map:
|
|
|
|
t := value.Type()
|
|
|
|
nvals := strings.FieldsFunc(val, func(c rune) bool { return c == ',' || c == ';' })
|
|
|
|
if value.IsNil() {
|
|
|
|
value.Set(reflect.MakeMapWithSize(t, len(nvals)))
|
|
|
|
}
|
|
|
|
kt := t.Key()
|
|
|
|
et := t.Elem()
|
|
|
|
for _, nval := range nvals {
|
|
|
|
kv := strings.FieldsFunc(nval, func(c rune) bool { return c == '=' })
|
|
|
|
mkey := reflect.Indirect(reflect.New(kt))
|
|
|
|
mval := reflect.Indirect(reflect.New(et))
|
2021-08-04 14:00:49 +03:00
|
|
|
if err := fillValue(ctx, mkey, kv[0]); err != nil {
|
2020-12-04 22:41:46 +03:00
|
|
|
return err
|
|
|
|
}
|
2021-08-04 14:00:49 +03:00
|
|
|
if err := fillValue(ctx, mval, kv[1]); err != nil {
|
2020-12-04 22:41:46 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
value.SetMapIndex(mkey, mval)
|
|
|
|
}
|
2020-12-04 21:52:21 +03:00
|
|
|
case reflect.Slice, reflect.Array:
|
|
|
|
nvals := strings.FieldsFunc(val, func(c rune) bool { return c == ',' || c == ';' })
|
|
|
|
value.Set(reflect.MakeSlice(reflect.SliceOf(value.Type().Elem()), len(nvals), len(nvals)))
|
|
|
|
for idx, nval := range nvals {
|
|
|
|
nvalue := reflect.Indirect(reflect.New(value.Type().Elem()))
|
2021-08-04 14:00:49 +03:00
|
|
|
if err := fillValue(ctx, nvalue, nval); err != nil {
|
2020-12-04 21:52:21 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
value.Index(idx).Set(nvalue)
|
|
|
|
}
|
|
|
|
case reflect.Bool:
|
|
|
|
v, err := strconv.ParseBool(val)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
value.Set(reflect.ValueOf(v))
|
|
|
|
case reflect.String:
|
|
|
|
value.Set(reflect.ValueOf(val))
|
|
|
|
case reflect.Float32:
|
|
|
|
v, err := strconv.ParseFloat(val, 32)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
value.Set(reflect.ValueOf(float32(v)))
|
|
|
|
case reflect.Float64:
|
|
|
|
v, err := strconv.ParseFloat(val, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
value.Set(reflect.ValueOf(float64(v)))
|
|
|
|
case reflect.Int:
|
|
|
|
v, err := strconv.ParseInt(val, 10, 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
value.Set(reflect.ValueOf(int(v)))
|
|
|
|
case reflect.Int8:
|
|
|
|
v, err := strconv.ParseInt(val, 10, 8)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
value.Set(reflect.ValueOf(v))
|
|
|
|
case reflect.Int16:
|
|
|
|
v, err := strconv.ParseInt(val, 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
value.Set(reflect.ValueOf(int16(v)))
|
|
|
|
case reflect.Int32:
|
|
|
|
v, err := strconv.ParseInt(val, 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
value.Set(reflect.ValueOf(int32(v)))
|
|
|
|
case reflect.Int64:
|
|
|
|
v, err := strconv.ParseInt(val, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
value.Set(reflect.ValueOf(int64(v)))
|
|
|
|
case reflect.Uint:
|
|
|
|
v, err := strconv.ParseUint(val, 10, 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
value.Set(reflect.ValueOf(uint(v)))
|
|
|
|
case reflect.Uint8:
|
|
|
|
v, err := strconv.ParseUint(val, 10, 8)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
value.Set(reflect.ValueOf(uint8(v)))
|
|
|
|
case reflect.Uint16:
|
|
|
|
v, err := strconv.ParseUint(val, 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
value.Set(reflect.ValueOf(uint16(v)))
|
|
|
|
case reflect.Uint32:
|
|
|
|
v, err := strconv.ParseUint(val, 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
value.Set(reflect.ValueOf(uint32(v)))
|
|
|
|
case reflect.Uint64:
|
|
|
|
v, err := strconv.ParseUint(val, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
value.Set(reflect.ValueOf(uint64(v)))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-08 19:38:03 +03:00
|
|
|
func (c *envConfig) setValues(ctx context.Context, valueOf reflect.Value) error {
|
|
|
|
var values reflect.Value
|
|
|
|
|
|
|
|
if valueOf.Kind() == reflect.Ptr {
|
|
|
|
values = valueOf.Elem()
|
|
|
|
} else {
|
|
|
|
values = valueOf
|
|
|
|
}
|
|
|
|
|
|
|
|
if values.Kind() == reflect.Invalid {
|
|
|
|
return config.ErrInvalidStruct
|
|
|
|
}
|
|
|
|
|
|
|
|
fields := values.Type()
|
|
|
|
|
|
|
|
for idx := 0; idx < fields.NumField(); idx++ {
|
|
|
|
field := fields.Field(idx)
|
|
|
|
value := values.Field(idx)
|
|
|
|
if rutil.IsZero(value) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if len(field.PkgPath) != 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch value.Kind() {
|
|
|
|
case reflect.Struct:
|
|
|
|
if err := c.setValues(ctx, value); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
case reflect.Ptr:
|
|
|
|
value = value.Elem()
|
|
|
|
if err := c.setValues(ctx, value); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-11-26 15:38:29 +03:00
|
|
|
tags, ok := field.Tag.Lookup(c.opts.StructTag)
|
2021-05-08 19:38:03 +03:00
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2022-11-26 15:38:29 +03:00
|
|
|
for _, tag := range strings.Split(tags, ",") {
|
|
|
|
if err := os.Setenv(tag, fmt.Sprintf("%v", value.Interface())); err != nil && !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-08 19:38:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-04 14:00:49 +03:00
|
|
|
func fillValues(ctx context.Context, valueOf reflect.Value, structTag string) error {
|
2020-12-04 21:52:21 +03:00
|
|
|
var values reflect.Value
|
|
|
|
|
|
|
|
if valueOf.Kind() == reflect.Ptr {
|
|
|
|
values = valueOf.Elem()
|
|
|
|
} else {
|
2020-12-04 22:41:46 +03:00
|
|
|
values = valueOf
|
2020-12-04 21:52:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if values.Kind() == reflect.Invalid {
|
2020-12-13 13:19:24 +03:00
|
|
|
return config.ErrInvalidStruct
|
2020-12-04 21:52:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fields := values.Type()
|
2020-12-04 02:37:48 +03:00
|
|
|
|
|
|
|
for idx := 0; idx < fields.NumField(); idx++ {
|
|
|
|
field := fields.Field(idx)
|
|
|
|
value := values.Field(idx)
|
|
|
|
if !value.CanSet() {
|
|
|
|
continue
|
|
|
|
}
|
2020-12-04 21:52:21 +03:00
|
|
|
if len(field.PkgPath) != 0 {
|
|
|
|
continue
|
|
|
|
}
|
2020-12-04 22:41:46 +03:00
|
|
|
switch value.Kind() {
|
2020-12-13 13:19:24 +03:00
|
|
|
case reflect.Struct:
|
|
|
|
value.Set(reflect.Indirect(reflect.New(value.Type())))
|
2021-08-04 14:00:49 +03:00
|
|
|
if err := fillValues(ctx, value, structTag); err != nil {
|
2020-12-13 13:19:24 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
case reflect.Ptr:
|
2020-12-04 21:52:21 +03:00
|
|
|
if value.IsNil() {
|
|
|
|
if value.Type().Elem().Kind() != reflect.Struct {
|
|
|
|
// nil pointer to a non-struct: leave it alone
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// nil pointer to struct: create a zero instance
|
|
|
|
value.Set(reflect.New(value.Type().Elem()))
|
|
|
|
}
|
|
|
|
value = value.Elem()
|
2021-08-04 14:00:49 +03:00
|
|
|
if err := fillValues(ctx, value, structTag); err != nil {
|
2020-12-04 21:52:21 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2022-11-26 15:38:29 +03:00
|
|
|
tags, ok := field.Tag.Lookup(structTag)
|
2020-12-04 02:37:48 +03:00
|
|
|
if !ok {
|
2021-04-01 10:56:47 +03:00
|
|
|
continue
|
2020-12-04 02:37:48 +03:00
|
|
|
}
|
2022-11-26 15:38:29 +03:00
|
|
|
|
|
|
|
var eval string
|
|
|
|
for _, tag := range strings.Split(tags, ",") {
|
|
|
|
if val, ok := os.LookupEnv(tag); !ok {
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
eval = val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if eval == "" {
|
2020-12-13 19:29:29 +03:00
|
|
|
continue
|
2020-12-04 02:37:48 +03:00
|
|
|
}
|
2020-12-04 21:52:21 +03:00
|
|
|
|
2022-11-26 15:38:29 +03:00
|
|
|
if err := fillValue(ctx, value, eval); err != nil {
|
2020-12-04 21:52:21 +03:00
|
|
|
return err
|
2020-12-04 02:37:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-21 16:09:28 +03:00
|
|
|
func (c *envConfig) Save(ctx context.Context, opts ...config.SaveOption) error {
|
2024-01-15 01:58:06 +03:00
|
|
|
if c.opts.SkipSave != nil && c.opts.SkipSave(ctx, c) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-15 00:06:46 +03:00
|
|
|
options := config.NewSaveOptions(opts...)
|
|
|
|
|
2023-03-14 13:58:03 +03:00
|
|
|
if err := config.DefaultBeforeSave(ctx, c); err != nil && !c.opts.AllowFail {
|
2021-05-08 19:38:03 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-15 00:06:46 +03:00
|
|
|
dst := c.opts.Struct
|
|
|
|
if options.Struct != nil {
|
|
|
|
dst = options.Struct
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.setValues(ctx, reflect.ValueOf(dst)); err != nil && !c.opts.AllowFail {
|
2023-03-14 13:58:03 +03:00
|
|
|
return err
|
2020-12-18 03:40:34 +03:00
|
|
|
}
|
|
|
|
|
2023-03-14 13:58:03 +03:00
|
|
|
if err := config.DefaultAfterSave(ctx, c); err != nil && !c.opts.AllowFail {
|
2021-09-30 01:40:49 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-04 02:37:48 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *envConfig) String() string {
|
|
|
|
return "env"
|
|
|
|
}
|
|
|
|
|
2021-01-29 16:23:04 +03:00
|
|
|
func (c *envConfig) Name() string {
|
|
|
|
return c.opts.Name
|
|
|
|
}
|
|
|
|
|
2021-08-04 14:00:49 +03:00
|
|
|
func (c *envConfig) Watch(ctx context.Context, opts ...config.WatchOption) (config.Watcher, error) {
|
|
|
|
w := &envWatcher{
|
|
|
|
opts: c.opts,
|
|
|
|
wopts: config.NewWatchOptions(opts...),
|
|
|
|
done: make(chan struct{}),
|
|
|
|
vchan: make(chan map[string]interface{}),
|
|
|
|
echan: make(chan error),
|
|
|
|
}
|
|
|
|
|
|
|
|
go w.run()
|
|
|
|
|
|
|
|
return w, nil
|
|
|
|
}
|
|
|
|
|
2020-12-13 13:19:24 +03:00
|
|
|
func NewConfig(opts ...config.Option) config.Config {
|
2020-12-04 02:37:48 +03:00
|
|
|
options := config.NewOptions(opts...)
|
|
|
|
if len(options.StructTag) == 0 {
|
|
|
|
options.StructTag = DefaultStructTag
|
|
|
|
}
|
2020-12-13 13:19:24 +03:00
|
|
|
return &envConfig{opts: options}
|
2020-12-04 02:37:48 +03:00
|
|
|
}
|