mirror of
https://gitee.com/johng/gf
synced 2026-07-04 21:03:13 +08:00
Co-authored-by: 杨延庆 <yangyq@bosyun.cn> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
83 lines
1.7 KiB
Go
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
|
|
}
|