mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
Co-authored-by: 杨延庆 <yangyq@bosyun.cn> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
This commit is contained in:
@ -11,8 +11,6 @@ import (
|
||||
"context"
|
||||
|
||||
"github.com/gogf/gf/v2/container/gvar"
|
||||
"github.com/gogf/gf/v2/errors/gcode"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/internal/command"
|
||||
"github.com/gogf/gf/v2/internal/intlog"
|
||||
"github.com/gogf/gf/v2/internal/utils"
|
||||
@ -119,20 +117,10 @@ func (c *Config) Get(ctx context.Context, pattern string, def ...any) (*gvar.Var
|
||||
//
|
||||
// Fetching Rules: Environment arguments are in uppercase format, eg: GF_PACKAGE_VARIABLE.
|
||||
func (c *Config) GetWithEnv(ctx context.Context, pattern string, def ...any) (*gvar.Var, error) {
|
||||
value, err := c.Get(ctx, pattern)
|
||||
if err != nil && gerror.Code(err) != gcode.CodeNotFound {
|
||||
return nil, err
|
||||
if v := genv.Get(utils.FormatEnvKey(pattern)); v != nil {
|
||||
return v, nil
|
||||
}
|
||||
if value == nil {
|
||||
if v := genv.Get(utils.FormatEnvKey(pattern)); v != nil {
|
||||
return v, nil
|
||||
}
|
||||
if len(def) > 0 {
|
||||
return gvar.New(def[0]), nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
return value, nil
|
||||
return c.Get(ctx, pattern, def...)
|
||||
}
|
||||
|
||||
// GetWithCmd returns the configuration value specified by pattern `pattern`.
|
||||
@ -141,20 +129,10 @@ func (c *Config) GetWithEnv(ctx context.Context, pattern string, def ...any) (*g
|
||||
//
|
||||
// Fetching Rules: Command line arguments are in lowercase format, eg: gf.package.variable.
|
||||
func (c *Config) GetWithCmd(ctx context.Context, pattern string, def ...any) (*gvar.Var, error) {
|
||||
value, err := c.Get(ctx, pattern)
|
||||
if err != nil && gerror.Code(err) != gcode.CodeNotFound {
|
||||
return nil, err
|
||||
if v := command.GetOpt(utils.FormatCmdKey(pattern)); v != "" {
|
||||
return gvar.New(v), nil
|
||||
}
|
||||
if value == nil {
|
||||
if v := command.GetOpt(utils.FormatCmdKey(pattern)); v != "" {
|
||||
return gvar.New(v), nil
|
||||
}
|
||||
if len(def) > 0 {
|
||||
return gvar.New(def[0]), nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
return value, nil
|
||||
return c.Get(ctx, pattern, def...)
|
||||
}
|
||||
|
||||
// Data retrieves and returns all configuration data as map type.
|
||||
|
||||
@ -10,6 +10,7 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gcfg"
|
||||
"github.com/gogf/gf/v2/os/gcmd"
|
||||
@ -23,10 +24,9 @@ func ExampleConfig_GetWithEnv() {
|
||||
ctx = gctx.New()
|
||||
)
|
||||
v, err := g.Cfg().GetWithEnv(ctx, key)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
if err == nil {
|
||||
panic(gerror.New("environment variable is not defined"))
|
||||
}
|
||||
fmt.Printf("env:%s\n", v)
|
||||
if err = genv.Set(key, "gf"); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -37,7 +37,6 @@ func ExampleConfig_GetWithEnv() {
|
||||
fmt.Printf("env:%s", v)
|
||||
|
||||
// Output:
|
||||
// env:
|
||||
// env:gf
|
||||
}
|
||||
|
||||
@ -47,10 +46,9 @@ func ExampleConfig_GetWithCmd() {
|
||||
ctx = gctx.New()
|
||||
)
|
||||
v, err := g.Cfg().GetWithCmd(ctx, key)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
if err == nil {
|
||||
panic(gerror.New("command option is not defined"))
|
||||
}
|
||||
fmt.Printf("cmd:%s\n", v)
|
||||
// Re-Initialize custom command arguments.
|
||||
os.Args = append(os.Args, fmt.Sprintf(`--%s=yes`, key))
|
||||
gcmd.Init(os.Args...)
|
||||
@ -62,7 +60,6 @@ func ExampleConfig_GetWithCmd() {
|
||||
fmt.Printf("cmd:%s", v)
|
||||
|
||||
// Output:
|
||||
// cmd:
|
||||
// cmd:yes
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user