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

config file can't be optional #1

Closed
joshuarubin opened this issue Apr 8, 2014 · 8 comments
Closed

config file can't be optional #1

joshuarubin opened this issue Apr 8, 2014 · 8 comments

Comments

@joshuarubin
Copy link
Contributor

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())
    }

    ...
@spf13
Copy link
Owner

spf13 commented Apr 8, 2014

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.

@joshuarubin
Copy link
Contributor Author

Closed in favor of #2

@stevenroose
Copy link

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

@balta2ar
Copy link

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?

@sagikazarmark
Copy link
Collaborator

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.

@alex-shpak
Copy link

alex-shpak commented Feb 20, 2019

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

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

@sagikazarmark
Copy link
Collaborator

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.

@alex-shpak
Copy link

Oh, yeah, you are right.
Scratch that.

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

6 participants