From 76ed77663a88139b4e7f6e4bc2f134581c7311b6 Mon Sep 17 00:00:00 2001 From: Doug Lauder Date: Thu, 14 May 2020 20:11:22 -0400 Subject: [PATCH] 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 --- utils/fileutils/fileutils.go | 29 ++++++++++++++++++++++++++--- utils/i18n.go | 2 +- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/utils/fileutils/fileutils.go b/utils/fileutils/fileutils.go index 8e708c8c8f..62ad3ad83d 100644 --- a/utils/fileutils/fileutils.go +++ b/utils/fileutils/fileutils.go @@ -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 _, err := os.Stat(path); err == nil { return path @@ -27,9 +27,12 @@ func FindPath(path string, baseSearchPaths []string, filter func(os.FileInfo) bo } searchPaths := []string{} - searchPaths = append(searchPaths, baseSearchPaths...) + if workingDirFirst { + 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 if exe, err := os.Executable(); 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 { found, err := filepath.Abs(filepath.Join(parent, path)) if err != nil { @@ -65,6 +72,10 @@ func FindPath(path string, baseSearchPaths []string, filter func(os.FileInfo) bo 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 // directory as well as the directory of the executable. func FindFile(path string) string { @@ -85,3 +96,15 @@ func FindDir(dir string) (string, bool) { 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 +} diff --git a/utils/i18n.go b/utils/i18n.go index 347a4664b4..6668da5059 100644 --- a/utils/i18n.go +++ b/utils/i18n.go @@ -44,7 +44,7 @@ func InitTranslations(localizationSettings model.LocalizationSettings) error { } func InitTranslationsWithDir(dir string) error { - i18nDirectory, found := fileutils.FindDir(dir) + i18nDirectory, found := fileutils.FindDirRelBinary(dir) if !found { return fmt.Errorf("Unable to find i18n directory") }