From 89188c03e10fc50432668bd4ac19943841e59a11 Mon Sep 17 00:00:00 2001 From: Adrian Gallagher Date: Mon, 22 Jan 2018 12:28:09 +1100 Subject: [PATCH] Fix config load failure when launching application on OSX through Finder --- common/common.go | 20 ++++++++++++++++++ config/config.go | 53 +++++++++++++++++++++++++++++------------------- 2 files changed, 52 insertions(+), 21 deletions(-) diff --git a/common/common.go b/common/common.go index 2717e727..4c6d4f80 100644 --- a/common/common.go +++ b/common/common.go @@ -20,8 +20,10 @@ import ( "net/http" "net/url" "os" + "path/filepath" "reflect" "regexp" + "runtime" "strconv" "strings" "time" @@ -446,3 +448,21 @@ func GetURIPath(uri string) string { } return urip.Path } + +// GetExecuteablePath returns the executables launch path +func GetExecutablePath() (string, error) { + ex, err := os.Executable() + if err != nil { + return "", err + } + return filepath.Dir(ex), nil +} + +// GetOSPathSlash returns the slash used by the operating systems +// file system +func GetOSPathSlash() string { + if runtime.GOOS == "windows" { + return "\\" + } + return "/" +} diff --git a/config/config.go b/config/config.go index 0ecf14a7..ab8e2790 100644 --- a/config/config.go +++ b/config/config.go @@ -387,32 +387,43 @@ func GetFilePath(file string) string { if file != "" { return file } - if flag.Lookup("test.v") == nil { - data, err := common.ReadFile(EncryptedConfigFile) - if err == nil { - if ConfirmECS(data) { - return EncryptedConfigFile - } - err = os.Rename(EncryptedConfigFile, ConfigFile) - if err != nil { - log.Fatalf("Unable to rename config file: %s", err) - } - log.Printf("Renaming non-encrypted config file from %s to %s", - EncryptedConfigFile, ConfigFile) - return ConfigFile + + if flag.Lookup("test.v") != nil { + return ConfigTestFile + } + + exePath, err := common.GetExecutablePath() + if err != nil { + log.Fatalf("Unable to get executable path: %s", err) + } + + tempPath := exePath + common.GetOSPathSlash() + encPath := tempPath + EncryptedConfigFile + cfgPath := tempPath + ConfigFile + + data, err := common.ReadFile(encPath) + if err == nil { + if ConfirmECS(data) { + return encPath } - if !ConfirmECS(data) { - return ConfigFile - } - err = os.Rename(ConfigFile, EncryptedConfigFile) + err = os.Rename(encPath, cfgPath) if err != nil { log.Fatalf("Unable to rename config file: %s", err) } - log.Printf("Renaming encrypted config file from %s to %s", ConfigFile, - EncryptedConfigFile) - return EncryptedConfigFile + log.Printf("Renaming non-encrypted config file from %s to %s", + encPath, cfgPath) + return cfgPath } - return ConfigTestFile + if !ConfirmECS(data) { + return cfgPath + } + err = os.Rename(cfgPath, encPath) + if err != nil { + log.Fatalf("Unable to rename config file: %s", err) + } + log.Printf("Renamed encrypted config file from %s to %s", cfgPath, + encPath) + return encPath } // ReadConfig verifies and checks for encryption and verifies the unencrypted