hello,i code the example:
viper.SetConfigType("yaml")
//viper.SetConfigFile("/etc/config.yaml")
viper.SetConfigName("config.yaml")
viper.AddConfigPath("/etc")
err := viper.ReadInConfig()
if err != nil {
fmt.Println(err)
}
or
viper.SetConfigType("yaml")
viper.SetConfigFile("config.yaml")
//viper.SetConfigName("config.yaml")
viper.AddConfigPath("/etc")
err := viper.ReadInConfig()
if err != nil {
fmt.Println(err)
}
i'm sure that i have config.yaml in /etc dirctionary,but the outputs of these code are:
Config File "config.yaml" Not Found in "[/etc]"
open config.yaml: no such file or directory
what's wrong?
I'm not sure but maybe try
viper.SetConfigType("yaml")
viper.SetConfigName("config")
viper.AddConfigPath("/etc")
err := viper.ReadInConfig()
if err != nil {
fmt.Println(err)
}
or try
viper.SetConfigFile("config.yaml")
viper.AddConfigPath("/etc")
err := viper.ReadInConfig()
if err != nil {
fmt.Println(err)
}
I confirm the issue with "not found" configuration. The proposed solution doesn't solve it.
It looks that config name should be without any extension (viper.SetConfigName(...)) while the actual file should have any supported extension. Take a look into https://github.com/spf13/viper/blob/d9cca5ef33035202efb1586825bdbb15ff9ec3ba/viper.go#L1536
@nad2000 is correct. searchInPath concatenates search path, file name and extension. Which appears to be the intended behaviour. filepath.Join(in, v.configName+"."+ext)
So viper will search, your filepaths + filename + extension:
/Users/example/go/src/github.com/your-app/config.json
/Users/example/go/src/github.com/your-app/config.toml
/Users/example/go/src/github.com/your-app/config.yaml
/Users/example/go/src/github.com/your-app/config.yml
/Users/example/go/src/github.com/your-app/config.properties
/Users/example/go/src/github.com/your-app/config.props
/Users/example/go/src/github.com/your-app/config.prop
/Users/example/go/src/github.com/your-app/config.hcl
Instead, what you could do is specify the file type yourself via:
viper.SetConfigType("yaml")
and pass in the absolute path to your config file via:
viper.SetConfigFile("/etc/configs/config")
or if the config file will always be in the same directory as main application:
viper.SetConfigFile("config")
How much time did I waste on this!? It should be documented clearly that specifying what data serialization format your config file uses, will modify the name that is searched for by appending an extension to the file name.
Message removed... I messed up with the folder names... :disappointed_relieved:
Instead of using AddConfigPath try the following:
// set the config file type
viper.SetConfigType("yaml")
// specify where the config file is
viper.SetConfigFile("./config/config") // or viper.SetConfigFile("config/config"), doesn't matter
// read in config file and check your errors
if err := viper.ReadInConfig(); err != nil {
// handle errors
}
// confirm where the file has been read in from
fmt.Println(viper.ConfigFileUsed())
This looks real issue as it always require complete path with file name for search. AddConfigPath() doesn't work in the case where config file is in other directory. @mfridman @nad2000
for me SetConfigName and AddConfigPath without SetConfigFile works
my conf file is at /secrets/app.yaml
Config.SetConfigType("yaml")
Config.AddConfigPath("/secrets/")
Config.SetConfigName("app")
err := Config.ReadInConfig()
this is because if you set ConfigFile it takes the file as the absolute path instead of searching for that file in the list of config paths
https://github.com/spf13/viper/blob/d9cca5ef33035202efb1586825bdbb15ff9ec3ba/viper.go#L1521-L1534
any update now?
Instead of using
AddConfigPathtry the following:// set the config file type viper.SetConfigType("yaml") // specify where the config file is viper.SetConfigFile("./config/config") // or viper.SetConfigFile("config/config"), doesn't matter // read in config file and check your errors if err := viper.ReadInConfig(); err != nil { // handle errors } // confirm where the file has been read in from fmt.Println(viper.ConfigFileUsed())
Thanks!! I solve my issue with this reply
Instead of using
AddConfigPathtry the following:// set the config file type viper.SetConfigType("yaml") // specify where the config file is viper.SetConfigFile("./config/config") // or viper.SetConfigFile("config/config"), doesn't matter // read in config file and check your errors if err := viper.ReadInConfig(); err != nil { // handle errors } // confirm where the file has been read in from fmt.Println(viper.ConfigFileUsed())Thanks!! I solve my issue with this reply
Neither Get nor SetConfigFile is working for me. I keep getting 'no such file'
Instead of using
AddConfigPathtry the following:// set the config file type viper.SetConfigType("yaml") // specify where the config file is viper.SetConfigFile("./config/config") // or viper.SetConfigFile("config/config"), doesn't matter // read in config file and check your errors if err := viper.ReadInConfig(); err != nil { // handle errors } // confirm where the file has been read in from fmt.Println(viper.ConfigFileUsed())Thanks!! I solve my issue with this reply
Neither Get nor SetConfigFile is working for me. I keep getting 'no such file'
Use this instead....
viper.SetConfigType("yaml")
viper.SetConfigName("configName")
viper.AddConfigPath("/path/to/config/file")
err := viper.ReadInConfig()
if err != nil {
// Handle Error
...
}
It's looks like viper.AddConfigPath(".") dosn't work correct now, must be specified a absolut path...
It's looks like
viper.AddConfigPath(".")dosn't work correct now, must be specified a absolut path...
But it doesn't work
The same problem happen to me. I use following method solve it.
viper.SetConfigType("yaml")
viper.SetConfigFile("/path/to/config/file/configName")
err := viper.ReadInConfig()
if err != nil {
// Handle Error
...
}
You can add multiple viper.AddConfigPath()lines based on commented code, https://github.com/spf13/viper/blob/master/viper.go#L411
viper.AddConfigPath("./")
viper.AddConfigPath("../")
@sagikazarmark this should be reopened as I mistakenly marked #722 as solving #390 . Thanks to @andig (https://github.com/spf13/viper/pull/722#issuecomment-572988072) for bringing this to attention.
Unfortunately, I can't really tell anymore what this issue is about, but I get the feeling it's rather a documentation problem than an actual bug in the code.
Re-reading this thread it seems the problem might be in the AddConfigPath as this comment https://github.com/spf13/viper/issues/390#issuecomment-511177615 seems to suggest
Assume your configs file in /home/ubuntu/go/bin/myapp/configs/conf.env
Your code should provide :
if run from /home/ubuntu/go/bin/myapp/configs/ then set viper.AddConfigPath(".")
if run from /home/ubuntu/go/bin/myapp/ then set viper.AddConfigPath("./configs")
if run from /home/ubuntu/go/bin/ then set viper.AddConfigPath("./myapp/configs")
if run from root of Ubuntu machine (such as make it as a service)
then set viper.AddConfigPath("/home/ubuntu/go/bin/myapp/configs/")
It doesn't work for production version
Works for me in production:
_, filePath, _, _ := runtime.Caller(0)
configFile := filePath[:len(filePath)-9]
viper.SetConfigFile(configFile + string(filepath.Separator) + ".env")
err := viper.ReadInConfig()
Just had following problem:(using an existing configfile "myConfig.json")
v := viper.New()
v.SetConfigName("myConfig.json")
v.SetConfigType("json")
v.AddConfigPath(".")
err := v.ReadInConfig()
if err != nil {
log.Panicf("Fatal error config file: %s", err)
}
Without "SetConfigType" the error is thrown ...
v := viper.New()
v.SetConfigName("myConfig.json")
// v.SetConfigType("json") => Omitting setting config type, leads to error condition, unless config name contains extension
v.AddConfigPath(".")
err := v.ReadInConfig()
if err != nil {
log.Panicf("Fatal error config file: %s", err)
}
Running: go version go1.14.2 windows/amd64
Worked in Win10 (using GoLand)!! 馃槂
// Set the file name of the configurations file
viper.SetConfigName("config")
// Set the configuration file type
viper.SetConfigType("yaml")
// Set the path to look for the configurations file
viper.AddConfigPath("./config")
// Find and read the config file
err := viper.ReadInConfig()
if err != nil {
fmt.Printf("Error reading config file, %s", err)
return err
}
BTW viper.SetConfigName("config.yml") did not work.
Can confirm viper.SetConfigFile does not work, the way to fix it was to add the directory the configuration would be present in using AddConfigPath
Most helpful comment
I'm not sure but maybe try
or try