improve package gmode

This commit is contained in:
john 2020-07-12 09:34:43 +08:00
parent 4e027c1de3
commit 293256c2ca
4 changed files with 89 additions and 15 deletions

View File

@ -26,8 +26,8 @@ import (
) )
const ( const (
// DEFAULT_CONFIG_FILE is the default configuration file name. DEFAULT_CONFIG_FILE = "config.toml" // The default configuration file name.
DEFAULT_CONFIG_FILE = "config.toml" gCMDENV_KEY = "gf.gcfg" // Configuration key for command argument or environment.
) )
// Configuration struct. // Configuration struct.
@ -55,7 +55,7 @@ func New(file ...string) *Config {
jsons: gmap.NewStrAnyMap(true), jsons: gmap.NewStrAnyMap(true),
} }
// Customized dir path from env/cmd. // 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) { if gfile.Exists(envPath) {
_ = c.SetPath(envPath) _ = c.SetPath(envPath)
} else { } else {

View File

@ -20,6 +20,7 @@
package gtimer package gtimer
import ( import (
"fmt"
"math" "math"
"time" "time"
@ -36,12 +37,13 @@ const (
gDEFAULT_SLOT_NUMBER = 10 // Default slot number. gDEFAULT_SLOT_NUMBER = 10 // Default slot number.
gDEFAULT_WHEEL_INTERVAL = 50 // Default wheel interval. gDEFAULT_WHEEL_INTERVAL = 50 // Default wheel interval.
gDEFAULT_WHEEL_LEVEL = 6 // Default wheel level. gDEFAULT_WHEEL_LEVEL = 6 // Default wheel level.
gCMDENV_KEY = "gf.gtimer" // Configuration key for command argument or environment.
) )
var ( var (
defaultSlots = cmdenv.Get("gf.gtimer.slots", gDEFAULT_SLOT_NUMBER).Int() defaultSlots = cmdenv.Get(fmt.Sprintf("%s.slots", gCMDENV_KEY), gDEFAULT_SLOT_NUMBER).Int()
defaultLevel = cmdenv.Get("gf.gtimer.level", gDEFAULT_WHEEL_LEVEL).Int() defaultLevel = cmdenv.Get(fmt.Sprintf("%s.level", gCMDENV_KEY), gDEFAULT_WHEEL_LEVEL).Int()
defaultInterval = cmdenv.Get("gf.gtimer.interval", gDEFAULT_WHEEL_INTERVAL).Duration() * time.Millisecond defaultInterval = cmdenv.Get(fmt.Sprintf("%s.interval", gCMDENV_KEY), gDEFAULT_WHEEL_INTERVAL).Duration() * time.Millisecond
defaultTimer = New(defaultSlots, defaultInterval, defaultLevel) defaultTimer = New(defaultSlots, defaultInterval, defaultLevel)
) )

View File

@ -9,14 +9,19 @@
// It uses string to mark the mode instead of integer, which is convenient for configuration. // It uses string to mark the mode instead of integer, which is convenient for configuration.
package gmode 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 ( const (
NOT_SET = "not-set" NOT_SET = "not-set"
DEVELOP = "develop" DEVELOP = "develop"
TESTING = "testing" TESTING = "testing"
STAGING = "staging" STAGING = "staging"
PRODUCT = "product" PRODUCT = "product"
gCMDENV_KEY = "gf.gmode"
) )
var ( var (
@ -52,10 +57,16 @@ func SetProduct() {
func Mode() string { func Mode() string {
// If current mode is not set, do this auto check. // If current mode is not set, do this auto check.
if currentMode == NOT_SET { if currentMode == NOT_SET {
if gfile.MainPkgPath() != "" { if v := cmdenv.Get(gCMDENV_KEY).String(); v != "" {
return DEVELOP // Mode configured from command argument of environment.
currentMode = v
} else { } 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 return currentMode

61
util/gmode/gmode_test.go Normal file
View 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)
})
}