2021-10-26 23:25:18 +03:00
|
|
|
package flag // import "go.unistack.org/micro-config-flag/v3"
|
2021-06-18 16:46:56 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"flag"
|
2021-08-04 15:06:43 +03:00
|
|
|
"fmt"
|
2021-06-18 16:46:56 +03:00
|
|
|
"reflect"
|
2021-06-19 16:00:47 +03:00
|
|
|
"time"
|
2021-06-18 16:46:56 +03:00
|
|
|
|
2021-10-26 23:25:18 +03:00
|
|
|
"go.unistack.org/micro/v3/config"
|
|
|
|
rutil "go.unistack.org/micro/v3/util/reflect"
|
2021-06-18 16:46:56 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-06-19 16:00:47 +03:00
|
|
|
DefaultStructTag = "flag"
|
|
|
|
ErrInvalidValue = errors.New("invalid value specified")
|
|
|
|
DefaultSliceDelim = ","
|
|
|
|
DefaultMapDelim = ","
|
2021-06-18 16:46:56 +03:00
|
|
|
)
|
|
|
|
|
2021-06-19 16:00:47 +03:00
|
|
|
/*
|
|
|
|
var (
|
|
|
|
timeTimeKind = reflect.TypeOf(time.Time{}).Kind()
|
|
|
|
timeDurationKind = reflect.TypeOf(time.Duration(0)).Kind()
|
|
|
|
)
|
|
|
|
*/
|
|
|
|
|
2021-06-18 16:46:56 +03:00
|
|
|
type flagConfig struct {
|
|
|
|
opts config.Options
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *flagConfig) Options() config.Options {
|
|
|
|
return c.opts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *flagConfig) Init(opts ...config.Option) error {
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&c.opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
fields, err := rutil.StructFields(c.opts.Struct)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, sf := range fields {
|
|
|
|
tf, ok := sf.Field.Tag.Lookup(c.opts.StructTag)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2022-03-21 20:00:02 +03:00
|
|
|
fn, fd, fv := getFlagOpts(tf)
|
2021-06-18 16:46:56 +03:00
|
|
|
|
2021-06-19 16:00:47 +03:00
|
|
|
rcheck := true
|
2021-10-27 22:56:15 +03:00
|
|
|
|
2021-06-19 16:00:47 +03:00
|
|
|
switch sf.Value.Interface().(type) {
|
|
|
|
case time.Duration:
|
|
|
|
err = c.flagDuration(sf.Value, fn, fv, fd)
|
|
|
|
rcheck = false
|
|
|
|
case time.Time:
|
|
|
|
err = c.flagTime(sf.Value, fn, fv, fd)
|
|
|
|
rcheck = false
|
|
|
|
}
|
2021-10-27 22:56:15 +03:00
|
|
|
|
2021-06-19 16:00:47 +03:00
|
|
|
if err != nil {
|
2021-09-30 01:55:01 +03:00
|
|
|
c.opts.Logger.Errorf(c.opts.Context, "flag init error: %v", err)
|
|
|
|
if !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2021-06-19 16:00:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if !rcheck {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if sf.Value.Kind() == reflect.Ptr {
|
|
|
|
sf.Value = sf.Value.Elem()
|
|
|
|
}
|
|
|
|
|
2021-06-18 16:46:56 +03:00
|
|
|
switch sf.Value.Kind() {
|
|
|
|
case reflect.String:
|
2021-06-19 16:00:47 +03:00
|
|
|
err = c.flagString(sf.Value, fn, fv, fd)
|
2021-06-18 16:46:56 +03:00
|
|
|
case reflect.Bool:
|
2021-06-19 16:00:47 +03:00
|
|
|
err = c.flagBool(sf.Value, fn, fv, fd)
|
|
|
|
case reflect.Int:
|
|
|
|
err = c.flagInt(sf.Value, fn, fv, fd)
|
|
|
|
case reflect.Int64:
|
|
|
|
err = c.flagInt64(sf.Value, fn, fv, fd)
|
|
|
|
case reflect.Float64:
|
|
|
|
err = c.flagFloat64(sf.Value, fn, fv, fd)
|
|
|
|
case reflect.Uint:
|
|
|
|
err = c.flagUint(sf.Value, fn, fv, fd)
|
|
|
|
case reflect.Uint64:
|
|
|
|
err = c.flagUint64(sf.Value, fn, fv, fd)
|
|
|
|
case reflect.Slice:
|
|
|
|
err = c.flagSlice(sf.Value, fn, fv, fd)
|
|
|
|
case reflect.Map:
|
|
|
|
err = c.flagMap(sf.Value, fn, fv, fd)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2021-09-30 01:55:01 +03:00
|
|
|
c.opts.Logger.Errorf(c.opts.Context, "flag init error: %v", err)
|
|
|
|
if !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2021-06-18 16:46:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-21 16:21:27 +03:00
|
|
|
func (c *flagConfig) Load(ctx context.Context, opts ...config.LoadOption) error {
|
|
|
|
options := config.NewLoadOptions(opts...)
|
|
|
|
_ = options
|
|
|
|
// TODO: allow merge, append and so
|
2021-06-18 16:46:56 +03:00
|
|
|
flag.Parse()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-21 16:21:27 +03:00
|
|
|
func (c *flagConfig) Save(ctx context.Context, opts ...config.SaveOption) error {
|
2021-06-18 16:46:56 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *flagConfig) String() string {
|
|
|
|
return "flag"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *flagConfig) Name() string {
|
|
|
|
return c.opts.Name
|
|
|
|
}
|
|
|
|
|
2021-08-04 15:06:43 +03:00
|
|
|
func (c *flagConfig) Watch(ctx context.Context, opts ...config.WatchOption) (config.Watcher, error) {
|
|
|
|
return nil, fmt.Errorf("not implemented")
|
|
|
|
}
|
|
|
|
|
2021-06-18 16:46:56 +03:00
|
|
|
func NewConfig(opts ...config.Option) config.Config {
|
|
|
|
options := config.NewOptions(opts...)
|
|
|
|
if len(options.StructTag) == 0 {
|
|
|
|
options.StructTag = DefaultStructTag
|
|
|
|
}
|
|
|
|
return &flagConfig{opts: options}
|
|
|
|
}
|