When calling viper.SafeWriteCofig() it fails with a file not found error.
Test code attached.
package main
import (
"fmt"
"os"
"github.com/spf13/viper"
)
func main() {
viper.AddConfigPath("/tmp")
viper.SetConfigName("filename")
viper.SetConfigType("yaml")
viper.SetDefault("LogLevel", "Info")
fmt.Println("Writing Config file!")
err := viper.SafeWriteConfig()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("wrote config file")
}
Output:
Writing Config file!
Config File "filename" Not Found in "[/tmp]"
Expected output:
Write Config file!
wrote config file
+1
Currently the documentation for SafeWriteConfig is quite misleading 馃槄
SafeWriteConfig writes current configuration to file only if the file does not exist.
This is issue is also present for SafeWriteConfigAs
This is fixed in https://github.com/spf13/viper/pull/450
Use viper.SetConfigFile("filename.yaml") instead of viper.SetConfigName("filename") then SafeWriteConfig and SafeWriteConfigAs() will work
Most helpful comment
This is fixed in https://github.com/spf13/viper/pull/450