Viper: config file can't be optional

Created on 8 Apr 2014  Â·  8Comments  Â·  Source: spf13/viper

It would be more useful if ReadInConfig could return an error instead of causing the process to Exit if the config can't be found. This would enable the case where the whole file can be optional and the values read from defaults.

func ReadInConfig() {
    jww.INFO.Println("Attempting to read in config file")

    if !stringInSlice(getConfigType(), SupportedExts) {
        jww.ERROR.Fatalf("Unsupported Config Type %q", getConfigType())
    }

    ...

Most helpful comment

Leaving the solution here for future reference:

    err := viper.ReadInConfig()
    if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
        panic(err)
    }

Basically you just have to check the error and ignore it if it is a config not found error.

All 8 comments

Agree. Please Send me a pull request.

Steve Francia
spf13.com
@spf13

On Apr 8, 2014, at 4:18 AM, Joshua Rubin [email protected] wrote:

It would be more useful if ReadInConfig could return an error instead of causing the process to Exit if the config can't be found. This would enable the case where the whole file can be optional and the values read from defaults.

func ReadInConfig() {
jww.INFO.Println("Attempting to read in config file")

if !stringInSlice(getConfigType(), SupportedExts) {
    jww.ERROR.Fatalf("Unsupported Config Type %q", getConfigType())
}

...

—
Reply to this email directly or view it on GitHub.

Closed in favor of #2

So it is really not possible to have Viper not error when the config file does not exist?

So it is really not possible to have Viper not error when the config file does not exist?

I have the same question... @spf13 @joshuarubin?

Leaving the solution here for future reference:

    err := viper.ReadInConfig()
    if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
        panic(err)
    }

Basically you just have to check the error and ignore it if it is a config not found error.

~If somebody interested, alternative solution~ It's not the same

if err := config.ReadConfig(strings.NewReader("")); err != nil {
    log.Fatal(err)
}

Hm, calling ReadConfig is totally optional, you only need it if you want to read a config file, so I'm failing to see how this helps.

Oh, yeah, you are right.
Scratch that.

Was this page helpful?
0 / 5 - 0 ratings