Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
7911bda9e8 | |||
4b7541ebaa | |||
bac916794d | |||
|
9f57c788bb | ||
3af7068238 | |||
|
c4eb9eaaef | ||
e6db31616a | |||
ec564c021e | |||
3ccffeb42b | |||
|
518816ca7f |
39
flag.go
39
flag.go
@@ -5,6 +5,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -27,6 +28,7 @@ var (
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
type flagConfig struct {
|
type flagConfig struct {
|
||||||
|
fset *flag.FlagSet
|
||||||
opts config.Options
|
opts config.Options
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,6 +46,7 @@ func (c *flagConfig) Init(opts ...config.Option) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// flag.CommandLine.Init(os.Args[0], flag.ContinueOnError)
|
||||||
for _, sf := range fields {
|
for _, sf := range fields {
|
||||||
tf, ok := sf.Field.Tag.Lookup(c.opts.StructTag)
|
tf, ok := sf.Field.Tag.Lookup(c.opts.StructTag)
|
||||||
if !ok {
|
if !ok {
|
||||||
@@ -63,6 +66,8 @@ func (c *flagConfig) Init(opts ...config.Option) error {
|
|||||||
if f := flag.Lookup(fn); f != nil {
|
if f := flag.Lookup(fn); f != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fmt.Printf("register %s flag\n", fn)
|
||||||
switch vi.(type) {
|
switch vi.(type) {
|
||||||
case time.Duration:
|
case time.Duration:
|
||||||
err = c.flagDuration(sf.Value, fn, fv, fd)
|
err = c.flagDuration(sf.Value, fn, fv, fd)
|
||||||
@@ -149,5 +154,37 @@ func NewConfig(opts ...config.Option) config.Config {
|
|||||||
if len(options.StructTag) == 0 {
|
if len(options.StructTag) == 0 {
|
||||||
options.StructTag = DefaultStructTag
|
options.StructTag = DefaultStructTag
|
||||||
}
|
}
|
||||||
return &flagConfig{opts: options}
|
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
|
||||||
}
|
}
|
||||||
|
21
flag_test.go
21
flag_test.go
@@ -2,6 +2,7 @@ package flag
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"flag"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@@ -18,27 +19,29 @@ func TestLoad(t *testing.T) {
|
|||||||
os.Args = append(os.Args, "-time", time.RFC822)
|
os.Args = append(os.Args, "-time", time.RFC822)
|
||||||
os.Args = append(os.Args, "-metadata", "key=20")
|
os.Args = append(os.Args, "-metadata", "key=20")
|
||||||
os.Args = append(os.Args, "-components", "all=info,api=debug")
|
os.Args = append(os.Args, "-components", "all=info,api=debug")
|
||||||
|
os.Args = append(os.Args, "-addr", "33,44")
|
||||||
|
os.Args = append(os.Args, "-badflag", "test")
|
||||||
type NestedConfig struct {
|
type NestedConfig struct {
|
||||||
Value string `flag:"name=nested_value"`
|
Value string `flag:"name=nested_value"`
|
||||||
}
|
}
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Broker string `flag:"name=broker,desc='description with, comma',default='127.0.0.1:9092'"`
|
|
||||||
Verbose bool `flag:"name=verbose,desc='verbose output',default='false'"`
|
|
||||||
Addr []string `flag:"name=addr,desc='addrs',default='127.0.0.1:9092'"`
|
|
||||||
Wait time.Duration `flag:"name=wait,desc='wait time',default='2s'"`
|
|
||||||
Time time.Time `flag:"name=time,desc='some time',default='02 Jan 06 15:04 MST'"`
|
Time time.Time `flag:"name=time,desc='some time',default='02 Jan 06 15:04 MST'"`
|
||||||
Metadata map[string]int `flag:"name=metadata,desc='some meta',default=''"`
|
|
||||||
WithoutDefault string `flag:"name=without_default,desc='with'"`
|
|
||||||
WithoutDesc string `flag:"name=without_desc,default='without_default'"`
|
|
||||||
WithoutAll string `flag:"name=without_all"`
|
|
||||||
Components map[string]string `flag:"name=components,desc='components logging'"`
|
Components map[string]string `flag:"name=components,desc='components logging'"`
|
||||||
|
Metadata map[string]int `flag:"name=metadata,desc='some meta',default=''"`
|
||||||
Nested *NestedConfig
|
Nested *NestedConfig
|
||||||
|
Broker string `flag:"name=broker,desc='description with, comma',default='127.0.0.1:9092'"`
|
||||||
|
WithoutDesc string `flag:"name=without_desc,default='without_default'"`
|
||||||
|
WithoutAll string `flag:"name=without_all"`
|
||||||
|
WithoutDefault string `flag:"name=without_default,desc='with'"`
|
||||||
|
Addr []string `flag:"name=addr,desc='addrs',default='127.0.0.1:9092'"`
|
||||||
|
Wait time.Duration `flag:"name=wait,desc='wait time',default='2s'"`
|
||||||
|
Verbose bool `flag:"name=verbose,desc='verbose output',default='false'"`
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
cfg := &Config{Nested: &NestedConfig{}}
|
cfg := &Config{Nested: &NestedConfig{}}
|
||||||
|
|
||||||
c := NewConfig(config.Struct(cfg), TimeFormat(time.RFC822))
|
c := NewConfig(config.Struct(cfg), TimeFormat(time.RFC822), FlagErrorHandling(flag.ContinueOnError))
|
||||||
if err := c.Init(); err != nil {
|
if err := c.Init(); err != nil {
|
||||||
t.Fatalf("init failed: %v", err)
|
t.Fatalf("init failed: %v", err)
|
||||||
}
|
}
|
||||||
|
2
go.mod
2
go.mod
@@ -2,4 +2,4 @@ module go.unistack.org/micro-config-flag/v3
|
|||||||
|
|
||||||
go 1.16
|
go 1.16
|
||||||
|
|
||||||
require go.unistack.org/micro/v3 v3.9.1
|
require go.unistack.org/micro/v3 v3.9.7
|
||||||
|
6
go.sum
6
go.sum
@@ -22,7 +22,7 @@ github.com/envoyproxy/go-control-plane v0.9.9-0.20210512163311-63b5d3c536b0/go.m
|
|||||||
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
|
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
|
||||||
github.com/flowstack/go-jsonschema v0.1.1/go.mod h1:yL7fNggx1o8rm9RlgXv7hTBWxdBM0rVwpMwimd3F3N0=
|
github.com/flowstack/go-jsonschema v0.1.1/go.mod h1:yL7fNggx1o8rm9RlgXv7hTBWxdBM0rVwpMwimd3F3N0=
|
||||||
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
|
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
|
||||||
github.com/golang-jwt/jwt/v4 v4.4.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg=
|
github.com/golang-jwt/jwt/v4 v4.4.1/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
|
||||||
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
|
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
|
||||||
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
|
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
|
||||||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||||
@@ -73,8 +73,8 @@ github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQ
|
|||||||
go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI=
|
go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI=
|
||||||
go.unistack.org/micro-proto/v3 v3.2.7 h1:zG6d69kHc+oij2lwQ3AfrCgdjiEVRG2A7TlsxjusWs4=
|
go.unistack.org/micro-proto/v3 v3.2.7 h1:zG6d69kHc+oij2lwQ3AfrCgdjiEVRG2A7TlsxjusWs4=
|
||||||
go.unistack.org/micro-proto/v3 v3.2.7/go.mod h1:ZltVWNECD5yK+40+OCONzGw4OtmSdTpVi8/KFgo9dqM=
|
go.unistack.org/micro-proto/v3 v3.2.7/go.mod h1:ZltVWNECD5yK+40+OCONzGw4OtmSdTpVi8/KFgo9dqM=
|
||||||
go.unistack.org/micro/v3 v3.9.1 h1:C1TEP8Eooqr5vlNLoZ3LCpNqk+XuCiC1yBHol+qJZ4E=
|
go.unistack.org/micro/v3 v3.9.7 h1:ROx4X6nP9rE72lKgawyys/8VdNXqU65fb3he7d00iZk=
|
||||||
go.unistack.org/micro/v3 v3.9.1/go.mod h1:7ssIWk+PJXvb2nSl8NUnQRs32JJEId2IDi9PobrQlKo=
|
go.unistack.org/micro/v3 v3.9.7/go.mod h1:E1pmj0cNC7P+QVX1sElRBMiNaQVTjDoEtYlbqZY84c4=
|
||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||||
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
||||||
|
30
options.go
30
options.go
@@ -1,6 +1,8 @@
|
|||||||
package flag
|
package flag
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"flag"
|
||||||
|
|
||||||
"go.unistack.org/micro/v3/config"
|
"go.unistack.org/micro/v3/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -24,3 +26,31 @@ type timeFormatKey struct{}
|
|||||||
func TimeFormat(s string) config.Option {
|
func TimeFormat(s string) config.Option {
|
||||||
return config.SetOption(timeFormatKey{}, s)
|
return config.SetOption(timeFormatKey{}, s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type flagSetKey struct{}
|
||||||
|
|
||||||
|
// FlagSet set flag set name
|
||||||
|
func FlagSet(f *flag.FlagSet) config.Option {
|
||||||
|
return config.SetOption(flagSetKey{}, f)
|
||||||
|
}
|
||||||
|
|
||||||
|
type flagSetNameKey struct{}
|
||||||
|
|
||||||
|
// FlagSetName set flag set name
|
||||||
|
func FlagSetName(n string) config.Option {
|
||||||
|
return config.SetOption(flagSetNameKey{}, n)
|
||||||
|
}
|
||||||
|
|
||||||
|
type flagSetErrorHandlingKey struct{}
|
||||||
|
|
||||||
|
// FlagErrorHandling set flag set error handling
|
||||||
|
func FlagErrorHandling(eh flag.ErrorHandling) config.Option {
|
||||||
|
return config.SetOption(flagSetErrorHandlingKey{}, eh)
|
||||||
|
}
|
||||||
|
|
||||||
|
type flagSetUsageKey struct{}
|
||||||
|
|
||||||
|
// FlagUsage set flag set usage func
|
||||||
|
func FlagUsage(fn func()) config.Option {
|
||||||
|
return config.SetOption(flagSetUsageKey{}, fn)
|
||||||
|
}
|
||||||
|
4
util.go
4
util.go
@@ -10,9 +10,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type mapValue struct {
|
type mapValue struct {
|
||||||
v reflect.Value
|
|
||||||
delim string
|
delim string
|
||||||
def string
|
def string
|
||||||
|
v reflect.Value
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v mapValue) String() string {
|
func (v mapValue) String() string {
|
||||||
@@ -67,9 +67,9 @@ func (v mapValue) Set(s string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type sliceValue struct {
|
type sliceValue struct {
|
||||||
v reflect.Value
|
|
||||||
delim string
|
delim string
|
||||||
def string
|
def string
|
||||||
|
v reflect.Value
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v sliceValue) String() string {
|
func (v sliceValue) String() string {
|
||||||
|
Reference in New Issue
Block a user