Skip to content

configFileNotFound bug? #141

@ghostsquad

Description

@ghostsquad

I was just reviewing the code, and I found a part that looks a little suspect..

https://github.com/sagikazarmark/modern-go-application/blob/master/cmd/modern-go-application/main.go?ts=4#L89

if ReadInConfig does not return an error, then configFileNotFound would be false, right? since the type assert from nil to viper.ConfigFileNotFoundError would fail?

err := v.ReadInConfig()
_, configFileNotFound := err.(viper.ConfigFileNotFoundError)
if !configFileNotFound {
	emperror.Panic(errors.Wrap(err, "failed to read configuration"))
}

# more stuff, including configuring the logger

if configFileNotFound {
	logger.Warn("configuration file not found")
}

it seems like the code will either always panic, or log a warning.

The viper shows the correct way: https://github.com/spf13/viper?ts=4#reading-config-files

if err := viper.ReadInConfig(); err != nil {
    if _, ok := err.(viper.ConfigFileNotFoundError); ok {
        // Config file not found; ignore error if desired
    } else {
        // Config file was found but another error was produced
    }
}

// Config file found and successfully parsed

Additionally, I don't think this is best practice anyway, since a configuration file should not be required if ENV variables are also an alternative. Maybe I'm also reading the code wrong...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions