2023-05-05 19:57:12 +03:00
|
|
|
package flag // import "go.unistack.org/micro-config-flag/v4"
|
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"
|
2022-04-02 15:12:34 +03:00
|
|
|
"strings"
|
2021-06-19 16:00:47 +03:00
|
|
|
"time"
|
2021-06-18 16:46:56 +03:00
|
|
|
|
2023-05-05 19:57:12 +03:00
|
|
|
"go.unistack.org/micro/v4/config"
|
|
|
|
rutil "go.unistack.org/micro/v4/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
|
2022-04-02 15:12:34 +03:00
|
|
|
name string
|
|
|
|
env string
|
2021-06-18 16:46:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *flagConfig) Options() config.Options {
|
|
|
|
return c.opts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *flagConfig) Init(opts ...config.Option) error {
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&c.opts)
|
|
|
|
}
|
2023-03-15 00:53:26 +03:00
|
|
|
|
|
|
|
if err := config.DefaultBeforeInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-02 15:12:34 +03:00
|
|
|
c.configure()
|
2021-06-18 16:46:56 +03:00
|
|
|
|
|
|
|
fields, err := rutil.StructFields(c.opts.Struct)
|
|
|
|
if err != nil {
|
2023-03-15 00:53:26 +03:00
|
|
|
if !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := config.DefaultAfterInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
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-04-02 15:12:34 +03:00
|
|
|
|
2022-03-21 20:00:02 +03:00
|
|
|
fn, fd, fv := getFlagOpts(tf)
|
2022-04-02 15:12:34 +03:00
|
|
|
if tf, ok = sf.Field.Tag.Lookup(c.env); ok {
|
|
|
|
fd += fmt.Sprintf(" (env %s)", 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
|
|
|
|
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
|
|
|
if !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-15 00:53:26 +03:00
|
|
|
if err := config.DefaultAfterInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-30 01:55:01 +03:00
|
|
|
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
|
|
|
if !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-15 00:53:26 +03:00
|
|
|
|
|
|
|
if err := config.DefaultAfterInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-30 01:55:01 +03:00
|
|
|
return nil
|
2021-06-18 16:46:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-15 00:53:26 +03:00
|
|
|
if err := config.DefaultAfterInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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
|
2023-03-15 00:53:26 +03:00
|
|
|
|
|
|
|
if err := config.DefaultBeforeLoad(ctx, c); err != nil && !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := config.DefaultAfterLoad(ctx, c); err != nil && !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-21 16:21:27 +03:00
|
|
|
// TODO: allow merge, append and so
|
2021-06-18 16:46:56 +03:00
|
|
|
flag.Parse()
|
2023-03-15 00:53:26 +03:00
|
|
|
|
2021-06-18 16:46:56 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-21 16:21:27 +03:00
|
|
|
func (c *flagConfig) Save(ctx context.Context, opts ...config.SaveOption) error {
|
2023-03-15 00:53:26 +03:00
|
|
|
if err := config.DefaultBeforeSave(ctx, c); err != nil && !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := config.DefaultAfterSave(ctx, c); err != nil && !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2022-04-02 15:12:34 +03:00
|
|
|
func (c *flagConfig) usage() {
|
|
|
|
mapDelim := DefaultMapDelim
|
|
|
|
sliceDelim := DefaultSliceDelim
|
|
|
|
|
|
|
|
if c.opts.Context != nil {
|
|
|
|
if d, ok := c.opts.Context.Value(mapDelimKey{}).(string); ok {
|
|
|
|
mapDelim = d
|
|
|
|
}
|
|
|
|
if d, ok := c.opts.Context.Value(sliceDelimKey{}).(string); ok {
|
|
|
|
sliceDelim = d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.name == "" {
|
|
|
|
fmt.Fprintf(c.fset.Output(), "Usage:\n")
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(c.fset.Output(), "Usage of %s:\n", c.name)
|
2021-06-18 16:46:56 +03:00
|
|
|
}
|
2022-04-02 15:12:34 +03:00
|
|
|
|
|
|
|
c.fset.VisitAll(func(f *flag.Flag) {
|
|
|
|
var b strings.Builder
|
|
|
|
fmt.Fprintf(&b, " -%s", f.Name) // Two spaces before -; see next two comments.
|
|
|
|
_, usage := flag.UnquoteUsage(f)
|
|
|
|
name := "value"
|
|
|
|
v := reflect.TypeOf(f.Value).String()
|
|
|
|
b.WriteString(" ")
|
|
|
|
switch v {
|
|
|
|
case "*flag.boolFlag":
|
|
|
|
name = "bool"
|
|
|
|
case "*flag.durationValue":
|
|
|
|
name = "duration"
|
|
|
|
case "*flag.float64Value":
|
|
|
|
name = "float"
|
|
|
|
case "*flag.intValue", "*flag.int64Value":
|
|
|
|
name = "int"
|
|
|
|
case "*flag.stringValue":
|
|
|
|
name = "string"
|
|
|
|
case "*flag.uintValue", "*flag.uint64Value":
|
|
|
|
name = "uint"
|
|
|
|
case "*flag.mapValue":
|
|
|
|
// nv := f.Value.(*mapValue)
|
|
|
|
name = fmt.Sprintf("string key=val with %q as separator", mapDelim)
|
|
|
|
case "*flag.sliceValue":
|
|
|
|
// nv := f.Value.(*sliceValue)
|
|
|
|
name = fmt.Sprintf("string with %q as separator", sliceDelim)
|
|
|
|
}
|
|
|
|
b.WriteString(name)
|
|
|
|
|
|
|
|
// Boolean flags of one ASCII letter are so common we
|
|
|
|
// treat them specially, putting their usage on the same line.
|
|
|
|
if b.Len() <= 4 { // space, space, '-', 'x'.
|
|
|
|
b.WriteString("\t")
|
|
|
|
} else {
|
|
|
|
// Four spaces before the tab triggers good alignment
|
|
|
|
// for both 4- and 8-space tab stops.
|
|
|
|
b.WriteString("\n \t")
|
|
|
|
}
|
|
|
|
b.WriteString(strings.ReplaceAll(usage, "\n", "\n \t"))
|
|
|
|
|
2022-04-02 15:15:49 +03:00
|
|
|
if f.Value.String() == f.DefValue {
|
2022-04-02 15:12:34 +03:00
|
|
|
fmt.Fprintf(&b, " (default %q)", f.DefValue)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(&b, " (default %q current %q)", f.DefValue, f.Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprint(c.fset.Output(), b.String(), "\n")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *flagConfig) configure() {
|
2022-04-01 15:24:56 +03:00
|
|
|
flagSet := flag.CommandLine
|
|
|
|
flagSetName := os.Args[0]
|
|
|
|
flagSetErrorHandling := flag.ExitOnError
|
2022-04-02 15:12:34 +03:00
|
|
|
flagEnv := "env"
|
2022-04-01 15:24:56 +03:00
|
|
|
var flagUsage func()
|
|
|
|
var isSet bool
|
|
|
|
|
2022-04-02 15:12:34 +03:00
|
|
|
if c.opts.Context != nil {
|
|
|
|
if v, ok := c.opts.Context.Value(flagSetNameKey{}).(string); ok {
|
2022-04-01 15:24:56 +03:00
|
|
|
isSet = true
|
|
|
|
flagSetName = v
|
|
|
|
}
|
2022-04-02 15:12:34 +03:00
|
|
|
if v, ok := c.opts.Context.Value(flagSetErrorHandlingKey{}).(flag.ErrorHandling); ok {
|
2022-04-01 15:24:56 +03:00
|
|
|
isSet = true
|
|
|
|
flagSetErrorHandling = v
|
|
|
|
}
|
2022-04-02 15:12:34 +03:00
|
|
|
if v, ok := c.opts.Context.Value(flagSetKey{}).(*flag.FlagSet); ok {
|
2022-04-01 15:24:56 +03:00
|
|
|
flagSet = v
|
|
|
|
}
|
2022-04-02 15:12:34 +03:00
|
|
|
if v, ok := c.opts.Context.Value(flagSetUsageKey{}).(func()); ok {
|
2022-04-01 15:24:56 +03:00
|
|
|
flagUsage = v
|
|
|
|
}
|
2022-04-02 15:12:34 +03:00
|
|
|
if v, ok := c.opts.Context.Value(flagEnvKey{}).(string); ok {
|
|
|
|
flagEnv = v
|
|
|
|
}
|
2022-04-01 15:24:56 +03:00
|
|
|
}
|
2022-04-02 15:12:34 +03:00
|
|
|
c.fset = flagSet
|
2022-04-01 15:24:56 +03:00
|
|
|
|
|
|
|
if isSet {
|
2022-04-02 15:12:34 +03:00
|
|
|
c.fset.Init(flagSetName, flagSetErrorHandling)
|
2022-04-01 15:24:56 +03:00
|
|
|
}
|
|
|
|
if flagUsage != nil {
|
2022-04-02 15:12:34 +03:00
|
|
|
c.fset.Usage = flagUsage
|
|
|
|
} else {
|
|
|
|
c.fset.Usage = c.usage
|
|
|
|
}
|
|
|
|
c.env = flagEnv
|
|
|
|
|
|
|
|
c.name = flagSetName
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-04-02 15:12:34 +03:00
|
|
|
c := &flagConfig{opts: options}
|
|
|
|
c.configure()
|
2022-04-01 15:24:56 +03:00
|
|
|
|
|
|
|
return c
|
2021-06-18 16:46:56 +03:00
|
|
|
}
|