improve package gcfg

This commit is contained in:
John Guo
2022-03-01 22:34:57 +08:00
parent 46dc68dfd5
commit 1724a26957
3 changed files with 30 additions and 17 deletions

View File

@ -65,16 +65,15 @@ func Test_Config2(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var err error
dirPath := gfile.Temp(gtime.TimestampNanoStr())
err = gfile.Mkdir(dirPath)
t.Assert(err, nil)
t.AssertNil(gfile.Mkdir(dirPath))
defer gfile.Remove(dirPath)
name := "config/config.toml"
err = gfile.PutContents(gfile.Join(dirPath, name), configContent)
t.Assert(err, nil)
t.AssertNil(err)
err = gins.Config().GetAdapter().(*gcfg.AdapterFile).AddPath(dirPath)
t.Assert(err, nil)
t.AssertNil(err)
defer gins.Config().GetAdapter().(*gcfg.AdapterFile).Clear()
@ -200,9 +199,11 @@ func Test_Config4(t *testing.T) {
func Test_Basic2(t *testing.T) {
config := `log-path = "logs"`
gtest.C(t, func(t *gtest.T) {
path := gcfg.DefaultConfigFile
err := gfile.PutContents(path, config)
t.Assert(err, nil)
var (
path = gcfg.DefaultConfigFileName
err = gfile.PutContents(path, config)
)
t.AssertNil(err)
defer func() {
_ = gfile.Remove(path)
}()

View File

@ -223,6 +223,7 @@ func (c *AdapterFile) getJson(fileName ...string) (configJson *gjson.Json, err e
} else {
usedFileName = c.defaultName
}
// It uses json map to cache specified configuration file content.
result := c.jsonMap.GetOrSetFuncLock(usedFileName, func() interface{} {
var (
content string

View File

@ -10,6 +10,7 @@ import (
"bytes"
"context"
"fmt"
"os"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
@ -142,23 +143,33 @@ func (c *AdapterFile) AddPath(path string) (err error) {
// If `file` is not passed, it returns the configuration file path of the default name.
// It returns an empty `path` string and an error if the given `file` does not exist.
func (c *AdapterFile) doGetFilePath(fileName string) (path string) {
var tempPath string
var (
tempPath string
resFile *gres.File
fileInfo os.FileInfo
)
// Searching resource manager.
if !gres.IsEmpty() {
for _, tryFolder := range resourceTryFolders {
tempPath = tryFolder + fileName
if file := gres.Get(tempPath); file != nil {
path = file.Name()
return
if resFile = gres.Get(tempPath); resFile != nil {
fileInfo, _ = resFile.Stat()
if fileInfo != nil && !fileInfo.IsDir() {
path = resFile.Name()
return
}
}
}
c.searchPaths.RLockFunc(func(array []string) {
for _, searchPath := range array {
for _, tryFolder := range resourceTryFolders {
tempPath = searchPath + tryFolder + fileName
if file := gres.Get(tempPath); file != nil {
path = file.Name()
return
if resFile = gres.Get(tempPath); resFile != nil {
fileInfo, _ = resFile.Stat()
if fileInfo != nil && !fileInfo.IsDir() {
path = resFile.Name()
return
}
}
}
}
@ -170,7 +181,7 @@ func (c *AdapterFile) doGetFilePath(fileName string) (path string) {
// Searching local file system.
if path == "" {
// Absolute path.
if path = gfile.RealPath(fileName); path != "" {
if path = gfile.RealPath(fileName); path != "" && !gfile.IsDir(path) {
return
}
c.searchPaths.RLockFunc(func(array []string) {
@ -181,7 +192,7 @@ func (c *AdapterFile) doGetFilePath(fileName string) (path string) {
gfile.Join(tryFolder, fileName),
`\/`,
)
if path, _ = gspath.Search(searchPath, relativePath); path != "" {
if path, _ = gspath.Search(searchPath, relativePath); path != "" && !gfile.IsDir(path) {
return
}
}
@ -204,7 +215,7 @@ func (c *AdapterFile) GetFilePath(fileName ...string) (path string, err error) {
usedFileName = fileName[0]
}
fileExtName = gfile.ExtName(usedFileName)
if path = c.doGetFilePath(usedFileName); path == "" && !gstr.InArray(supportedFileTypes, fileExtName) {
if path = c.doGetFilePath(usedFileName); (path == "" || gfile.IsDir(path)) && !gstr.InArray(supportedFileTypes, fileExtName) {
// If it's not using default configuration or its configuration file is not available,
// it searches the possible configuration file according to the name and all supported
// file types.