add absolute file path support for package gcfg/gview

This commit is contained in:
John Guo 2022-01-14 17:16:38 +08:00
parent fe93d7b332
commit 6468d55a81
2 changed files with 28 additions and 18 deletions

View File

@ -174,25 +174,30 @@ func (c *AdapterFile) GetFilePath(fileName ...string) (path string, err error) {
c.autoCheckAndAddMainPkgPathToSearchPaths()
// Searching local file system.
c.searchPaths.RLockFunc(func(array []string) {
for _, searchPath := range array {
searchPath = gstr.TrimRight(searchPath, `\/`)
for _, tryFolder := range localSystemTryFolders {
relativePath := gstr.TrimRight(
gfile.Join(tryFolder, usedFileName),
`\/`,
)
if path, _ = gspath.Search(searchPath, relativePath); path != "" {
return
if path == "" {
// Absolute path.
if path = gfile.RealPath(usedFileName); path != "" {
return
}
c.searchPaths.RLockFunc(func(array []string) {
for _, searchPath := range array {
searchPath = gstr.TrimRight(searchPath, `\/`)
for _, tryFolder := range localSystemTryFolders {
relativePath := gstr.TrimRight(
gfile.Join(tryFolder, usedFileName),
`\/`,
)
if path, _ = gspath.Search(searchPath, relativePath); path != "" {
return
}
}
}
}
})
})
}
// If it cannot find the path of `file`, it formats and returns a detailed error.
if path == "" {
var (
buffer = bytes.NewBuffer(nil)
)
var buffer = bytes.NewBuffer(nil)
if c.searchPaths.Len() > 0 {
buffer.WriteString(fmt.Sprintf(
`config file "%s" not found in resource manager or the following system searching paths:`,

View File

@ -319,9 +319,7 @@ func (view *View) formatTemplateObjectCreatingError(filePath, tplName string, er
// Note that, the returned `folder` is the template folder path, but not the folder of
// the returned template file `path`.
func (view *View) searchFile(ctx context.Context, file string) (path string, folder string, resource *gres.File, err error) {
var (
tempPath string
)
var tempPath string
// Firstly checking the resource manager.
if !gres.IsEmpty() {
// Try folders.
@ -350,6 +348,13 @@ func (view *View) searchFile(ctx context.Context, file string) (path string, fol
// Secondly checking the file system.
if path == "" {
// Absolute path.
path = gfile.RealPath(file)
if path != "" {
folder = gfile.Dir(path)
return
}
// In search paths.
view.searchPaths.RLockFunc(func(array []string) {
for _, searchPath := range array {
searchPath = gstr.TrimRight(searchPath, `\/`)