From 1e12bb7f7cc271aeb3582e50e1c9355a2c9f51af Mon Sep 17 00:00:00 2001
From: 1138-4EB <1138-4EB@users.noreply.github.com>
Date: Thu, 16 Aug 2018 09:37:07 +0100
Subject: [PATCH] fix(config): ensure provided config path is used (#508)

---
 cmd/filebrowser/main.go | 48 ++++++++++++++++++++---------------------
 1 file changed, 24 insertions(+), 24 deletions(-)

diff --git a/cmd/filebrowser/main.go b/cmd/filebrowser/main.go
index d1c3d634..e2bb787e 100644
--- a/cmd/filebrowser/main.go
+++ b/cmd/filebrowser/main.go
@@ -117,9 +117,6 @@ func setupViper() {
 	viper.BindPFlag("AlternativeRecaptcha", flag.Lookup("alternative-recaptcha"))
 	viper.BindPFlag("ReCaptchaKey", flag.Lookup("recaptcha-key"))
 	viper.BindPFlag("ReCaptchaSecret", flag.Lookup("recaptcha-secret"))
-
-	viper.SetConfigName("filebrowser")
-	viper.AddConfigPath(".")
 }
 
 func printVersion() {
@@ -127,6 +124,29 @@ func printVersion() {
 	os.Exit(0)
 }
 
+func initConfig() {
+	// Add a configuration file if set.
+	if config != "" {
+		cfg := strings.TrimSuffix(config, filepath.Ext(config))
+		if dir := filepath.Dir(cfg); dir != "" {
+			viper.AddConfigPath(dir)
+			cfg = strings.TrimPrefix(cfg, dir)
+		}
+		viper.SetConfigName(cfg)
+	} else {
+		viper.SetConfigName("filebrowser")
+		viper.AddConfigPath(".")
+	}
+
+	// Read configuration from a file if exists.
+	err := viper.ReadInConfig()
+	if err != nil {
+		if _, ok := err.(viper.ConfigParseError); ok {
+			panic(err)
+		}
+	}
+}
+
 func main() {
 	setupViper()
 	flag.Parse()
@@ -135,27 +155,7 @@ func main() {
 		printVersion()
 	}
 
-	// Add a configuration file if set.
-	if config != "" {
-		ext := filepath.Ext(config)
-		dir := filepath.Dir(config)
-		config = strings.TrimSuffix(config, ext)
-
-		if dir != "" {
-			viper.AddConfigPath(dir)
-			config = strings.TrimPrefix(config, dir)
-		}
-
-		viper.SetConfigName(config)
-	}
-
-	// Read configuration from a file if exists.
-	err := viper.ReadInConfig()
-	if err != nil {
-		if _, ok := err.(viper.ConfigParseError); ok {
-			panic(err)
-		}
-	}
+	initConfig();
 
 	// Set up process log before anything bad happens.
 	switch viper.GetString("Logger") {