Skip to content

Commit

Permalink
feat: show power by
Browse files Browse the repository at this point in the history
  • Loading branch information
hui.wang committed Mar 21, 2022
1 parent c976ecc commit ef6c3df
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
16 changes: 15 additions & 1 deletion gen_options_optiongen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ xconf : https://github.com/sandwich-go/xconf
optiongen: https://github.com/timestee/optiongen
xconf-providers: https://github.com/sandwich-go/xconf-providers
`
var powerBy = `Powered by: https://github.com/sandwich-go/xconf`

// OptionsOptionDeclareWithDefault go-lint
//go:generate optiongen --option_with_struct_name=false --xconf=true --usage_tag_name=usage
Expand Down Expand Up @@ -85,6 +86,8 @@ func OptionsOptionDeclareWithDefault() interface{} {
"ParseMetaKeyFlagFiles": true,
// annotation@EnvironPrefix(comment="绑定ENV前缀,防止ENV名称覆盖污染")
"EnvironPrefix": "",
// annotation@OptionUsagePoweredBy(comment="--help中显示Power by")
"OptionUsagePoweredBy": string(powerBy),
// annotation@StringAlias(comment="值别名")
"StringAlias": (map[string]string)(map[string]string{
"math.MaxInt": strconv.Itoa(maxInt),
Expand Down
1 change: 1 addition & 0 deletions tests/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ 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
14 changes: 10 additions & 4 deletions xconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,17 @@ func (x *XConf) mergeMap(srcName string, dstName string, src map[string]interfac
return mergeMap("", 0, x.runningLogger, src, dst, x.isLeafFieldPath, nil, nil)
}

func getOptionUsage(valPtr interface{}) string {
func (x *XConf) getOptionUsage(valPtr interface{}) (ret string) {
if w, ok := valPtr.(GetOptionUsage); ok {
return xutil.StringTrim(w.GetOptionUsage())
ret = xutil.StringTrim(w.GetOptionUsage())
}
return ""
if ret == "" {
return x.cc.OptionUsagePoweredBy
}
if x.cc.OptionUsagePoweredBy == "" {
return ret
}
return ret + "\n" + x.cc.OptionUsagePoweredBy
}

// Merge 合并配置
Expand All @@ -223,7 +229,7 @@ func (x *XConf) parse(valPtr interface{}) (err error) {
if reflect.ValueOf(valPtr).Kind() != reflect.Ptr {
return errors.New("unsupported type, pass in as ptr")
}
x.optionUsage = getOptionUsage(valPtr)
x.optionUsage = x.getOptionUsage(valPtr)

// 如果应用层配置实现了XConfOptions
applyXConfOptions(valPtr, x)
Expand Down

0 comments on commit ef6c3df

Please sign in to comment.