Ecosyste.ms: Packages

An open API service providing package, version and dependency metadata of many open source software ecosystems and registries.

Top 8.2% on proxy.golang.org

proxy.golang.org : github.com/dim/envconfig

Package envconfig implements a configuration reader which reads each value from an environment variable. The basic idea is that you define a configuration struct, like this: Once you have that, you need to initialize the configuration: Then it's just a matter of setting the environment variables when calling your binary: Your conf struct must follow the following rules: By default, envconfig generates all possible keys based on the field chain according to a flexible naming scheme. The field chain is how you access your field in the configuration struct. For example: With that struct, you access the name field via the chain *Shard.Name* The default naming scheme takes that and transforms it into the following: It can handles more complicated cases, with multiple words in one field name. It needs to be in the correct case though, for example: With that struct, you access the name field via the chain *Cassandra.SSLCert* or *Cassandra.SslKey* The default naming scheme takes that and transforms it into the following: And, if that is not good enough for you, you always have the option to use a custom key: Now envconfig will only ever checks the environment variable _cassandraMyName_. There are three types of content for a single variable: Example of a valid slice value: The format for a struct is as follow: Example of a valid struct value: Example of a valid slice of struct values: For bytes slices, you generally don't want to type out a comma-separated list of byte values. For this use case, we support base64 encoded values. Here's an example: This will decode DATA to FOOBAR and put that into conf.Data. Sometimes you don't absolutely need a value. Here's how we tell envconfig a value is optional: The two syntax are equivalent. Often times you have configuration keys which almost never changes, but you still want to be able to change them. In such cases, you might want to provide a default value. Here's to do this with envconfig: You can of course combine multiple options. The syntax is simple enough, separate each option with a comma. For example: This would give you the default timeout of 1 minute, and lookup the myTimeout environment variable. envconfig supports the following list of types: Notably, we don't (yet) support complex types simply because I had no use for it yet. When the standard types are not enough, you will want to use a custom unmarshaler for your types. You do this by implementing Unmarshaler on your type. Here's an example:

Registry - Source - Documentation - JSON
purl: pkg:golang/github.com/dim/envconfig
License: MIT
Latest release: almost 8 years ago
First release: almost 8 years ago
Namespace: github.com/dim
Stars: 0 on GitHub
Forks: 0 on GitHub
See more repository details: repos.ecosyste.ms
Last synced: 23 days ago

    Loading...
    Readme
    Loading...