2021-06-18 16:46:56 +03:00
|
|
|
package flag
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"flag"
|
|
|
|
"reflect"
|
2021-06-19 16:00:47 +03:00
|
|
|
"time"
|
2021-06-18 16:46:56 +03:00
|
|
|
|
|
|
|
"github.com/unistack-org/micro/v3/config"
|
|
|
|
rutil "github.com/unistack-org/micro/v3/util/reflect"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
fn, fv, fd := getFlagOpts(tf)
|
|
|
|
|
2021-06-19 16:00:47 +03:00
|
|
|
rcheck := true
|
|
|
|
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
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
return err
|
2021-06-18 16:46:56 +03:00
|
|
|
}
|
2021-06-19 16:00:47 +03:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewConfig(opts ...config.Option) config.Config {
|
|
|
|
options := config.NewOptions(opts...)
|
|
|
|
if len(options.StructTag) == 0 {
|
|
|
|
options.StructTag = DefaultStructTag
|
|
|
|
}
|
|
|
|
return &flagConfig{opts: options}
|
|
|
|
}
|