Skip to content

Commit 2e47166

Browse files
authored
os/gcfg: fix file searching issue always returning the configuration file of pwd (#3592)
1 parent 1e8f428 commit 2e47166

File tree

3 files changed

+71
-72
lines changed

3 files changed

+71
-72
lines changed

os/gcfg/gcfg_adapter_file.go

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ import (
2525

2626
// AdapterFile implements interface Adapter using file.
2727
type AdapterFile struct {
28-
defaultName string // Default configuration file name.
29-
searchPaths *garray.StrArray // Searching path array.
30-
jsonMap *gmap.StrAnyMap // The pared JSON objects for configuration files.
31-
violenceCheck bool // Whether it does violence check in value index searching. It affects the performance when set true(false in default).
28+
defaultFileNameOrPath string // Default configuration file name or file path.
29+
searchPaths *garray.StrArray // Searching path array.
30+
jsonMap *gmap.StrAnyMap // The pared JSON objects for configuration files.
31+
violenceCheck bool // Whether it does violence check in value index searching. It affects the performance when set true(false in default).
3232
}
3333

3434
const (
@@ -53,23 +53,23 @@ var (
5353

5454
// NewAdapterFile returns a new configuration management object.
5555
// The parameter `file` specifies the default configuration file name for reading.
56-
func NewAdapterFile(file ...string) (*AdapterFile, error) {
56+
func NewAdapterFile(fileNameOrPath ...string) (*AdapterFile, error) {
5757
var (
58-
err error
59-
name = DefaultConfigFileName
58+
err error
59+
usedFileNameOrPath = DefaultConfigFileName
6060
)
61-
if len(file) > 0 {
62-
name = file[0]
61+
if len(fileNameOrPath) > 0 {
62+
usedFileNameOrPath = fileNameOrPath[0]
6363
} else {
6464
// Custom default configuration file name from command line or environment.
6565
if customFile := command.GetOptWithEnv(commandEnvKeyForFile); customFile != "" {
66-
name = customFile
66+
usedFileNameOrPath = customFile
6767
}
6868
}
6969
config := &AdapterFile{
70-
defaultName: name,
71-
searchPaths: garray.NewStrArray(true),
72-
jsonMap: gmap.NewStrAnyMap(true),
70+
defaultFileNameOrPath: usedFileNameOrPath,
71+
searchPaths: garray.NewStrArray(true),
72+
jsonMap: gmap.NewStrAnyMap(true),
7373
}
7474
// Customized dir path from env/cmd.
7575
if customPath := command.GetOptWithEnv(commandEnvKeyForPath); customPath != "" {
@@ -120,13 +120,13 @@ func (a *AdapterFile) SetViolenceCheck(check bool) {
120120
}
121121

122122
// SetFileName sets the default configuration file name.
123-
func (a *AdapterFile) SetFileName(name string) {
124-
a.defaultName = name
123+
func (a *AdapterFile) SetFileName(fileNameOrPath string) {
124+
a.defaultFileNameOrPath = fileNameOrPath
125125
}
126126

127127
// GetFileName returns the default configuration file name.
128128
func (a *AdapterFile) GetFileName() string {
129-
return a.defaultName
129+
return a.defaultFileNameOrPath
130130
}
131131

132132
// Get retrieves and returns value by specified `pattern`.
@@ -200,7 +200,7 @@ func (a *AdapterFile) Dump() {
200200

201201
// Available checks and returns whether configuration of given `file` is available.
202202
func (a *AdapterFile) Available(ctx context.Context, fileName ...string) bool {
203-
checkFileName := gutil.GetOrDefaultStr(a.defaultName, fileName...)
203+
checkFileName := gutil.GetOrDefaultStr(a.defaultFileNameOrPath, fileName...)
204204
// Custom configuration content exists.
205205
if a.GetContent(checkFileName) != "" {
206206
return true
@@ -227,26 +227,26 @@ func (a *AdapterFile) autoCheckAndAddMainPkgPathToSearchPaths() {
227227

228228
// getJson returns a *gjson.Json object for the specified `file` content.
229229
// It would print error if file reading fails. It returns nil if any error occurs.
230-
func (a *AdapterFile) getJson(fileName ...string) (configJson *gjson.Json, err error) {
230+
func (a *AdapterFile) getJson(fileNameOrPath ...string) (configJson *gjson.Json, err error) {
231231
var (
232-
usedFileName = a.defaultName
232+
usedFileNameOrPath = a.defaultFileNameOrPath
233233
)
234-
if len(fileName) > 0 && fileName[0] != "" {
235-
usedFileName = fileName[0]
234+
if len(fileNameOrPath) > 0 && fileNameOrPath[0] != "" {
235+
usedFileNameOrPath = fileNameOrPath[0]
236236
} else {
237-
usedFileName = a.defaultName
237+
usedFileNameOrPath = a.defaultFileNameOrPath
238238
}
239239
// It uses json map to cache specified configuration file content.
240-
result := a.jsonMap.GetOrSetFuncLock(usedFileName, func() interface{} {
240+
result := a.jsonMap.GetOrSetFuncLock(usedFileNameOrPath, func() interface{} {
241241
var (
242242
content string
243243
filePath string
244244
)
245245
// The configured content can be any kind of data type different from its file type.
246246
isFromConfigContent := true
247-
if content = a.GetContent(usedFileName); content == "" {
247+
if content = a.GetContent(usedFileNameOrPath); content == "" {
248248
isFromConfigContent = false
249-
filePath, err = a.GetFilePath(usedFileName)
249+
filePath, err = a.GetFilePath(usedFileNameOrPath)
250250
if err != nil {
251251
return nil
252252
}
@@ -279,7 +279,7 @@ func (a *AdapterFile) getJson(fileName ...string) (configJson *gjson.Json, err e
279279
// any changes of this file will refresh its cache in Config object.
280280
if filePath != "" && !gres.Contains(filePath) {
281281
_, err = gfsnotify.Add(filePath, func(event *gfsnotify.Event) {
282-
a.jsonMap.Remove(usedFileName)
282+
a.jsonMap.Remove(usedFileNameOrPath)
283283
})
284284
if err != nil {
285285
return nil

os/gcfg/gcfg_adapter_file_content.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,58 +14,58 @@ import (
1414

1515
// SetContent sets customized configuration content for specified `file`.
1616
// The `file` is unnecessary param, default is DefaultConfigFile.
17-
func (a *AdapterFile) SetContent(content string, file ...string) {
18-
name := DefaultConfigFileName
19-
if len(file) > 0 {
20-
name = file[0]
17+
func (a *AdapterFile) SetContent(content string, fileNameOrPath ...string) {
18+
var usedFileNameOrPath = DefaultConfigFileName
19+
if len(fileNameOrPath) > 0 {
20+
usedFileNameOrPath = fileNameOrPath[0]
2121
}
2222
// Clear file cache for instances which cached `name`.
2323
localInstances.LockFunc(func(m map[string]interface{}) {
24-
if customConfigContentMap.Contains(name) {
24+
if customConfigContentMap.Contains(usedFileNameOrPath) {
2525
for _, v := range m {
2626
if configInstance, ok := v.(*Config); ok {
2727
if fileConfig, ok := configInstance.GetAdapter().(*AdapterFile); ok {
28-
fileConfig.jsonMap.Remove(name)
28+
fileConfig.jsonMap.Remove(usedFileNameOrPath)
2929
}
3030
}
3131
}
3232
}
33-
customConfigContentMap.Set(name, content)
33+
customConfigContentMap.Set(usedFileNameOrPath, content)
3434
})
3535
}
3636

3737
// GetContent returns customized configuration content for specified `file`.
3838
// The `file` is unnecessary param, default is DefaultConfigFile.
39-
func (a *AdapterFile) GetContent(file ...string) string {
40-
name := DefaultConfigFileName
41-
if len(file) > 0 {
42-
name = file[0]
39+
func (a *AdapterFile) GetContent(fileNameOrPath ...string) string {
40+
var usedFileNameOrPath = DefaultConfigFileName
41+
if len(fileNameOrPath) > 0 {
42+
usedFileNameOrPath = fileNameOrPath[0]
4343
}
44-
return customConfigContentMap.Get(name)
44+
return customConfigContentMap.Get(usedFileNameOrPath)
4545
}
4646

4747
// RemoveContent removes the global configuration with specified `file`.
4848
// If `name` is not passed, it removes configuration of the default group name.
49-
func (a *AdapterFile) RemoveContent(file ...string) {
50-
name := DefaultConfigFileName
51-
if len(file) > 0 {
52-
name = file[0]
49+
func (a *AdapterFile) RemoveContent(fileNameOrPath ...string) {
50+
var usedFileNameOrPath = DefaultConfigFileName
51+
if len(fileNameOrPath) > 0 {
52+
usedFileNameOrPath = fileNameOrPath[0]
5353
}
5454
// Clear file cache for instances which cached `name`.
5555
localInstances.LockFunc(func(m map[string]interface{}) {
56-
if customConfigContentMap.Contains(name) {
56+
if customConfigContentMap.Contains(usedFileNameOrPath) {
5757
for _, v := range m {
5858
if configInstance, ok := v.(*Config); ok {
5959
if fileConfig, ok := configInstance.GetAdapter().(*AdapterFile); ok {
60-
fileConfig.jsonMap.Remove(name)
60+
fileConfig.jsonMap.Remove(usedFileNameOrPath)
6161
}
6262
}
6363
}
64-
customConfigContentMap.Remove(name)
64+
customConfigContentMap.Remove(usedFileNameOrPath)
6565
}
6666
})
6767

68-
intlog.Printf(context.TODO(), `RemoveContent: %s`, name)
68+
intlog.Printf(context.TODO(), `RemoveContent: %s`, usedFileNameOrPath)
6969
}
7070

7171
// ClearContent removes all global configuration contents.

os/gcfg/gcfg_adapter_file_path.go

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,9 @@ func (a *AdapterFile) GetPaths() []string {
174174
return a.searchPaths.Slice()
175175
}
176176

177-
// doGetFilePath returns the absolute configuration file path for the given filename by `file`.
178-
// If `file` is not passed, it returns the configuration file path of the default name.
179-
// It returns an empty `path` string and an error if the given `file` does not exist.
180-
func (a *AdapterFile) doGetFilePath(fileName string) (filePath string) {
177+
// doGetFilePath returns the absolute configuration file path for the given filename by `fileNameOrPath`.
178+
// The `fileNameOrPath` can be either a file name or the file path.
179+
func (a *AdapterFile) doGetFilePath(fileNameOrPath string) (filePath string) {
181180
var (
182181
tempPath string
183182
resFile *gres.File
@@ -186,7 +185,7 @@ func (a *AdapterFile) doGetFilePath(fileName string) (filePath string) {
186185
// Searching resource manager.
187186
if !gres.IsEmpty() {
188187
for _, tryFolder := range resourceTryFolders {
189-
tempPath = tryFolder + fileName
188+
tempPath = tryFolder + fileNameOrPath
190189
if resFile = gres.Get(tempPath); resFile != nil {
191190
fileInfo, _ = resFile.Stat()
192191
if fileInfo != nil && !fileInfo.IsDir() {
@@ -198,7 +197,7 @@ func (a *AdapterFile) doGetFilePath(fileName string) (filePath string) {
198197
a.searchPaths.RLockFunc(func(array []string) {
199198
for _, searchPath := range array {
200199
for _, tryFolder := range resourceTryFolders {
201-
tempPath = searchPath + tryFolder + fileName
200+
tempPath = searchPath + tryFolder + fileNameOrPath
202201
if resFile = gres.Get(tempPath); resFile != nil {
203202
fileInfo, _ = resFile.Stat()
204203
if fileInfo != nil && !fileInfo.IsDir() {
@@ -215,16 +214,12 @@ func (a *AdapterFile) doGetFilePath(fileName string) (filePath string) {
215214

216215
// Searching local file system.
217216
if filePath == "" {
218-
// Absolute path.
219-
/*if filePath = gfile.RealPath(fileName); filePath != "" && !gfile.IsDir(filePath) {
220-
return
221-
}*/
222217
a.searchPaths.RLockFunc(func(array []string) {
223218
for _, searchPath := range array {
224219
searchPath = gstr.TrimRight(searchPath, `\/`)
225220
for _, tryFolder := range localSystemTryFolders {
226221
relativePath := gstr.TrimRight(
227-
gfile.Join(tryFolder, fileName),
222+
gfile.Join(tryFolder, fileNameOrPath),
228223
`\/`,
229224
)
230225
if filePath, _ = gspath.Search(searchPath, relativePath); filePath != "" &&
@@ -235,9 +230,9 @@ func (a *AdapterFile) doGetFilePath(fileName string) (filePath string) {
235230
}
236231
})
237232
}
233+
// The `fileNameOrPath` can be a file path.
238234
if filePath == "" {
239-
// Absolute path.
240-
if filePath = gfile.RealPath(fileName); filePath != "" && !gfile.IsDir(filePath) {
235+
if filePath = gfile.RealPath(fileNameOrPath); filePath != "" && !gfile.IsDir(filePath) {
241236
return
242237
}
243238
}
@@ -247,23 +242,24 @@ func (a *AdapterFile) doGetFilePath(fileName string) (filePath string) {
247242
// GetFilePath returns the absolute configuration file path for the given filename by `file`.
248243
// If `file` is not passed, it returns the configuration file path of the default name.
249244
// It returns an empty `path` string and an error if the given `file` does not exist.
250-
func (a *AdapterFile) GetFilePath(fileName ...string) (filePath string, err error) {
245+
func (a *AdapterFile) GetFilePath(fileNameOrPath ...string) (filePath string, err error) {
251246
var (
252-
fileExtName string
253-
tempFileName string
254-
usedFileName = a.defaultName
247+
fileExtName string
248+
tempFileNameOrPath string
249+
usedFileNameOrPath = a.defaultFileNameOrPath
255250
)
256-
if len(fileName) > 0 {
257-
usedFileName = fileName[0]
251+
if len(fileNameOrPath) > 0 {
252+
usedFileNameOrPath = fileNameOrPath[0]
258253
}
259-
fileExtName = gfile.ExtName(usedFileName)
260-
if filePath = a.doGetFilePath(usedFileName); (filePath == "" || gfile.IsDir(filePath)) && !gstr.InArray(supportedFileTypes, fileExtName) {
254+
fileExtName = gfile.ExtName(usedFileNameOrPath)
255+
if filePath = a.doGetFilePath(usedFileNameOrPath); (filePath == "" || gfile.IsDir(filePath)) &&
256+
!gstr.InArray(supportedFileTypes, fileExtName) {
261257
// If it's not using default configuration or its configuration file is not available,
262258
// it searches the possible configuration file according to the name and all supported
263259
// file types.
264260
for _, fileType := range supportedFileTypes {
265-
tempFileName = fmt.Sprintf(`%s.%s`, usedFileName, fileType)
266-
if filePath = a.doGetFilePath(tempFileName); filePath != "" {
261+
tempFileNameOrPath = fmt.Sprintf(`%s.%s`, usedFileNameOrPath, fileType)
262+
if filePath = a.doGetFilePath(tempFileNameOrPath); filePath != "" {
267263
break
268264
}
269265
}
@@ -275,12 +271,12 @@ func (a *AdapterFile) GetFilePath(fileName ...string) (filePath string, err erro
275271
if !gstr.InArray(supportedFileTypes, fileExtName) {
276272
buffer.WriteString(fmt.Sprintf(
277273
`possible config files "%s" or "%s" not found in resource manager or following system searching paths:`,
278-
usedFileName, fmt.Sprintf(`%s.%s`, usedFileName, gstr.Join(supportedFileTypes, "/")),
274+
usedFileNameOrPath, fmt.Sprintf(`%s.%s`, usedFileNameOrPath, gstr.Join(supportedFileTypes, "/")),
279275
))
280276
} else {
281277
buffer.WriteString(fmt.Sprintf(
282278
`specified config file "%s" not found in resource manager or following system searching paths:`,
283-
usedFileName,
279+
usedFileNameOrPath,
284280
))
285281
}
286282
a.searchPaths.RLockFunc(func(array []string) {
@@ -297,7 +293,10 @@ func (a *AdapterFile) GetFilePath(fileName ...string) (filePath string, err erro
297293
}
298294
})
299295
} else {
300-
buffer.WriteString(fmt.Sprintf(`cannot find config file "%s" with no filePath configured`, usedFileName))
296+
buffer.WriteString(fmt.Sprintf(
297+
`cannot find config file "%s" with no filePath configured`,
298+
usedFileNameOrPath,
299+
))
301300
}
302301
err = gerror.NewCode(gcode.CodeNotFound, buffer.String())
303302
}

0 commit comments

Comments
 (0)