Files
gf/os/gcfg/gcfg_z_example_test.go
Frank Yang caea7ea4b8 fix(os/gcfg): adjust priority of env|cmd higer than config file (#4074) (#4587)
Co-authored-by: 杨延庆 <yangyq@bosyun.cn>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2026-01-09 11:04:00 +08:00

83 lines
1.7 KiB
Go

// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gcfg_test
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"
"github.com/gogf/gf/v2/os/gctx"
"github.com/gogf/gf/v2/os/genv"
)
func ExampleConfig_GetWithEnv() {
var (
key = `ENV_TEST`
ctx = gctx.New()
)
v, err := g.Cfg().GetWithEnv(ctx, key)
if err == nil {
panic(gerror.New("environment variable is not defined"))
}
if err = genv.Set(key, "gf"); err != nil {
panic(err)
}
v, err = g.Cfg().GetWithEnv(ctx, key)
if err != nil {
panic(err)
}
fmt.Printf("env:%s", v)
// Output:
// env:gf
}
func ExampleConfig_GetWithCmd() {
var (
key = `cmd.test`
ctx = gctx.New()
)
v, err := g.Cfg().GetWithCmd(ctx, key)
if err == nil {
panic(gerror.New("command option is not defined"))
}
// Re-Initialize custom command arguments.
os.Args = append(os.Args, fmt.Sprintf(`--%s=yes`, key))
gcmd.Init(os.Args...)
// Retrieve the configuration and command option again.
v, err = g.Cfg().GetWithCmd(ctx, key)
if err != nil {
panic(err)
}
fmt.Printf("cmd:%s", v)
// Output:
// cmd:yes
}
func ExampleConfig_newWithAdapter() {
var (
ctx = gctx.New()
content = `{"a":"b", "c":1}`
adapter, err = gcfg.NewAdapterContent(content)
)
if err != nil {
panic(err)
}
config := gcfg.NewWithAdapter(adapter)
fmt.Println(config.MustGet(ctx, "a"))
fmt.Println(config.MustGet(ctx, "c"))
// Output:
// b
// 1
}