Fix config load failure when launching application on OSX through Finder

This commit is contained in:
Adrian Gallagher
2018-01-22 12:28:09 +11:00
parent 226a79e6e0
commit 89188c03e1
2 changed files with 52 additions and 21 deletions

View File

@@ -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 "/"
}

View File

@@ -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