Comments (1)
Hi there
So if i understand this currently, you need to load a config file into your application but some of the variables need to be set by environment variables.
I had a similar experience and this little trick may be helpful:
if errConfig := cleanenv.ReadConfig("config.yaml", configuration); errConfig != nil {
if errEnv := cleanenv.ReadEnv(configuration); errEnv != nil {
return nil, multierr.Combine(errConfig, errEnv)
}
}
Basically you can load your configs from a config.yaml file but you can also load some other variables from env after that and you can use the uber's "multierr" package to combine ReadConfig and ReadEnv errors together. Here's an example of the struct that i used:
type Configuration struct {
Dialer struct {
UpstreamTimeout string `env:"DIALER_UPSTREAM_TIMEOUT" env-default:"2s"`
KeepAliveDuration string `env:"DIALER_KEEPALIVE_DURATION" env-default:"5s"`
InsecureSkipVerify bool `env:"DIALER_INSECURE_SKIP_VERIFY" env-default:"false"`
}
DNS struct {
Resolver string `env:"DNS_RESOLVER" env-default:"8.8.8.8:53"`
Protocol string `env:"DNS_PROTOCOL" env-default:"udp"`
Timeout string `env:"DNS_TIMEOUT" env-default:"10ms"`
}
Log struct {
LogLevel string `env:"LOG_LEVEL" env-default:"info"`
SentryDSN string `env:"SENTRY_DSN"`
TimePrecision uint `env:"LOG_TIMEPRECISION" env-default:"3"`
}
Storage struct {
Endpoints []struct {
Name string `yaml:"name"`
Endpoint string `yaml:"endpoint"`
} `yaml:"endpoints"`
Key struct {
ServiceAlpha struct {
Access string `env:"STORAGE_ALPHA_ACCESS"`
Secret string `env:"STORAGE_ALPHA_SECRET"`
}
ServiceBeta struct {
Access string `env:"STORAGE_BETA_ACCESS"`
Secret string `env:"STORAGE_BETA_SECRET"`
}
}
} `yaml:"storage"`
}
As you can see i have a combination of yaml configs and env configs.
Here's the config.yaml as well:
storage:
endpoints:
- name: Main
endpoint: https://s3.us-east-2.amazonaws.com
- name: Fallback
endpoint: https://s3.us-west-2.amazonaws.com
Hope this helps.
from cleanenv.
Related Issues (20)
- Create a version compatibility policy
- Didn't skip unexported field HOT 4
- Apply Custom Value Setters to custom struct types
- Does this support parsing `map[string]CustomStruct` using the `env` tag? HOT 2
- What is the correct location for the config.yml? HOT 4
- Set but empty Environment Variables don't raise an error HOT 1
- Nested structures not sorted HOT 2
- [][]string from env HOT 1
- Setup env parameter for array in config? HOT 1
- Config returns nothing
- Update GetDescription to show which variables are required
- print the name of the env var in the error message when a required var is missing rather than just the struct field name HOT 1
- Allow reading from `fs.FS`
- Return wrapped error with "%w" verb instead of "%s"
- File decodes based on extension, meaning it fails to load config if `.rc` files are used
- env-required pretty limited usage
- Example of using map of structs in config, example of using `env-separator`
- Can not overwrite variable HOT 5
- Can't read config file in Amazon EC2 server HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cleanenv.