From c47dbbcca113e448936daaf1758f54eb038749a6 Mon Sep 17 00:00:00 2001 From: xgfone Date: Sat, 20 Feb 2021 22:53:54 +0800 Subject: [PATCH] add the methods Args and SetArgs to support the CLI rest arguments --- config.go | 16 ++++++++++++++++ global.go | 10 ++++++++++ go.mod | 2 +- go.sum | 4 ++-- source.go | 8 +++++++- source_cli.go | 1 + source_flag.go | 8 +++++++- 7 files changed, 44 insertions(+), 5 deletions(-) diff --git a/config.go b/config.go index e168731..27aa11f 100644 --- a/config.go +++ b/config.go @@ -78,6 +78,7 @@ type Config struct { lock sync.RWMutex gsep string // The separator between the group names. + args []string snap *snapshot groups map[string]*OptGroup // the option groups groups2 map[string]*OptGroup // The auxiliary groups @@ -312,6 +313,21 @@ func (c *Config) GetVersion() (version Opt) { return } +// Args returns the rest CLI arguments. +func (c *Config) Args() []string { + c.lock.RLock() + args := c.args + c.lock.RUnlock() + return args +} + +// SetArgs sets the rest CLI arguments to args. +func (c *Config) SetArgs(args []string) { + c.lock.Lock() + c.args = args + c.lock.Unlock() +} + // PrintGroup prints the information of all groups to w. func (c *Config) PrintGroup(w io.Writer) error { for _, group := range c.AllGroups() { diff --git a/global.go b/global.go index ffc8738..025ea26 100644 --- a/global.go +++ b/global.go @@ -16,6 +16,16 @@ package gconf import "time" +// Args is equal to Conf.Args(). +func Args() []string { + return Conf.Args() +} + +// SetArgs is equal to Conf.SetArgs(args). +func SetArgs(args []string) { + Conf.SetArgs(args) +} + // GetVersion is equal to Conf.GetVersion(). func GetVersion() Opt { return Conf.GetVersion() diff --git a/go.mod b/go.mod index 27f0d57..b3e6cf9 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ require ( github.com/fsnotify/fsnotify v1.4.9 github.com/samuel/go-zookeeper v0.0.0-20190923202752-2cc03de413da github.com/urfave/cli/v2 v2.2.0 - github.com/xgfone/cast v0.1.0 + github.com/xgfone/cast v0.5.0 gopkg.in/yaml.v2 v2.3.0 ) diff --git a/go.sum b/go.sum index 8599258..c2836af 100644 --- a/go.sum +++ b/go.sum @@ -14,8 +14,8 @@ github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5I github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/urfave/cli/v2 v2.2.0 h1:JTTnM6wKzdA0Jqodd966MVj4vWbbquZykeX1sKbe2C4= github.com/urfave/cli/v2 v2.2.0/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ= -github.com/xgfone/cast v0.1.0 h1:iXykFomf2xhXJi+M7v/wVDXXRs+qqUAfh0knADglorc= -github.com/xgfone/cast v0.1.0/go.mod h1:T+gPbsD/fD72zz9wy/XaLTv236sPHaf6TcT7uvIhV/k= +github.com/xgfone/cast v0.5.0 h1:5nROCXsIKvdD+zxNeiiPT1BqnLM8sDm4elTYd5KR50c= +github.com/xgfone/cast v0.5.0/go.mod h1:T+gPbsD/fD72zz9wy/XaLTv236sPHaf6TcT7uvIhV/k= golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9 h1:L2auWcuQIvxz9xSEqzESnV/QN/gNRXNApHi3fYwl2w0= golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= diff --git a/source.go b/source.go index aec48dc..aaaecac 100644 --- a/source.go +++ b/source.go @@ -40,6 +40,7 @@ func NewSourceError(source, format string, data []byte, err error) SourceError { // DataSet represents the information of the configuration data. type DataSet struct { + Args []string // The CLI arguments filled by the CLI source such as flag. Data []byte // The original data. Format string // Such as "json", "xml", etc. Source string // Such as "file:/path/to/file", "zk:127.0.0.1:2181", etc. @@ -81,7 +82,12 @@ func (c *Config) LoadDataSet(ds DataSet, force ...bool) (err error) { return err } - return c.LoadMap(ms, force...) + if err = c.LoadMap(ms, force...); err == nil && ds.Args != nil { + if c.Args() == nil || (len(force) > 0 && force[0]) { + c.SetArgs(ds.Args) + } + } + return } func (c *Config) loadDataSetWithError(ds DataSet, err error, force ...bool) (ok bool) { diff --git a/source_cli.go b/source_cli.go index c5a05b2..008ef72 100644 --- a/source_cli.go +++ b/source_cli.go @@ -147,6 +147,7 @@ func (c cliSource) Read() (DataSet, error) { data, _ := json.Marshal(opts) ds := DataSet{ + Args: c.ctx.Args().Slice(), Data: data, Format: "json", Source: "cli", diff --git a/source_flag.go b/source_flag.go index 7ca9bd9..3196220 100644 --- a/source_flag.go +++ b/source_flag.go @@ -208,7 +208,13 @@ func (f flagSource) Read() (DataSet, error) { if err != nil { return DataSet{Source: "flag", Format: "json"}, err } - ds := DataSet{Data: data, Format: "json", Source: "flag", Timestamp: time.Now()} + ds := DataSet{ + Args: f.flagSet.Args(), + Data: data, + Format: "json", + Source: "flag", + Timestamp: time.Now(), + } ds.Checksum = fmt.Sprintf("md5:%s", ds.Md5()) return ds, nil }