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

Simple way to initialize a config file #430

Open
michaeleekk opened this issue Dec 22, 2017 · 6 comments · May be fixed by #936
Open

Simple way to initialize a config file #430

michaeleekk opened this issue Dec 22, 2017 · 6 comments · May be fixed by #936

Comments

@michaeleekk
Copy link

michaeleekk commented Dec 22, 2017

Hello. I was searching for a way to initialize the config file, but still couldn't find one.

viper.SetConfigName("config")
viper.SetConfigType("json")
viper.AddConfigPath("$HOME")

And then I tried the new SafeWriteConfig() or any of the variety merged two weeks ago, but it didn't create new file or it will overwrite the original config file no matter what. And because of the $HOME, I might need to write a function to be cross-platform. I just wish there could be a cleaner way.

Or we could add O_CREATE to this line of code ?
https://github.com/spf13/viper/blob/master/viper.go#L1254

And also is it a good idea to have SafeWriteConfig() to create a new file with the first config path and config name ?

@revilwang
Copy link

Is there anyone working on this issue? 18 months passed and WriteConfig still requires the file must exist before writing, which is different from what the docs say.

@berlincount
Copy link

This seems to be fixed?

yann-soubeyrand added a commit to yann-soubeyrand/viper that referenced this issue Jul 14, 2020
yann-soubeyrand added a commit to yann-soubeyrand/viper that referenced this issue Jul 14, 2020
yann-soubeyrand added a commit to yann-soubeyrand/viper that referenced this issue Jul 14, 2020
yann-soubeyrand added a commit to yann-soubeyrand/viper that referenced this issue Jul 14, 2020
@yann-soubeyrand yann-soubeyrand linked a pull request Jul 14, 2020 that will close this issue
@kevingentile
Copy link

kevingentile commented Jul 21, 2020

v1.4.0: SafeWriteConfig() does not create a new config file:

File "config" Not Found in "[{path hidden}]"

Create and initialize with:

// stub init
configHome := "my/path"
configName := "config"
configType := "yml"
configPath := filepath.Join(configHome, configName+"."+configType)
// ----

viper.AddConfigPath(configHome)
viper.SetConfigName(configName)
viper.SetConfigType(configType)

_, err := os.Stat(configPath)
if !os.IsExist(err) {
    if _, err := os.Create(configPath); err != nil { // perm 0666
        // handle failed create
        ....
    }
}
if err := viper.SafeWriteConfig(); err != nil {
    // handle failed write
    ....
}

@errogaht
Copy link

errogaht commented Apr 8, 2022

as for me it is strange. i want to use viper to manage all cases with config files, but i have to manually create file, why?

@malohr
Copy link

malohr commented Apr 12, 2022

Hi there, not sure if this is still an issue but here is the simple solution you were looking for:

// using the home dir
home, err := os.UserHomeDir()
cobra.CheckErr(err)

configName := ".mycfg"

viper.WriteConfigAs(fmt.Sprintf("%s/%s", home, configName))

@marcosanchotene
Copy link

Adding the call to viper.SafeWriteConfig(), after configuring the path and file name, worked for me:

func initConfig() {
	if cfgFile != "" {
		// Use config file from the flag.
		viper.SetConfigFile(cfgFile)
	} else {
		// Find home directory.
		home, err := os.UserHomeDir()
		cobra.CheckErr(err)

		// Search config in home directory with name ".config.yaml".
		viper.AddConfigPath(home)
		viper.SetConfigType("yaml")
		viper.SetConfigName(".config")
		viper.SafeWriteConfig()
	}

}

yann-soubeyrand added a commit to yann-soubeyrand/viper that referenced this issue Feb 9, 2025
When a config file is set using SetConfigFile:
– WriteConfig failed if the file did not already exist,
– SafeWriteConfig did not use it.

Fixes spf13#430
Fixes spf13#433

Signed-off-by: Yann Soubeyrand <yann.soubeyrand@camptocamp.com>
yann-soubeyrand added a commit to yann-soubeyrand/viper that referenced this issue Feb 9, 2025
When a config file is set using SetConfigFile:
– WriteConfig failed if the file did not already exist,
– SafeWriteConfig did not use it.

Fixes spf13#430
Fixes spf13#433

Signed-off-by: Yann Soubeyrand <yann.soubeyrand@camptocamp.com>
yann-soubeyrand added a commit to yann-soubeyrand/viper that referenced this issue Feb 10, 2025
When a config file is set using SetConfigFile:
– WriteConfig failed if the file did not already exist,
– SafeWriteConfig did not use it.

Fixes spf13#430
Fixes spf13#433

Signed-off-by: Yann Soubeyrand <yann.soubeyrand@camptocamp.com>
yann-soubeyrand added a commit to yann-soubeyrand/viper that referenced this issue Feb 10, 2025
When a config file is set using SetConfigFile:
– WriteConfig failed if the file did not already exist,
– SafeWriteConfig did not use it.

Fixes spf13#430
Fixes spf13#433

Signed-off-by: Yann Soubeyrand <yann.soubeyrand@camptocamp.com>
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

Successfully merging a pull request may close this issue.

7 participants