Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve comments and variable names for gcfg.AdapterFile of package gcfg #3046

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
95 changes: 58 additions & 37 deletions os/gcfg/gcfg_adapter_file_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,27 @@ import (
"github.com/gogf/gf/v2/text/gstr"
)

// SetPath sets the configuration directory path for file search.
// The parameter `path` can be absolute or relative path,
// but absolute path is strongly recommended.
func (a *AdapterFile) SetPath(path string) (err error) {
// SetPath sets the configuration `directory` path for file search.
// The parameter `path` can be absolute or relative `directory` path,
// but absolute `directory` path is strongly recommended.
//
// Note that this parameter is a path to a directory not a file.
func (a *AdapterFile) SetPath(directoryPath string) (err error) {
var (
isDir = false
realPath = ""
)
if file := gres.Get(path); file != nil {
realPath = path
if file := gres.Get(directoryPath); file != nil {
realPath = directoryPath
isDir = file.FileInfo().IsDir()
} else {
// Absolute path.
realPath = gfile.RealPath(path)
realPath = gfile.RealPath(directoryPath)
if realPath == "" {
// Relative path.
a.searchPaths.RLockFunc(func(array []string) {
for _, v := range array {
if searchedPath, _ := gspath.Search(v, path); searchedPath != "" {
if searchedPath, _ := gspath.Search(v, directoryPath); searchedPath != "" {
realPath = searchedPath
break
}
Expand All @@ -54,14 +56,20 @@ func (a *AdapterFile) SetPath(path string) (err error) {
if realPath == "" {
buffer := bytes.NewBuffer(nil)
if a.searchPaths.Len() > 0 {
buffer.WriteString(fmt.Sprintf(`SetPath failed: cannot find directory "%s" in following paths:`, path))
buffer.WriteString(fmt.Sprintf(
`SetPath failed: cannot find directory "%s" in following paths:`,
directoryPath,
))
a.searchPaths.RLockFunc(func(array []string) {
for k, v := range array {
buffer.WriteString(fmt.Sprintf("\n%d. %s", k+1, v))
}
})
} else {
buffer.WriteString(fmt.Sprintf(`SetPath failed: path "%s" does not exist`, path))
buffer.WriteString(fmt.Sprintf(
`SetPath failed: path "%s" does not exist`,
directoryPath,
))
}
return gerror.New(buffer.String())
}
Expand All @@ -70,7 +78,7 @@ func (a *AdapterFile) SetPath(path string) (err error) {
return gerror.NewCodef(
gcode.CodeInvalidParameter,
`SetPath failed: path "%s" should be directory type`,
path,
directoryPath,
)
}
// Repeated path check.
Expand All @@ -84,35 +92,37 @@ func (a *AdapterFile) SetPath(path string) (err error) {
return nil
}

// AddPath adds an absolute or relative path to the search paths.
func (a *AdapterFile) AddPath(paths ...string) (err error) {
for _, path := range paths {
if err = a.doAddPath(path); err != nil {
// AddPath adds an absolute or relative `directory` path to the search paths.
//
// Note that this parameter is paths to a directories not files.
func (a *AdapterFile) AddPath(directoryPaths ...string) (err error) {
for _, directoryPath := range directoryPaths {
if err = a.doAddPath(directoryPath); err != nil {
return err
}
}
return nil
}

// doAddPath adds an absolute or relative path to the search paths.
func (a *AdapterFile) doAddPath(path string) (err error) {
// doAddPath adds an absolute or relative `directory` path to the search paths.
func (a *AdapterFile) doAddPath(directoryPath string) (err error) {
var (
isDir = false
realPath = ""
)
// It firstly checks the resource manager,
// and then checks the filesystem for the path.
if file := gres.Get(path); file != nil {
realPath = path
if file := gres.Get(directoryPath); file != nil {
realPath = directoryPath
isDir = file.FileInfo().IsDir()
} else {
// Absolute path.
realPath = gfile.RealPath(path)
realPath = gfile.RealPath(directoryPath)
if realPath == "" {
// Relative path.
a.searchPaths.RLockFunc(func(array []string) {
for _, v := range array {
if searchedPath, _ := gspath.Search(v, path); searchedPath != "" {
if searchedPath, _ := gspath.Search(v, directoryPath); searchedPath != "" {
realPath = searchedPath
break
}
Expand All @@ -126,19 +136,29 @@ func (a *AdapterFile) doAddPath(path string) (err error) {
if realPath == "" {
buffer := bytes.NewBuffer(nil)
if a.searchPaths.Len() > 0 {
buffer.WriteString(fmt.Sprintf(`AddPath failed: cannot find directory "%s" in following paths:`, path))
buffer.WriteString(fmt.Sprintf(
`AddPath failed: cannot find directory "%s" in following paths:`,
directoryPath,
))
a.searchPaths.RLockFunc(func(array []string) {
for k, v := range array {
buffer.WriteString(fmt.Sprintf("\n%d. %s", k+1, v))
}
})
} else {
buffer.WriteString(fmt.Sprintf(`AddPath failed: path "%s" does not exist`, path))
buffer.WriteString(fmt.Sprintf(
`AddPath failed: path "%s" does not exist`,
directoryPath,
))
}
return gerror.New(buffer.String())
}
if !isDir {
return gerror.NewCodef(gcode.CodeInvalidParameter, `AddPath failed: path "%s" should be directory type`, path)
return gerror.NewCodef(
gcode.CodeInvalidParameter,
`AddPath failed: path "%s" should be directory type`,
directoryPath,
)
}
// Repeated path check.
if a.searchPaths.Search(realPath) != -1 {
Expand All @@ -149,15 +169,15 @@ func (a *AdapterFile) doAddPath(path string) (err error) {
return nil
}

// GetPaths returns the searching path array of current configuration manager.
// GetPaths returns the searching directory path array of current configuration manager.
func (a *AdapterFile) GetPaths() []string {
return a.searchPaths.Slice()
}

// doGetFilePath returns the absolute configuration file path for the given filename by `file`.
// 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 (a *AdapterFile) doGetFilePath(fileName string) (path string) {
func (a *AdapterFile) doGetFilePath(fileName string) (filePath string) {
var (
tempPath string
resFile *gres.File
Expand All @@ -170,7 +190,7 @@ func (a *AdapterFile) doGetFilePath(fileName string) (path string) {
if resFile = gres.Get(tempPath); resFile != nil {
fileInfo, _ = resFile.Stat()
if fileInfo != nil && !fileInfo.IsDir() {
path = resFile.Name()
filePath = resFile.Name()
return
}
}
Expand All @@ -182,7 +202,7 @@ func (a *AdapterFile) doGetFilePath(fileName string) (path string) {
if resFile = gres.Get(tempPath); resFile != nil {
fileInfo, _ = resFile.Stat()
if fileInfo != nil && !fileInfo.IsDir() {
path = resFile.Name()
filePath = resFile.Name()
return
}
}
Expand All @@ -194,9 +214,9 @@ func (a *AdapterFile) doGetFilePath(fileName string) (path string) {
a.autoCheckAndAddMainPkgPathToSearchPaths()

// Searching local file system.
if path == "" {
if filePath == "" {
// Absolute path.
if path = gfile.RealPath(fileName); path != "" && !gfile.IsDir(path) {
if filePath = gfile.RealPath(fileName); filePath != "" && !gfile.IsDir(filePath) {
return
}
a.searchPaths.RLockFunc(func(array []string) {
Expand All @@ -207,7 +227,8 @@ func (a *AdapterFile) doGetFilePath(fileName string) (path string) {
gfile.Join(tryFolder, fileName),
`\/`,
)
if path, _ = gspath.Search(searchPath, relativePath); path != "" && !gfile.IsDir(path) {
if filePath, _ = gspath.Search(searchPath, relativePath); filePath != "" &&
!gfile.IsDir(filePath) {
return
}
}
Expand All @@ -220,7 +241,7 @@ func (a *AdapterFile) doGetFilePath(fileName string) (path string) {
// GetFilePath returns the absolute configuration file path for the given filename by `file`.
// 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 (a *AdapterFile) GetFilePath(fileName ...string) (path string, err error) {
func (a *AdapterFile) GetFilePath(fileName ...string) (filePath string, err error) {
var (
fileExtName string
tempFileName string
Expand All @@ -230,19 +251,19 @@ func (a *AdapterFile) GetFilePath(fileName ...string) (path string, err error) {
usedFileName = fileName[0]
}
fileExtName = gfile.ExtName(usedFileName)
if path = a.doGetFilePath(usedFileName); (path == "" || gfile.IsDir(path)) && !gstr.InArray(supportedFileTypes, fileExtName) {
if filePath = a.doGetFilePath(usedFileName); (filePath == "" || gfile.IsDir(filePath)) && !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.
for _, fileType := range supportedFileTypes {
tempFileName = fmt.Sprintf(`%s.%s`, usedFileName, fileType)
if path = a.doGetFilePath(tempFileName); path != "" {
if filePath = a.doGetFilePath(tempFileName); filePath != "" {
break
}
}
}
// If it cannot find the path of `file`, it formats and returns a detailed error.
if path == "" {
// If it cannot find the filePath of `file`, it formats and returns a detailed error.
if filePath == "" {
var buffer = bytes.NewBuffer(nil)
if a.searchPaths.Len() > 0 {
if !gstr.InArray(supportedFileTypes, fileExtName) {
Expand Down Expand Up @@ -270,7 +291,7 @@ func (a *AdapterFile) GetFilePath(fileName ...string) (path string, err error) {
}
})
} else {
buffer.WriteString(fmt.Sprintf(`cannot find config file "%s" with no path configured`, usedFileName))
buffer.WriteString(fmt.Sprintf(`cannot find config file "%s" with no filePath configured`, usedFileName))
}
err = gerror.NewCode(gcode.CodeNotFound, buffer.String())
}
Expand Down