Skip to content

Commit

Permalink
add the methods Args and SetArgs to support the CLI rest arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
xgfone committed Feb 20, 2021
1 parent 8f6ac29 commit c47dbbc
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 5 deletions.
16 changes: 16 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down
10 changes: 10 additions & 0 deletions global.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
8 changes: 7 additions & 1 deletion source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions source_cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 7 additions & 1 deletion source_flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit c47dbbc

Please sign in to comment.