Skip to content

Commit

Permalink
feat: support dump config to yaml through help command
Browse files Browse the repository at this point in the history
  • Loading branch information
hui.wang committed Mar 23, 2022
1 parent f365a9b commit 53c60f1
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 7 deletions.
1 change: 0 additions & 1 deletion tests/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ func main() {
xconf.WithFiles("c1.yaml"),
xconf.WithDebug(false),
xconf.WithEnvironPrefix("test_prefix_"),
xconf.WithOptionUsagePoweredBy(""),
)
if err := xx.Parse(cc); err != nil {
panic(err)
Expand Down
2 changes: 2 additions & 0 deletions xconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type XConf struct {
atomicSetFunc func(interface{})
optionUsage string
parseForMerge bool
valPtrForUsageDump interface{}
}

// New 构造新的Xconf
Expand Down Expand Up @@ -236,6 +237,7 @@ func (x *XConf) parse(valPtr interface{}) (err error) {
}

if x.cc.FlagSet != nil && x.cc.ReplaceFlagSetUsage {
x.valPtrForUsageDump = valPtr
x.cc.FlagSet.Usage = x.Usage
}

Expand Down
42 changes: 36 additions & 6 deletions xconf_usage.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package xconf

import (
"bytes"
"errors"
"fmt"
"io"
Expand All @@ -17,24 +18,53 @@ import (
func (x *XConf) Usage() { x.UsageToWriter(os.Stderr, x.cc.FlagArgs...) }

func (x *XConf) UsageToWriter(w io.Writer, args ...string) {
err := x.usageToWriter(w, args...)
if err == nil {
return
}
x.cc.LogWarning(fmt.Sprintf("UsageToWriter got error:%s", err.Error()))
}

func (x *XConf) usageToWriter(w io.Writer, args ...string) (err error) {
parsedOptions := xflag.ParseArgsToMapStringString(args)
val, got := parsedOptions["help"]
if !got {
val, got = parsedOptions["h"]
}
var err error
val = xutil.StringTrim(val)
if got && strings.EqualFold(xutil.StringTrim(val), "xconf") {
// 指定xconf_usage的FlagArgs为空,避免再次触发help逻辑
xx := New(WithFlagSet(newFlagSetContinueOnError("xconf_usage")), WithFlagArgs(), WithErrorHandling(ContinueOnError))
cc := NewOptions()
xutil.PanicErr(xx.Parse(cc))
err = xx.usageLinesToWriter(w)
} else {
err = x.usageLinesToWriter(w)
return xx.usageLinesToWriter(w)
}
if err != nil {
x.cc.LogWarning(fmt.Sprintf("UsageToWriter got error:%s", err.Error()))
if got && strings.EqualFold(xutil.StringTrim(val), "yaml") {
if x.valPtrForUsageDump == nil {
return errors.New("usage for yaml got empty config input")
}
return x.SaveVarToWriterAsYAML(x.valPtrForUsageDump, w)
}
if got && strings.HasSuffix(val, string(ConfigTypeYAML)) { // 输出到文件
defer func() {
if err == nil {
fmt.Println("\n🍺 save config file as yaml to: ", val)
} else {
fmt.Println("\n🚫 got error while save config file as yaml, err:", err.Error())
}
err = nil
}()
if x.valPtrForUsageDump == nil {
return errors.New("usage for yaml file got config input")
}
bytesBuffer := bytes.NewBuffer([]byte{})
err := x.SaveVarToWriterAsYAML(x.valPtrForUsageDump, bytesBuffer)
if err != nil {
return err
}
return xutil.FilePutContents(val, bytesBuffer.Bytes())
}
return x.usageLinesToWriter(w)
}

// usageLinesToWriter 打印usage信息到io.Writer
Expand Down
15 changes: 15 additions & 0 deletions xutil/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
)
Expand Down Expand Up @@ -189,3 +192,15 @@ func KVListApplyFunc(f func(k, v string) bool, kv ...string) error {
}
return nil
}

func FilePutContents(filename string, content []byte) error {
err := os.MkdirAll(filepath.Dir(filename), os.ModePerm)
if err != nil {
return fmt.Errorf("got error:%s while mkdir:%s", err.Error(), filepath.Dir(filename))
}
err = ioutil.WriteFile(filename, content, 0644)
if err != nil {
return fmt.Errorf("got error:%s while write to file:%s", err.Error(), filename)
}
return nil
}

0 comments on commit 53c60f1

Please sign in to comment.