Fix --config flag

This commit is contained in:
Richard Patel 2019-02-03 03:17:22 +01:00
parent 120c026983
commit 0ca6deede8
No known key found for this signature in database
GPG Key ID: C268B2BBDA2ABECB

View File

@ -7,7 +7,6 @@ import (
"github.com/spf13/viper" "github.com/spf13/viper"
"io" "io"
"os" "os"
"path/filepath"
"strings" "strings"
"time" "time"
) )
@ -135,17 +134,23 @@ func prepareConfig() {
func readConfig() { func readConfig() {
// If config.yml in working dir, use it // If config.yml in working dir, use it
if _, err := os.Stat("config.yml"); err == nil { if configFile == "" {
_, err := os.Stat("config.yml")
if err == nil {
configFile = "config.yml" configFile = "config.yml"
} }
}
if configFile != "" { if configFile != "" {
var err error confF, err := os.Open(configFile)
confPath, err := filepath.Abs(configFile) if err != nil {
if err != nil { panic(err) } fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
defer confF.Close()
viper.SetConfigFile(confPath) viper.SetConfigType("yml")
err = viper.ReadInConfig() err = viper.ReadConfig(confF)
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
os.Exit(1) os.Exit(1)