Compare commits
9 Commits
v3.8.11
...
ec3ae6b934
| Author | SHA1 | Date | |
|---|---|---|---|
| ec3ae6b934 | |||
| e712519f44 | |||
| a8d5f21394 | |||
| 5fef84faa0 | |||
| 600608bda5 | |||
| 728f1ca453 | |||
| c325b0a760 | |||
| 05cb25c2e1 | |||
| bc599cddb3 |
31
flag.go
31
flag.go
@@ -1,4 +1,4 @@
|
|||||||
package flag // import "go.unistack.org/micro-config-flag/v3"
|
package flag // import "go.unistack.org/micro-config-flag/v4"
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -10,8 +10,9 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"go.unistack.org/micro/v3/config"
|
"go.unistack.org/micro/v4/config"
|
||||||
rutil "go.unistack.org/micro/v3/util/reflect"
|
"go.unistack.org/micro/v4/options"
|
||||||
|
rutil "go.unistack.org/micro/v4/util/reflect"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -39,9 +40,13 @@ func (c *flagConfig) Options() config.Options {
|
|||||||
return c.opts
|
return c.opts
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *flagConfig) Init(opts ...config.Option) error {
|
func (c *flagConfig) Init(opts ...options.Option) error {
|
||||||
|
var err error
|
||||||
|
|
||||||
for _, o := range opts {
|
for _, o := range opts {
|
||||||
o(&c.opts)
|
if err = o(&c.opts); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := config.DefaultBeforeInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
|
if err := config.DefaultBeforeInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
|
||||||
@@ -155,7 +160,11 @@ func (c *flagConfig) Init(opts ...config.Option) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *flagConfig) Load(ctx context.Context, opts ...config.LoadOption) error {
|
func (c *flagConfig) Load(ctx context.Context, opts ...options.Option) error {
|
||||||
|
if c.opts.SkipLoad != nil && c.opts.SkipLoad(ctx, c) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
options := config.NewLoadOptions(opts...)
|
options := config.NewLoadOptions(opts...)
|
||||||
_ = options
|
_ = options
|
||||||
|
|
||||||
@@ -173,7 +182,11 @@ func (c *flagConfig) Load(ctx context.Context, opts ...config.LoadOption) error
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *flagConfig) Save(ctx context.Context, opts ...config.SaveOption) error {
|
func (c *flagConfig) Save(ctx context.Context, opts ...options.Option) error {
|
||||||
|
if c.opts.SkipSave != nil && c.opts.SkipSave(ctx, c) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
if err := config.DefaultBeforeSave(ctx, c); err != nil && !c.opts.AllowFail {
|
if err := config.DefaultBeforeSave(ctx, c); err != nil && !c.opts.AllowFail {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -193,7 +206,7 @@ func (c *flagConfig) Name() string {
|
|||||||
return c.opts.Name
|
return c.opts.Name
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *flagConfig) Watch(ctx context.Context, opts ...config.WatchOption) (config.Watcher, error) {
|
func (c *flagConfig) Watch(ctx context.Context, opts ...options.Option) (config.Watcher, error) {
|
||||||
return nil, fmt.Errorf("not implemented")
|
return nil, fmt.Errorf("not implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -308,7 +321,7 @@ func (c *flagConfig) configure() {
|
|||||||
c.name = flagSetName
|
c.name = flagSetName
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConfig(opts ...config.Option) config.Config {
|
func NewConfig(opts ...options.Option) config.Config {
|
||||||
options := config.NewOptions(opts...)
|
options := config.NewOptions(opts...)
|
||||||
if len(options.StructTag) == 0 {
|
if len(options.StructTag) == 0 {
|
||||||
options.StructTag = DefaultStructTag
|
options.StructTag = DefaultStructTag
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"go.unistack.org/micro/v3/config"
|
"go.unistack.org/micro/v4/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestLoad(t *testing.T) {
|
func TestLoad(t *testing.T) {
|
||||||
|
|||||||
9
go.mod
9
go.mod
@@ -1,7 +1,10 @@
|
|||||||
module go.unistack.org/micro-config-flag/v3
|
module go.unistack.org/micro-config-flag/v4
|
||||||
|
|
||||||
go 1.18
|
go 1.18
|
||||||
|
|
||||||
require go.unistack.org/micro/v3 v3.10.16
|
require go.unistack.org/micro/v4 v4.0.17
|
||||||
|
|
||||||
require github.com/imdario/mergo v0.3.13 // indirect
|
require (
|
||||||
|
dario.cat/mergo v1.0.0 // indirect
|
||||||
|
github.com/google/uuid v1.6.0 // indirect
|
||||||
|
)
|
||||||
|
|||||||
13
go.sum
13
go.sum
@@ -1,10 +1,9 @@
|
|||||||
github.com/imdario/mergo v0.3.13 h1:lFzP57bqS/wsqKssCGmtLAb8A0wKjLGrve2q3PPVcBk=
|
dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk=
|
||||||
github.com/imdario/mergo v0.3.13/go.mod h1:4lJ1jqUDcsbIECGy0RUJAXNIhg+6ocWgb1ALK2O4oXg=
|
dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
|
||||||
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
|
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||||
github.com/silas/dag v0.0.0-20211117232152-9d50aa809f35/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I=
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
go.unistack.org/micro/v3 v3.10.16 h1:2er/SKKYbV60M+UuJM4eYCF0MZYAIq/yNUrAbTfgq8Q=
|
go.unistack.org/micro/v4 v4.0.17 h1:mF7uM+J4ILdG+1fcwzKYCwDlxhdbF/e1WnGzKKLnIXc=
|
||||||
go.unistack.org/micro/v3 v3.10.16/go.mod h1:uMAc0U/x7dmtICCrblGf0ZLgYegu3VwQAquu+OFCw1Q=
|
go.unistack.org/micro/v4 v4.0.17/go.mod h1:ZDgU9931vm2l7X6RN/6UuwRIVp24GRdmQ7dKmegArk4=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
|||||||
35
options.go
35
options.go
@@ -3,62 +3,61 @@ package flag
|
|||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
|
|
||||||
"go.unistack.org/micro/v3/config"
|
"go.unistack.org/micro/v4/options"
|
||||||
)
|
)
|
||||||
|
|
||||||
type sliceDelimKey struct{}
|
type sliceDelimKey struct{}
|
||||||
|
|
||||||
// SliceDelim set the slice delimeter
|
// SliceDelim set the slice delimeter
|
||||||
func SliceDelim(s string) config.Option {
|
func SliceDelim(s string) options.Option {
|
||||||
return config.SetOption(sliceDelimKey{}, s)
|
return options.ContextOption(sliceDelimKey{}, s)
|
||||||
}
|
}
|
||||||
|
|
||||||
type mapDelimKey struct{}
|
type mapDelimKey struct{}
|
||||||
|
|
||||||
// MapDelim set the map delimeter
|
// MapDelim set the map delimeter
|
||||||
func MapDelim(s string) config.Option {
|
func MapDelim(s string) options.Option {
|
||||||
return config.SetOption(mapDelimKey{}, s)
|
return options.ContextOption(mapDelimKey{}, s)
|
||||||
}
|
}
|
||||||
|
|
||||||
type timeFormatKey struct{}
|
type timeFormatKey struct{}
|
||||||
|
|
||||||
// TimeFormat set the time format
|
// TimeFormat set the time format
|
||||||
func TimeFormat(s string) config.Option {
|
func TimeFormat(s string) options.Option {
|
||||||
return config.SetOption(timeFormatKey{}, s)
|
return options.ContextOption(timeFormatKey{}, s)
|
||||||
}
|
}
|
||||||
|
|
||||||
type flagSetKey struct{}
|
type flagSetKey struct{}
|
||||||
|
|
||||||
// FlagSet set flag set name
|
// FlagSet set flag set name
|
||||||
func FlagSet(f *flag.FlagSet) config.Option {
|
func FlagSet(f *flag.FlagSet) options.Option {
|
||||||
return config.SetOption(flagSetKey{}, f)
|
return options.ContextOption(flagSetKey{}, f)
|
||||||
}
|
}
|
||||||
|
|
||||||
type flagSetNameKey struct{}
|
type flagSetNameKey struct{}
|
||||||
|
|
||||||
// FlagSetName set flag set name
|
// FlagSetName set flag set name
|
||||||
func FlagSetName(n string) config.Option {
|
func FlagSetName(n string) options.Option {
|
||||||
return config.SetOption(flagSetNameKey{}, n)
|
return options.ContextOption(flagSetNameKey{}, n)
|
||||||
}
|
}
|
||||||
|
|
||||||
type flagSetErrorHandlingKey struct{}
|
type flagSetErrorHandlingKey struct{}
|
||||||
|
|
||||||
// FlagErrorHandling set flag set error handling
|
// FlagErrorHandling set flag set error handling
|
||||||
func FlagErrorHandling(eh flag.ErrorHandling) config.Option {
|
func FlagErrorHandling(eh flag.ErrorHandling) options.Option {
|
||||||
return config.SetOption(flagSetErrorHandlingKey{}, eh)
|
return options.ContextOption(flagSetErrorHandlingKey{}, eh)
|
||||||
}
|
}
|
||||||
|
|
||||||
type flagSetUsageKey struct{}
|
type flagSetUsageKey struct{}
|
||||||
|
|
||||||
// FlagUsage set flag set usage func
|
// FlagUsage set flag set usage func
|
||||||
func FlagUsage(fn func()) config.Option {
|
func FlagUsage(fn func()) options.Option {
|
||||||
return config.SetOption(flagSetUsageKey{}, fn)
|
return options.ContextOption(flagSetUsageKey{}, fn)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
type flagEnvKey struct{}
|
type flagEnvKey struct{}
|
||||||
|
|
||||||
// FlagEnv set flag set usage func
|
// FlagEnv set flag set usage func
|
||||||
func FlagEnv(n string) config.Option {
|
func FlagEnv(n string) options.Option {
|
||||||
return config.SetOption(flagEnvKey{}, n)
|
return options.ContextOption(flagEnvKey{}, n)
|
||||||
}
|
}
|
||||||
|
|||||||
2
util.go
2
util.go
@@ -325,6 +325,7 @@ func (c *flagConfig) flagFloat64(v reflect.Value, fn, fv, fd string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
func (c *flagConfig) flagStringSlice(v reflect.Value, fn, fv, fd string) error {
|
func (c *flagConfig) flagStringSlice(v reflect.Value, fn, fv, fd string) error {
|
||||||
nv, ok := v.Addr().Interface().(*string)
|
nv, ok := v.Addr().Interface().(*string)
|
||||||
if !ok {
|
if !ok {
|
||||||
@@ -333,6 +334,7 @@ func (c *flagConfig) flagStringSlice(v reflect.Value, fn, fv, fd string) error {
|
|||||||
flag.StringVar(nv, fn, fv, fd)
|
flag.StringVar(nv, fn, fv, fd)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
func getFlagOpts(tf string) (string, string, string) {
|
func getFlagOpts(tf string) (string, string, string) {
|
||||||
var name, desc, def string
|
var name, desc, def string
|
||||||
|
|||||||
Reference in New Issue
Block a user