gf/os/gcfg/gcfg_z_example_test.go
2022-01-25 20:43:57 +08:00

67 lines
1.3 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/frame/g"
"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(err)
}
fmt.Printf("env:%s\n", v)
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:
// env:gf
}
func ExampleConfig_GetWithCmd() {
var (
key = `cmd.test`
ctx = gctx.New()
)
v, err := g.Cfg().GetWithCmd(ctx, key)
if err != nil {
panic(err)
}
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...)
// 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:
// cmd:yes
}