Skip to content

Commit

Permalink
Fix config load failure when launching application on OSX through Finder
Browse files Browse the repository at this point in the history
  • Loading branch information
thrasher- committed Jan 22, 2018
1 parent 226a79e commit 89188c0
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 21 deletions.
20 changes: 20 additions & 0 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ import (
"net/http"
"net/url"
"os"
"path/filepath"
"reflect"
"regexp"
"runtime"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -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 "/"
}
53 changes: 32 additions & 21 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 !ConfirmECS(data) {
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
}
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
}
if !ConfirmECS(data) {
return cfgPath
}
err = os.Rename(cfgPath, encPath)
if err != nil {
log.Fatalf("Unable to rename config file: %s", err)
}
return ConfigTestFile
log.Printf("Renamed encrypted config file from %s to %s", cfgPath,
encPath)
return encPath
}

// ReadConfig verifies and checks for encryption and verifies the unencrypted
Expand Down

0 comments on commit 89188c0

Please sign in to comment.