From caea7ea4b8a36fe7cb222dbf1ad323914db80e95 Mon Sep 17 00:00:00 2001 From: Frank Yang Date: Fri, 9 Jan 2026 11:04:00 +0800 Subject: [PATCH] fix(os/gcfg): adjust priority of env|cmd higer than config file (#4074) (#4587) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 杨延庆 Co-authored-by: github-actions[bot] --- os/gcfg/gcfg.go | 34 ++++++---------------------------- os/gcfg/gcfg_z_example_test.go | 13 +++++-------- 2 files changed, 11 insertions(+), 36 deletions(-) diff --git a/os/gcfg/gcfg.go b/os/gcfg/gcfg.go index 0768f8d5a..8ab058c3f 100644 --- a/os/gcfg/gcfg.go +++ b/os/gcfg/gcfg.go @@ -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. diff --git a/os/gcfg/gcfg_z_example_test.go b/os/gcfg/gcfg_z_example_test.go index 78ea671ba..3ce4ebe1c 100644 --- a/os/gcfg/gcfg_z_example_test.go +++ b/os/gcfg/gcfg_z_example_test.go @@ -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 }