Exit with error if the requested configuration file does not exist
authorMikael Berthe <mikael@lilotux.net>
Wed, 03 May 2017 22:58:57 +0200
changeset 65 0491cc43911e
parent 64 0a3663970622
child 66 43d43c8b53aa
Exit with error if the requested configuration file does not exist Thanks to Alexis Vachette for the testing.
cmd/root.go
--- a/cmd/root.go	Wed May 03 22:58:37 2017 +0200
+++ b/cmd/root.go	Wed May 03 22:58:57 2017 +0200
@@ -183,7 +183,13 @@
 	viper.AutomaticEnv()
 
 	// If a config file is found, read it in.
-	if err := viper.ReadInConfig(); viper.GetBool("verbose") && err == nil {
+	err := viper.ReadInConfig()
+	if err != nil {
+		if cfgFile != "" {
+			errPrint("Error: cannot read configuration file '%s'", cfgFile)
+			os.Exit(-1)
+		}
+	} else if viper.GetBool("verbose") {
 		errPrint("Using config file: %s", viper.ConfigFileUsed())
 	}
 }