1
0
mirror of https://github.com/gogf/gf.git synced 2025-04-05 11:18:50 +08:00

unify command or enviroment key names for packages

This commit is contained in:
John Guo 2021-06-26 12:08:18 +08:00
parent 025cdd66c5
commit c25f88293b
22 changed files with 93 additions and 263 deletions

View File

@ -269,6 +269,7 @@ const (
ctxTimeoutTypeExec = iota
ctxTimeoutTypeQuery
ctxTimeoutTypePrepare
commandEnvKeyForDryRun = "gf.gdb.dryrun"
)
var (
@ -313,7 +314,7 @@ var (
func init() {
// allDryRun is initialized from environment or command options.
allDryRun = gcmd.GetOptWithEnv("gf.gdb.dryrun", false).Bool()
allDryRun = gcmd.GetOptWithEnv(commandEnvKeyForDryRun, false).Bool()
}
// Register registers custom database driver to gdb.

View File

@ -12,7 +12,6 @@ import (
"fmt"
"github.com/gogf/gf"
"github.com/gogf/gf/net/gtrace"
"github.com/gogf/gf/os/gcmd"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
@ -34,19 +33,9 @@ const (
tracingEventDbExecutionType = "db.execution.type"
)
var (
// tracingInternal enables tracing for internal type spans.
// It's true in default.
tracingInternal = true
)
func init() {
tracingInternal = gcmd.GetOptWithEnv("gf.tracing.internal", true).Bool()
}
// addSqlToTracing adds sql information to tracer if it's enabled.
func (c *Core) addSqlToTracing(ctx context.Context, sql *Sql) {
if !tracingInternal || !gtrace.IsActivated(ctx) {
if !gtrace.IsTracingInternal() || !gtrace.IsActivated(ctx) {
return
}
tr := otel.GetTracerProvider().Tracer(

View File

@ -12,7 +12,6 @@ import (
"github.com/gogf/gf"
"github.com/gogf/gf/internal/json"
"github.com/gogf/gf/net/gtrace"
"github.com/gogf/gf/os/gcmd"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
@ -38,19 +37,9 @@ const (
tracingEventRedisExecutionArguments = "redis.execution.arguments"
)
var (
// tracingInternal enables tracing for internal type spans.
// It's true in default.
tracingInternal = true
)
func init() {
tracingInternal = gcmd.GetOptWithEnv("gf.tracing.internal", true).Bool()
}
// addTracingItem checks and adds redis tracing information to OpenTelemetry.
func (c *Conn) addTracingItem(item *tracingItem) {
if !tracingInternal || !gtrace.IsActivated(c.ctx) {
if !gtrace.IsTracingInternal() || !gtrace.IsActivated(c.ctx) {
return
}
tr := otel.GetTracerProvider().Tracer(

View File

@ -319,23 +319,11 @@ func (j *Json) GetStruct(pattern string, pointer interface{}, mapping ...map[str
return gconv.Struct(j.Get(pattern), pointer, mapping...)
}
// GetStructDeep does GetStruct recursively.
// Deprecated, use GetStruct instead.
func (j *Json) GetStructDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
return gconv.StructDeep(j.Get(pattern), pointer, mapping...)
}
// GetStructs converts any slice to given struct slice.
func (j *Json) GetStructs(pattern string, pointer interface{}, mapping ...map[string]string) error {
return gconv.Structs(j.Get(pattern), pointer, mapping...)
}
// GetStructsDeep converts any slice to given struct slice recursively.
// Deprecated, use GetStructs instead.
func (j *Json) GetStructsDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
return gconv.StructsDeep(j.Get(pattern), pointer, mapping...)
}
// GetScan automatically calls Struct or Structs function according to the type of parameter
// <pointer> to implement the converting..
func (j *Json) GetScan(pattern string, pointer interface{}, mapping ...map[string]string) error {

View File

@ -70,7 +70,7 @@ func (j *Json) MustToJsonIndentString() string {
// ========================================================================
func (j *Json) ToXml(rootTag ...string) ([]byte, error) {
return gxml.Encode(j.ToMap(), rootTag...)
return gxml.Encode(j.Map(), rootTag...)
}
func (j *Json) ToXmlString(rootTag ...string) (string, error) {
@ -79,7 +79,7 @@ func (j *Json) ToXmlString(rootTag ...string) (string, error) {
}
func (j *Json) ToXmlIndent(rootTag ...string) ([]byte, error) {
return gxml.EncodeWithIndent(j.ToMap(), rootTag...)
return gxml.EncodeWithIndent(j.Map(), rootTag...)
}
func (j *Json) ToXmlIndentString(rootTag ...string) (string, error) {

View File

@ -1,104 +0,0 @@
// 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 gjson
import "github.com/gogf/gf/util/gconv"
// ToMap converts current Json object to map[string]interface{}.
// It returns nil if fails.
// Deprecated, use Map instead.
func (j *Json) ToMap() map[string]interface{} {
j.mu.RLock()
defer j.mu.RUnlock()
return gconv.Map(*(j.p))
}
// ToArray converts current Json object to []interface{}.
// It returns nil if fails.
// Deprecated, use Array instead.
func (j *Json) ToArray() []interface{} {
j.mu.RLock()
defer j.mu.RUnlock()
return gconv.Interfaces(*(j.p))
}
// ToStruct converts current Json object to specified object.
// The <pointer> should be a pointer type of *struct.
// Deprecated, use Struct instead.
func (j *Json) ToStruct(pointer interface{}, mapping ...map[string]string) error {
j.mu.RLock()
defer j.mu.RUnlock()
return gconv.Struct(*(j.p), pointer, mapping...)
}
// ToStructDeep converts current Json object to specified object recursively.
// The <pointer> should be a pointer type of *struct.
// Deprecated, use Struct instead.
func (j *Json) ToStructDeep(pointer interface{}, mapping ...map[string]string) error {
j.mu.RLock()
defer j.mu.RUnlock()
return gconv.StructDeep(*(j.p), pointer, mapping...)
}
// ToStructs converts current Json object to specified object slice.
// The <pointer> should be a pointer type of []struct/*struct.
// Deprecated, use Structs instead.
func (j *Json) ToStructs(pointer interface{}, mapping ...map[string]string) error {
j.mu.RLock()
defer j.mu.RUnlock()
return gconv.Structs(*(j.p), pointer, mapping...)
}
// ToStructsDeep converts current Json object to specified object slice recursively.
// The <pointer> should be a pointer type of []struct/*struct.
// Deprecated, use Structs instead.
func (j *Json) ToStructsDeep(pointer interface{}, mapping ...map[string]string) error {
j.mu.RLock()
defer j.mu.RUnlock()
return gconv.StructsDeep(*(j.p), pointer, mapping...)
}
// ToScan automatically calls Struct or Structs function according to the type of parameter
// <pointer> to implement the converting..
// Deprecated, use Scan instead.
func (j *Json) ToScan(pointer interface{}, mapping ...map[string]string) error {
return gconv.Scan(*(j.p), pointer, mapping...)
}
// ToScanDeep automatically calls StructDeep or StructsDeep function according to the type of
// parameter <pointer> to implement the converting..
// Deprecated, use Scan instead.
func (j *Json) ToScanDeep(pointer interface{}, mapping ...map[string]string) error {
return gconv.ScanDeep(*(j.p), pointer, mapping...)
}
// ToMapToMap converts current Json object to specified map variable.
// The parameter of <pointer> should be type of *map.
// Deprecated, use MapToMap instead.
func (j *Json) ToMapToMap(pointer interface{}, mapping ...map[string]string) error {
j.mu.RLock()
defer j.mu.RUnlock()
return gconv.MapToMap(*(j.p), pointer, mapping...)
}
// ToMapToMaps converts current Json object to specified map variable slice.
// The parameter of <pointer> should be type of []map/*map.
// Deprecated, use MapToMaps instead.
func (j *Json) ToMapToMaps(pointer interface{}, mapping ...map[string]string) error {
j.mu.RLock()
defer j.mu.RUnlock()
return gconv.MapToMaps(*(j.p), pointer, mapping...)
}
// ToMapToMapsDeep converts current Json object to specified map variable slice recursively.
// The parameter of <pointer> should be type of []map/*map.
// Deprecated, use MapToMaps instead.
func (j *Json) ToMapToMapsDeep(pointer interface{}, mapping ...map[string]string) error {
j.mu.RLock()
defer j.mu.RUnlock()
return gconv.MapToMapsDeep(*(j.p), pointer, mapping...)
}

View File

@ -100,7 +100,7 @@ func Example_conversionToStruct() {
Array []string
}
users := new(Users)
if err := j.ToStruct(users); err != nil {
if err := j.Struct(users); err != nil {
panic(err)
}
fmt.Printf(`%+v`, users)

View File

@ -353,7 +353,7 @@ func Test_Convert2(t *testing.T) {
t.Assert(j.GetGTime("time").Format("Y-m-d"), "2019-06-12")
t.Assert(j.GetDuration("time").String(), "0s")
err := j.ToStruct(&name)
err := j.Struct(&name)
t.Assert(err, nil)
t.Assert(name.Name, "gf")
//j.Dump()
@ -369,7 +369,7 @@ func Test_Convert2(t *testing.T) {
t.Assert(err, nil)
j = gjson.New(`[1,2,3]`)
t.Assert(len(j.ToArray()), 3)
t.Assert(len(j.Array()), 3)
})
}
@ -400,7 +400,7 @@ func Test_Basic(t *testing.T) {
err = j.Remove("1")
t.Assert(err, nil)
t.Assert(j.Get("0"), 1)
t.Assert(len(j.ToArray()), 2)
t.Assert(len(j.Array()), 2)
j = gjson.New(`[1,2,3]`)
// If index 0 is delete, its next item will be at index 0.
@ -408,13 +408,13 @@ func Test_Basic(t *testing.T) {
t.Assert(j.Remove("0"), nil)
t.Assert(j.Remove("0"), nil)
t.Assert(j.Get("0"), nil)
t.Assert(len(j.ToArray()), 0)
t.Assert(len(j.Array()), 0)
j = gjson.New(`[1,2,3]`)
err = j.Remove("3")
t.Assert(err, nil)
t.Assert(j.Get("0"), 1)
t.Assert(len(j.ToArray()), 3)
t.Assert(len(j.Array()), 3)
j = gjson.New(`[1,2,3]`)
err = j.Remove("0.3")

View File

@ -61,7 +61,7 @@ func Test_MapAttributeConvert(t *testing.T) {
Title map[string]interface{}
}{}
err = j.ToStruct(&tx)
err = j.Struct(&tx)
gtest.Assert(err, nil)
t.Assert(tx.Title, g.Map{
"l1": "标签1", "l2": "标签2",
@ -76,7 +76,7 @@ func Test_MapAttributeConvert(t *testing.T) {
Title map[string]string
}{}
err = j.ToStruct(&tx)
err = j.Struct(&tx)
gtest.Assert(err, nil)
t.Assert(tx.Title, g.Map{
"l1": "标签1", "l2": "标签2",

View File

@ -230,14 +230,14 @@ func Test_Convert(t *testing.T) {
err := p.GetStruct("person", &name)
t.Assert(err, nil)
t.Assert(name.Name, "gf")
t.Assert(p.ToMap()["name"], "gf")
err = p.ToStruct(&name)
t.Assert(p.Map()["name"], "gf")
err = p.Struct(&name)
t.Assert(err, nil)
t.Assert(name.Name, "gf")
//p.Dump()
p = gparser.New(`[0,1,2]`)
t.Assert(p.ToArray()[0], 0)
t.Assert(p.Array()[0], 0)
})
}

View File

@ -11,7 +11,7 @@ import (
)
const (
debugKey = "gf.debug" // Debug key for checking if in debug mode.
commandEnvKeyForDebugKey = "gf.debug" // Debug key for checking if in debug mode.
StackFilterKeyForGoFrame = "github.com/gogf/gf@" // Stack filtering key for all GoFrame module paths.
)
@ -22,7 +22,7 @@ var (
func init() {
// Debugging configured.
value := command.GetOptWithEnv(debugKey)
value := command.GetOptWithEnv(commandEnvKeyForDebugKey)
if value == "" || value == "0" || value == "false" {
isDebugEnabled = false
} else {

View File

@ -173,7 +173,7 @@ func bufferToServerFdMap(buffer []byte) map[string]listenerFdMap {
sfm := make(map[string]listenerFdMap)
if len(buffer) > 0 {
j, _ := gjson.LoadContent(buffer)
for k, _ := range j.ToMap() {
for k, _ := range j.Map() {
m := make(map[string]string)
for k, v := range j.GetMap(k) {
m[k] = gconv.String(v)

View File

@ -9,7 +9,6 @@ package gtrace
import (
"context"
"fmt"
"github.com/gogf/gf/container/gmap"
"github.com/gogf/gf/container/gvar"
"github.com/gogf/gf/net/gipv4"
@ -23,15 +22,17 @@ import (
)
const (
tracingCommonKeyIpIntranet = `ip.intranet`
tracingCommonKeyIpHostname = `hostname`
cmdEnvKey = "gf.gtrace" // Configuration key for command argument or environment.
tracingCommonKeyIpIntranet = `ip.intranet`
tracingCommonKeyIpHostname = `hostname`
commandEnvKeyForMaxContentLogSize = "gf.gtrace.maxcontentlogsize"
commandEnvKeyForTracingInternal = "gf.gtrace.tracinginternal"
)
var (
intranetIps, _ = gipv4.GetIntranetIpArray()
intranetIpStr = strings.Join(intranetIps, ",")
hostname, _ = os.Hostname()
tracingInternal = true // tracingInternal enables tracing for internal type spans.
tracingMaxContentLogSize = 256 * 1024 // Max log size for request and response body, especially for HTTP/RPC request.
// defaultTextMapPropagator is the default propagator for context propagation between peers.
defaultTextMapPropagator = propagation.NewCompositeTextMapPropagator(
@ -41,12 +42,18 @@ var (
)
func init() {
if maxContentLogSize := gcmd.GetOptWithEnv(fmt.Sprintf("%s.maxcontentlogsize", cmdEnvKey)).Int(); maxContentLogSize > 0 {
tracingInternal = gcmd.GetOptWithEnv(commandEnvKeyForTracingInternal, true).Bool()
if maxContentLogSize := gcmd.GetOptWithEnv(commandEnvKeyForMaxContentLogSize).Int(); maxContentLogSize > 0 {
tracingMaxContentLogSize = maxContentLogSize
}
CheckSetDefaultTextMapPropagator()
}
// IsTracingInternal returns whether tracing spans of internal components.
func IsTracingInternal() bool {
return tracingInternal
}
// MaxContentLogSize returns the max log size for request and response body, especially for HTTP/RPC request.
func MaxContentLogSize() int {
return tracingMaxContentLogSize

View File

@ -23,10 +23,11 @@ type Config struct {
}
const (
DefaultName = "config" // DefaultName is the default group name for instance usage.
DefaultConfigFile = "config.toml" // DefaultConfigFile is the default configuration file name.
cmdEnvKey = "gf.gcfg" // cmdEnvKey is the configuration key for command argument or environment.
errorPrintKey = "gf.gcfg.errorprint" // errorPrintKey is used to specify the key controlling error printing to stdout.
DefaultName = "config" // DefaultName is the default group name for instance usage.
DefaultConfigFile = "config.toml" // DefaultConfigFile is the default configuration file name.
commandEnvKeyForFile = "gf.gcfg.file" // commandEnvKeyForFile is the configuration key for command argument or environment configuring file name.
commandEnvKeyForPath = "gf.gcfg.path" // commandEnvKeyForPath is the configuration key for command argument or environment configuring directory path.
commandEnvKeyForErrorPrint = "gf.gcfg.errorprint" // commandEnvKeyForErrorPrint is used to specify the key controlling error printing to stdout.
)
var (
@ -99,5 +100,5 @@ func ClearContent() {
// errorPrint checks whether printing error to stdout.
func errorPrint() bool {
return gcmd.GetOptWithEnv(errorPrintKey, true).Bool()
return gcmd.GetOptWithEnv(commandEnvKeyForErrorPrint, true).Bool()
}

View File

@ -33,7 +33,7 @@ func New(file ...string) *Config {
name = file[0]
} else {
// Custom default configuration file name from command line or environment.
if customFile := gcmd.GetOptWithEnv(fmt.Sprintf("%s.file", cmdEnvKey)).String(); customFile != "" {
if customFile := gcmd.GetOptWithEnv(commandEnvKeyForFile).String(); customFile != "" {
name = customFile
}
}
@ -43,7 +43,7 @@ func New(file ...string) *Config {
jsonMap: gmap.NewStrAnyMap(true),
}
// Customized dir path from env/cmd.
if customPath := gcmd.GetOptWithEnv(fmt.Sprintf("%s.path", cmdEnvKey)).String(); customPath != "" {
if customPath := gcmd.GetOptWithEnv(commandEnvKeyForPath).String(); customPath != "" {
if gfile.Exists(customPath) {
_ = c.SetPath(customPath)
} else {

View File

@ -298,15 +298,6 @@ func (c *Config) GetStruct(pattern string, pointer interface{}, mapping ...map[s
return errors.New("configuration not found")
}
// GetStructDeep does GetStruct recursively.
// Deprecated, use GetStruct instead.
func (c *Config) GetStructDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil {
return j.GetStructDeep(pattern, pointer, mapping...)
}
return errors.New("configuration not found")
}
// GetStructs converts any slice to given struct slice.
func (c *Config) GetStructs(pattern string, pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil {
@ -315,15 +306,6 @@ func (c *Config) GetStructs(pattern string, pointer interface{}, mapping ...map[
return errors.New("configuration not found")
}
// GetStructsDeep converts any slice to given struct slice recursively.
// Deprecated, use GetStructs instead.
func (c *Config) GetStructsDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil {
return j.GetStructsDeep(pattern, pointer, mapping...)
}
return errors.New("configuration not found")
}
// GetMapToMap retrieves the value by specified `pattern` and converts it to specified map variable.
// See gconv.MapToMap.
func (c *Config) GetMapToMap(pattern string, pointer interface{}, mapping ...map[string]string) error {
@ -353,83 +335,55 @@ func (c *Config) GetMapToMapsDeep(pattern string, pointer interface{}, mapping .
return errors.New("configuration not found")
}
// ToMap converts current Json object to map[string]interface{}.
// It returns nil if fails.
func (c *Config) ToMap() map[string]interface{} {
// Map converts current Json object to map[string]interface{}. It returns nil if fails.
func (c *Config) Map() map[string]interface{} {
if j := c.getJson(); j != nil {
return j.ToMap()
return j.Map()
}
return nil
}
// ToArray converts current Json object to []interface{}.
// Array converts current Json object to []interface{}.
// It returns nil if fails.
func (c *Config) ToArray() []interface{} {
func (c *Config) Array() []interface{} {
if j := c.getJson(); j != nil {
return j.ToArray()
return j.Array()
}
return nil
}
// ToStruct converts current Json object to specified object.
// Struct converts current Json object to specified object.
// The `pointer` should be a pointer type of *struct.
func (c *Config) ToStruct(pointer interface{}, mapping ...map[string]string) error {
func (c *Config) Struct(pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil {
return j.ToStruct(pointer, mapping...)
return j.Struct(pointer, mapping...)
}
return errors.New("configuration not found")
}
// ToStructDeep converts current Json object to specified object recursively.
// The `pointer` should be a pointer type of *struct.
func (c *Config) ToStructDeep(pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil {
return j.ToStructDeep(pointer, mapping...)
}
return errors.New("configuration not found")
}
// ToStructs converts current Json object to specified object slice.
// Structs converts current Json object to specified object slice.
// The `pointer` should be a pointer type of []struct/*struct.
func (c *Config) ToStructs(pointer interface{}, mapping ...map[string]string) error {
func (c *Config) Structs(pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil {
return j.ToStructs(pointer, mapping...)
return j.Structs(pointer, mapping...)
}
return errors.New("configuration not found")
}
// ToStructsDeep converts current Json object to specified object slice recursively.
// The `pointer` should be a pointer type of []struct/*struct.
func (c *Config) ToStructsDeep(pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil {
return j.ToStructsDeep(pointer, mapping...)
}
return errors.New("configuration not found")
}
// ToMapToMap converts current Json object to specified map variable.
// MapToMap converts current Json object to specified map variable.
// The parameter of `pointer` should be type of *map.
func (c *Config) ToMapToMap(pointer interface{}, mapping ...map[string]string) error {
func (c *Config) MapToMap(pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil {
return j.ToMapToMap(pointer, mapping...)
return j.MapToMap(pointer, mapping...)
}
return errors.New("configuration not found")
}
// ToMapToMaps converts current Json object to specified map variable slice.
// MapToMaps converts current Json object to specified map variable slice.
// The parameter of `pointer` should be type of []map/*map.
func (c *Config) ToMapToMaps(pointer interface{}, mapping ...map[string]string) error {
func (c *Config) MapToMaps(pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil {
return j.ToMapToMaps(pointer, mapping...)
}
return errors.New("configuration not found")
}
// ToMapToMapsDeep converts current Json object to specified map variable slice recursively.
// The parameter of `pointer` should be type of []map/*map.
func (c *Config) ToMapToMapsDeep(pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil {
return j.ToMapToMapsDeep(pointer, mapping...)
return j.MapToMaps(pointer, mapping...)
}
return errors.New("configuration not found")
}

View File

@ -14,19 +14,19 @@ import (
)
const (
// Default expire time for file content caching in seconds.
gDEFAULT_CACHE_EXPIRE = time.Minute
defaultCacheExpire = time.Minute // defaultCacheExpire is the expire time for file content caching in seconds.
commandEnvKeyForCache = "gf.gfile.cache" // commandEnvKeyForCache is the configuration key for command argument or environment configuring cache expire duration.
)
var (
// Default expire time for file content caching.
cacheExpire = gcmd.GetOptWithEnv("gf.gfile.cache", gDEFAULT_CACHE_EXPIRE).Duration()
cacheExpire = gcmd.GetOptWithEnv(commandEnvKeyForCache, defaultCacheExpire).Duration()
// internalCache is the memory cache for internal usage.
internalCache = gcache.New()
)
// GetContents returns string content of given file by <path> from cache.
// GetContentsWithCache returns string content of given file by <path> from cache.
// If there's no content in the cache, it will read it from disk file specified by <path>.
// The parameter <expire> specifies the caching time for this file content in seconds.
func GetContentsWithCache(path string, duration ...time.Duration) string {
@ -62,5 +62,5 @@ func GetBytesWithCache(path string, duration ...time.Duration) []byte {
// cacheKey produces the cache key for gcache.
func cacheKey(path string) string {
return "gf.gfile.cache:" + path
return commandEnvKeyForCache + path
}

View File

@ -12,12 +12,16 @@ import (
"github.com/gogf/gf/os/grpool"
)
const (
commandEnvKeyForDebug = "gf.glog.debug"
)
var (
// Default logger object, for package method usage.
logger = New()
// Goroutine pool for async logging output.
// It uses only one asynchronize worker to ensure log sequence.
// It uses only one asynchronous worker to ensure log sequence.
asyncPool = grpool.New(1)
// defaultDebug enables debug level or not in default,
@ -26,7 +30,7 @@ var (
)
func init() {
defaultDebug = gcmd.GetOptWithEnv("gf.glog.debug", true).Bool()
defaultDebug = gcmd.GetOptWithEnv(commandEnvKeyForDebug, true).Bool()
SetDebug(defaultDebug)
}

View File

@ -19,7 +19,6 @@
package gtimer
import (
"fmt"
"github.com/gogf/gf/container/gtype"
"math"
"sync"
@ -43,21 +42,19 @@ type TimerOptions struct {
}
const (
StatusReady = 0 // Job or Timer is ready for running.
StatusRunning = 1 // Job or Timer is already running.
StatusStopped = 2 // Job or Timer is stopped.
StatusClosed = -1 // Job or Timer is closed and waiting to be deleted.
panicExit = "exit" // panicExit is used for custom job exit with panic.
defaultTimes = math.MaxInt32 // defaultTimes is the default limit running times, a big number.
defaultTimerInterval = 100 // defaultTimerInterval is the default timer interval in milliseconds.
cmdEnvKey = "gf.gtimer" // Configuration key for command argument or environment.
StatusReady = 0 // Job or Timer is ready for running.
StatusRunning = 1 // Job or Timer is already running.
StatusStopped = 2 // Job or Timer is stopped.
StatusClosed = -1 // Job or Timer is closed and waiting to be deleted.
panicExit = "exit" // panicExit is used for custom job exit with panic.
defaultTimes = math.MaxInt32 // defaultTimes is the default limit running times, a big number.
defaultTimerInterval = 100 // defaultTimerInterval is the default timer interval in milliseconds.
commandEnvKeyForInterval = "gf.gtimer.interval" // commandEnvKeyForInterval is the key for command argument or environment configuring default interval duration for timer.
)
var (
defaultTimer = New()
defaultInterval = gcmd.GetOptWithEnv(
fmt.Sprintf("%s.interval", cmdEnvKey), defaultTimerInterval,
).Duration() * time.Millisecond
defaultInterval = gcmd.GetOptWithEnv(commandEnvKeyForInterval, defaultTimerInterval).Duration() * time.Millisecond
)
// DefaultOptions creates and returns a default options object for Timer creation.

View File

@ -36,6 +36,10 @@ type (
FuncMap = map[string]interface{} // FuncMap is type for custom template functions.
)
const (
commandEnvKeyForPath = "gf.gview.path"
)
var (
// Default view object.
defaultViewObj *View
@ -72,7 +76,7 @@ func New(path ...string) *View {
}
} else {
// Customized dir path from env/cmd.
if envPath := gcmd.GetOptWithEnv("gf.gview.path").String(); envPath != "" {
if envPath := gcmd.GetOptWithEnv(commandEnvKeyForPath).String(); envPath != "" {
if gfile.Exists(envPath) {
if err := view.SetPath(envPath); err != nil {
intlog.Error(err)

View File

@ -11,12 +11,12 @@ import (
)
const (
// gERROR_PRINT_KEY is used to specify the key controlling error printing to stdout.
// commandEnvKeyForErrorPrint is used to specify the key controlling error printing to stdout.
// This error is designed not to be returned by functions.
gERROR_PRINT_KEY = "gf.gview.errorprint"
commandEnvKeyForErrorPrint = "gf.gview.errorprint"
)
// errorPrint checks whether printing error to stdout.
func errorPrint() bool {
return gcmd.GetOptWithEnv(gERROR_PRINT_KEY, true).Bool()
return gcmd.GetOptWithEnv(commandEnvKeyForErrorPrint, true).Bool()
}

View File

@ -16,12 +16,12 @@ import (
)
const (
NOT_SET = "not-set"
DEVELOP = "develop"
TESTING = "testing"
STAGING = "staging"
PRODUCT = "product"
cmdEnvKey = "gf.gmode"
NOT_SET = "not-set"
DEVELOP = "develop"
TESTING = "testing"
STAGING = "staging"
PRODUCT = "product"
commandEnvKey = "gf.gmode"
)
var (
@ -58,7 +58,7 @@ func SetProduct() {
func Mode() string {
// If current mode is not set, do this auto check.
if currentMode == NOT_SET {
if v := gcmd.GetOptWithEnv(cmdEnvKey).String(); v != "" {
if v := gcmd.GetOptWithEnv(commandEnvKey).String(); v != "" {
// Mode configured from command argument of environment.
currentMode = v
} else {