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"
|
2022-03-27 13:55:27 +03:00
|
|
|
"os"
|
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 {
|
2022-04-01 15:24:56 +03:00
|
|
|
fset *flag.FlagSet
|
2021-06-18 16:46:56 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-04-01 15:24:56 +03:00
|
|
|
// flag.CommandLine.Init(os.Args[0], flag.ContinueOnError)
|
2021-06-18 16:46:56 +03:00
|
|
|
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
|
|
|
|
2022-03-23 00:57:59 +03:00
|
|
|
if !sf.Value.IsValid() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
vi := sf.Value.Interface()
|
|
|
|
if vi == nil {
|
|
|
|
continue
|
|
|
|
}
|
2022-03-25 14:06:21 +03:00
|
|
|
if f := flag.Lookup(fn); f != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2022-04-01 15:24:56 +03:00
|
|
|
|
|
|
|
fmt.Printf("register %s flag\n", fn)
|
2022-03-23 00:57:59 +03:00
|
|
|
switch vi.(type) {
|
2021-06-19 16:00:47 +03:00
|
|
|
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
|
|
|
|
}
|
2022-04-01 15:24:56 +03:00
|
|
|
flagSet := flag.CommandLine
|
|
|
|
flagSetName := os.Args[0]
|
|
|
|
flagSetErrorHandling := flag.ExitOnError
|
|
|
|
var flagUsage func()
|
|
|
|
var isSet bool
|
|
|
|
|
|
|
|
if options.Context != nil {
|
|
|
|
if v, ok := options.Context.Value(flagSetNameKey{}).(string); ok {
|
|
|
|
isSet = true
|
|
|
|
flagSetName = v
|
|
|
|
}
|
|
|
|
if v, ok := options.Context.Value(flagSetErrorHandlingKey{}).(flag.ErrorHandling); ok {
|
|
|
|
isSet = true
|
|
|
|
flagSetErrorHandling = v
|
|
|
|
}
|
|
|
|
if v, ok := options.Context.Value(flagSetKey{}).(*flag.FlagSet); ok {
|
|
|
|
flagSet = v
|
|
|
|
}
|
|
|
|
if v, ok := options.Context.Value(flagSetUsageKey{}).(func()); ok {
|
|
|
|
flagUsage = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if isSet {
|
|
|
|
flagSet.Init(flagSetName, flagSetErrorHandling)
|
|
|
|
}
|
|
|
|
if flagUsage != nil {
|
|
|
|
flagSet.Usage = flagUsage
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &flagConfig{opts: options, fset: flagSet}
|
|
|
|
|
|
|
|
return c
|
2021-06-18 16:46:56 +03:00
|
|
|
}
|