mirror of
https://gitee.com/johng/gf.git
synced 2024-11-29 18:57:44 +08:00
improve package gcfg
This commit is contained in:
parent
adb4a1e6c0
commit
01b9fa8ac9
@ -241,22 +241,30 @@ func WrapCodeSkipf(code gcode.Code, skip int, err error, format string, args ...
|
||||
}
|
||||
|
||||
// Code returns the error code of current error.
|
||||
// It returns CodeNil if it has no error code or it does not implements interface Code.
|
||||
// It returns CodeNil if it has no error code neither it does not implement interface Code.
|
||||
func Code(err error) gcode.Code {
|
||||
if err != nil {
|
||||
if e, ok := err.(iCode); ok {
|
||||
return e.Code()
|
||||
}
|
||||
if err == nil {
|
||||
return gcode.CodeNil
|
||||
}
|
||||
if e, ok := err.(iCode); ok {
|
||||
return e.Code()
|
||||
}
|
||||
if e, ok := err.(iNext); ok {
|
||||
return Code(e.Next())
|
||||
}
|
||||
return gcode.CodeNil
|
||||
}
|
||||
|
||||
// Cause returns the root cause error of `err`.
|
||||
func Cause(err error) error {
|
||||
if err != nil {
|
||||
if e, ok := err.(iCause); ok {
|
||||
return e.Cause()
|
||||
}
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
if e, ok := err.(iCause); ok {
|
||||
return e.Cause()
|
||||
}
|
||||
if e, ok := err.(iNext); ok {
|
||||
return Cause(e.Next())
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
@ -65,6 +65,9 @@ func (err *Error) Code() gcode.Code {
|
||||
if err == nil {
|
||||
return gcode.CodeNil
|
||||
}
|
||||
if err.code == gcode.CodeNil {
|
||||
return Code(err.Next())
|
||||
}
|
||||
return err.code
|
||||
}
|
||||
|
||||
|
@ -13,6 +13,8 @@ import (
|
||||
|
||||
"github.com/gogf/gf/v2/container/gmap"
|
||||
"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"
|
||||
@ -140,7 +142,7 @@ func (c *Config) Get(ctx context.Context, pattern string, def ...interface{}) (*
|
||||
// Fetching Rules: Environment arguments are in uppercase format, eg: GF_PACKAGE_VARIABLE.
|
||||
func (c *Config) GetWithEnv(ctx context.Context, pattern string, def ...interface{}) (*gvar.Var, error) {
|
||||
value, err := c.Get(ctx, pattern)
|
||||
if err != nil {
|
||||
if err != nil && gerror.Code(err) != gcode.CodeNotFound {
|
||||
return nil, err
|
||||
}
|
||||
if value == nil {
|
||||
@ -162,7 +164,7 @@ func (c *Config) GetWithEnv(ctx context.Context, pattern string, def ...interfac
|
||||
// Fetching Rules: Command line arguments are in lowercase format, eg: gf.package.variable.
|
||||
func (c *Config) GetWithCmd(ctx context.Context, pattern string, def ...interface{}) (*gvar.Var, error) {
|
||||
value, err := c.Get(ctx, pattern)
|
||||
if err != nil {
|
||||
if err != nil && gerror.Code(err) != gcode.CodeNotFound {
|
||||
return nil, err
|
||||
}
|
||||
if value == nil {
|
||||
|
@ -219,7 +219,7 @@ func (c *AdapterFile) GetFilePath(fileName ...string) (path string, err error) {
|
||||
} else {
|
||||
buffer.WriteString(fmt.Sprintf(`cannot find config file "%s" with no path configured`, usedFileName))
|
||||
}
|
||||
err = gerror.New(buffer.String())
|
||||
err = gerror.NewCode(gcode.CodeNotFound, buffer.String())
|
||||
}
|
||||
return
|
||||
}
|
||||
|
66
os/gcfg/gcfg_z_example_test.go
Normal file
66
os/gcfg/gcfg_z_example_test.go
Normal file
@ -0,0 +1,66 @@
|
||||
// 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user