MM-23706 search executable dir before working dir when looking for i18n (#14241)

* MM-23706 search executable dir before working dir when looking for i18n
Этот коммит содержится в:
Doug Lauder
2020-05-14 20:11:22 -04:00
коммит произвёл GitHub
родитель e6bbb4bd7a
Коммит 76ed77663a
2 изменённых файлов: 27 добавлений и 4 удалений

Просмотреть файл

@@ -17,7 +17,7 @@ var (
} }
) )
func FindPath(path string, baseSearchPaths []string, filter func(os.FileInfo) bool) string { func findPath(path string, baseSearchPaths []string, workingDirFirst bool, filter func(os.FileInfo) bool) string {
if filepath.IsAbs(path) { if filepath.IsAbs(path) {
if _, err := os.Stat(path); err == nil { if _, err := os.Stat(path); err == nil {
return path return path
@@ -27,9 +27,12 @@ func FindPath(path string, baseSearchPaths []string, filter func(os.FileInfo) bo
} }
searchPaths := []string{} searchPaths := []string{}
if workingDirFirst {
searchPaths = append(searchPaths, baseSearchPaths...) searchPaths = append(searchPaths, baseSearchPaths...)
}
// Additionally attempt to search relative to the location of the running binary. // Attempt to search relative to the location of the running binary either before
// or after searching relative to the working directory, depending on `workingDirFirst`.
var binaryDir string var binaryDir string
if exe, err := os.Executable(); err == nil { if exe, err := os.Executable(); err == nil {
if exe, err = filepath.EvalSymlinks(exe); err == nil { if exe, err = filepath.EvalSymlinks(exe); err == nil {
@@ -47,6 +50,10 @@ func FindPath(path string, baseSearchPaths []string, filter func(os.FileInfo) bo
} }
} }
if !workingDirFirst {
searchPaths = append(searchPaths, baseSearchPaths...)
}
for _, parent := range searchPaths { for _, parent := range searchPaths {
found, err := filepath.Abs(filepath.Join(parent, path)) found, err := filepath.Abs(filepath.Join(parent, path))
if err != nil { if err != nil {
@@ -65,6 +72,10 @@ func FindPath(path string, baseSearchPaths []string, filter func(os.FileInfo) bo
return "" return ""
} }
func FindPath(path string, baseSearchPaths []string, filter func(os.FileInfo) bool) string {
return findPath(path, baseSearchPaths, true, filter)
}
// FindFile looks for the given file in nearby ancestors relative to the current working // FindFile looks for the given file in nearby ancestors relative to the current working
// directory as well as the directory of the executable. // directory as well as the directory of the executable.
func FindFile(path string) string { func FindFile(path string) string {
@@ -85,3 +96,15 @@ func FindDir(dir string) (string, bool) {
return found, true return found, true
} }
// FindDirRelBinary looks for the given directory in nearby ancestors relative to the
// directory of the executable, then relative to the working directory, falling back to `./` if not found.
func FindDirRelBinary(dir string) (string, bool) {
found := findPath(dir, commonBaseSearchPaths, false, func(fileInfo os.FileInfo) bool {
return fileInfo.IsDir()
})
if found == "" {
return "./", false
}
return found, true
}

Просмотреть файл

@@ -44,7 +44,7 @@ func InitTranslations(localizationSettings model.LocalizationSettings) error {
} }
func InitTranslationsWithDir(dir string) error { func InitTranslationsWithDir(dir string) error {
i18nDirectory, found := fileutils.FindDir(dir) i18nDirectory, found := fileutils.FindDirRelBinary(dir)
if !found { if !found {
return fmt.Errorf("Unable to find i18n directory") return fmt.Errorf("Unable to find i18n directory")
} }