mirror of
https://gitee.com/johng/gf.git
synced 2024-11-30 11:18:02 +08:00
improve package gmode
This commit is contained in:
parent
4e027c1de3
commit
293256c2ca
@ -26,8 +26,8 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
// DEFAULT_CONFIG_FILE is the default configuration file name.
|
||||
DEFAULT_CONFIG_FILE = "config.toml"
|
||||
DEFAULT_CONFIG_FILE = "config.toml" // The default configuration file name.
|
||||
gCMDENV_KEY = "gf.gcfg" // Configuration key for command argument or environment.
|
||||
)
|
||||
|
||||
// Configuration struct.
|
||||
@ -55,7 +55,7 @@ func New(file ...string) *Config {
|
||||
jsons: gmap.NewStrAnyMap(true),
|
||||
}
|
||||
// Customized dir path from env/cmd.
|
||||
if envPath := cmdenv.Get("gf.gcfg.path").String(); envPath != "" {
|
||||
if envPath := cmdenv.Get(fmt.Sprintf("%s.path", gCMDENV_KEY)).String(); envPath != "" {
|
||||
if gfile.Exists(envPath) {
|
||||
_ = c.SetPath(envPath)
|
||||
} else {
|
||||
|
@ -20,6 +20,7 @@
|
||||
package gtimer
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"time"
|
||||
|
||||
@ -36,12 +37,13 @@ const (
|
||||
gDEFAULT_SLOT_NUMBER = 10 // Default slot number.
|
||||
gDEFAULT_WHEEL_INTERVAL = 50 // Default wheel interval.
|
||||
gDEFAULT_WHEEL_LEVEL = 6 // Default wheel level.
|
||||
gCMDENV_KEY = "gf.gtimer" // Configuration key for command argument or environment.
|
||||
)
|
||||
|
||||
var (
|
||||
defaultSlots = cmdenv.Get("gf.gtimer.slots", gDEFAULT_SLOT_NUMBER).Int()
|
||||
defaultLevel = cmdenv.Get("gf.gtimer.level", gDEFAULT_WHEEL_LEVEL).Int()
|
||||
defaultInterval = cmdenv.Get("gf.gtimer.interval", gDEFAULT_WHEEL_INTERVAL).Duration() * time.Millisecond
|
||||
defaultSlots = cmdenv.Get(fmt.Sprintf("%s.slots", gCMDENV_KEY), gDEFAULT_SLOT_NUMBER).Int()
|
||||
defaultLevel = cmdenv.Get(fmt.Sprintf("%s.level", gCMDENV_KEY), gDEFAULT_WHEEL_LEVEL).Int()
|
||||
defaultInterval = cmdenv.Get(fmt.Sprintf("%s.interval", gCMDENV_KEY), gDEFAULT_WHEEL_INTERVAL).Duration() * time.Millisecond
|
||||
defaultTimer = New(defaultSlots, defaultInterval, defaultLevel)
|
||||
)
|
||||
|
||||
|
@ -9,14 +9,19 @@
|
||||
// It uses string to mark the mode instead of integer, which is convenient for configuration.
|
||||
package gmode
|
||||
|
||||
import "github.com/gogf/gf/os/gfile"
|
||||
import (
|
||||
"github.com/gogf/gf/debug/gdebug"
|
||||
"github.com/gogf/gf/internal/cmdenv"
|
||||
"github.com/gogf/gf/os/gfile"
|
||||
)
|
||||
|
||||
const (
|
||||
NOT_SET = "not-set"
|
||||
DEVELOP = "develop"
|
||||
TESTING = "testing"
|
||||
STAGING = "staging"
|
||||
PRODUCT = "product"
|
||||
NOT_SET = "not-set"
|
||||
DEVELOP = "develop"
|
||||
TESTING = "testing"
|
||||
STAGING = "staging"
|
||||
PRODUCT = "product"
|
||||
gCMDENV_KEY = "gf.gmode"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -52,10 +57,16 @@ func SetProduct() {
|
||||
func Mode() string {
|
||||
// If current mode is not set, do this auto check.
|
||||
if currentMode == NOT_SET {
|
||||
if gfile.MainPkgPath() != "" {
|
||||
return DEVELOP
|
||||
if v := cmdenv.Get(gCMDENV_KEY).String(); v != "" {
|
||||
// Mode configured from command argument of environment.
|
||||
currentMode = v
|
||||
} else {
|
||||
return PRODUCT
|
||||
// If there are source codes found, it's in develop mode, or else in product mode.
|
||||
if gfile.Exists(gdebug.CallerFilePath()) {
|
||||
currentMode = DEVELOP
|
||||
} else {
|
||||
currentMode = PRODUCT
|
||||
}
|
||||
}
|
||||
}
|
||||
return currentMode
|
||||
|
61
util/gmode/gmode_test.go
Normal file
61
util/gmode/gmode_test.go
Normal file
@ -0,0 +1,61 @@
|
||||
// Copyright 2020 gf Author(https://github.com/gogf/gf). 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.
|
||||
|
||||
// go test *.go -bench=".*"
|
||||
|
||||
package gmode_test
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/util/gmode"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/test/gtest"
|
||||
)
|
||||
|
||||
func Test_AutoCheckSourceCodes(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
t.Assert(gmode.IsDevelop(), true)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Set(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
oldMode := gmode.Mode()
|
||||
defer gmode.Set(oldMode)
|
||||
gmode.SetDevelop()
|
||||
t.Assert(gmode.IsDevelop(), true)
|
||||
t.Assert(gmode.IsTesting(), false)
|
||||
t.Assert(gmode.IsStaging(), false)
|
||||
t.Assert(gmode.IsProduct(), false)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
oldMode := gmode.Mode()
|
||||
defer gmode.Set(oldMode)
|
||||
gmode.SetTesting()
|
||||
t.Assert(gmode.IsDevelop(), false)
|
||||
t.Assert(gmode.IsTesting(), true)
|
||||
t.Assert(gmode.IsStaging(), false)
|
||||
t.Assert(gmode.IsProduct(), false)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
oldMode := gmode.Mode()
|
||||
defer gmode.Set(oldMode)
|
||||
gmode.SetStaging()
|
||||
t.Assert(gmode.IsDevelop(), false)
|
||||
t.Assert(gmode.IsTesting(), false)
|
||||
t.Assert(gmode.IsStaging(), true)
|
||||
t.Assert(gmode.IsProduct(), false)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
oldMode := gmode.Mode()
|
||||
defer gmode.Set(oldMode)
|
||||
gmode.SetProduct()
|
||||
t.Assert(gmode.IsDevelop(), false)
|
||||
t.Assert(gmode.IsTesting(), false)
|
||||
t.Assert(gmode.IsStaging(), false)
|
||||
t.Assert(gmode.IsProduct(), true)
|
||||
})
|
||||
}
|
Loading…
Reference in New Issue
Block a user