From d4e4b9addf1b99d5cbc71647b42b063efeb6737e Mon Sep 17 00:00:00 2001 From: John Guo Date: Mon, 29 Mar 2021 18:05:47 +0800 Subject: [PATCH] improve package gcfg for automatically checking and adding path of package main --- os/gcfg/gcfg.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/os/gcfg/gcfg.go b/os/gcfg/gcfg.go index 1cd6a7d25..76f5f7985 100644 --- a/os/gcfg/gcfg.go +++ b/os/gcfg/gcfg.go @@ -15,6 +15,7 @@ import ( "github.com/gogf/gf/internal/intlog" "github.com/gogf/gf/os/gcmd" "github.com/gogf/gf/text/gstr" + "github.com/gogf/gf/util/gmode" "github.com/gogf/gf/os/gres" @@ -290,6 +291,7 @@ func (c *Config) GetFilePath(file ...string) (path string, err error) { } }) } + c.autoCheckAndAddMainPkgPathToSearchPaths() // Searching the file system. c.searchPaths.RLockFunc(func(array []string) { for _, prefix := range array { @@ -327,6 +329,19 @@ func (c *Config) GetFilePath(file ...string) (path string, err error) { return } +// autoCheckAndAddMainPkgPathToSearchPaths automatically checks and adds directory path of package main +// to the searching path list if it's currently in development environment. +func (c *Config) autoCheckAndAddMainPkgPathToSearchPaths() { + if gmode.IsDevelop() { + mainPkgPath := gfile.MainPkgPath() + if mainPkgPath != "" { + if !c.searchPaths.Contains(mainPkgPath) { + c.searchPaths.Append(mainPkgPath) + } + } + } +} + // getJson returns a *gjson.Json object for the specified `file` content. // It would print error if file reading fails. It return nil if any error occurs. func (c *Config) getJson(file ...string) *gjson.Json {