Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SafeWriteConfig() fails if file does not exist #448

Open
TWinsnes opened this issue Jan 28, 2018 · 3 comments
Open

SafeWriteConfig() fails if file does not exist #448

TWinsnes opened this issue Jan 28, 2018 · 3 comments

Comments

@TWinsnes
Copy link

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
@tskardal
Copy link

tskardal commented Mar 12, 2018

+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

@Vaelatern
Copy link

This is fixed in #450

@sureshannadurai
Copy link

Use viper.SetConfigFile("filename.yaml") instead of viper.SetConfigName("filename") then SafeWriteConfig and SafeWriteConfigAs() will work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants