From 6468d55a8145b8cba9acca9c956ed9d6c4b04583 Mon Sep 17 00:00:00 2001 From: John Guo Date: Fri, 14 Jan 2022 17:16:38 +0800 Subject: [PATCH] add absolute file path support for package gcfg/gview --- os/gcfg/gcfg_adapter_file_path.go | 35 ++++++++++++++++++------------- os/gview/gview_parse.go | 11 +++++++--- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/os/gcfg/gcfg_adapter_file_path.go b/os/gcfg/gcfg_adapter_file_path.go index c94d34644..fa65ecca5 100644 --- a/os/gcfg/gcfg_adapter_file_path.go +++ b/os/gcfg/gcfg_adapter_file_path.go @@ -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:`, diff --git a/os/gview/gview_parse.go b/os/gview/gview_parse.go index ea89b5503..b480528e5 100644 --- a/os/gview/gview_parse.go +++ b/os/gview/gview_parse.go @@ -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, `\/`)