Viper: WriteConfig fails to write if file does not exist

Created on 9 Jan 2018  路  3Comments  路  Source: spf13/viper

The new WriteConfig function ends up calling findConfigFile, which in turn checks whether the file exists. If it doesn't then it returns an error, which stops the config being written.

Most helpful comment

I got same issue.
I think WriteConfig should create configfile as same as WriteConfigAs if the file does not exist

All 3 comments

I had similar issue too and filed as #430 and right now my workaround is like this,

if err := viper.SafeWriteConfigAs(configPath); err != nil {
    if os.IsNotExist(err) {
        err = viper.WriteConfigAs(configPath)
    }
}

I'm just hoping there is a better solution.

Use WriteConfigAs which creates the file before writing.

I got same issue.
I think WriteConfig should create configfile as same as WriteConfigAs if the file does not exist

Was this page helpful?
0 / 5 - 0 ratings