-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
44 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
package config | ||
|
||
import ( | ||
"os" | ||
|
||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/viper" | ||
) | ||
|
@@ -53,7 +55,7 @@ func Load() Config { | |
if _, ok := err.(viper.ConfigFileNotFoundError); ok { | ||
log.Warnf("No config file found in search paths, using default values") | ||
} else { | ||
log.Fatalf("Error reading config: %+v", err) | ||
log.Fatalf("Error reading config: %s", err) | ||
} | ||
} | ||
|
||
|
@@ -66,6 +68,30 @@ func Load() Config { | |
return conf | ||
} | ||
|
||
func LoadFile(configFile string) Config { | ||
var conf Config | ||
|
||
f, err := os.OpenFile(configFile, os.O_RDONLY, 0444) | ||
|
||
if err != nil { | ||
log.Fatalf("Failed to open config file: %s", err) | ||
} | ||
|
||
defer f.Close() | ||
|
||
if err := viper.ReadConfig(f); err != nil { | ||
This comment was marked as resolved.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
maddie
Author
Collaborator
|
||
log.Fatalf("Error reading config: %s", err) | ||
} | ||
|
||
if err := viper.Unmarshal(&conf); err != nil { | ||
log.Fatalf("Error parsing config: %s", err) | ||
} | ||
|
||
loadedConfig = &conf | ||
|
||
return conf | ||
} | ||
|
||
func LoadedConfig() *Config { | ||
if loadedConfig == nil { | ||
Load() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
viper.ReadConfig
does not seem to work.Just after
viper.ReadConfig
, I calledfmt.Println(viper.AllSettings())
and it showed default values rather than those read fromconfigFile
.