- Branches
master2015-02-14
envcfg 
Package envcfg provides environment variable mapping to structs.
It can be used to read configuration parameters from the environment.
Fields for which environment variables can be found are overwritten, otherwise they are left to their previous value.
This package can be used, for example, after gcfg to override settings provided in a configuration file.
import "github.com/Pacien/envcfg"Documentation
Package documentation can be found on GoDoc
Usage example
Set environment variables:
export PORT=8080
export USER_PASSWORD="S3cUrE"Create a struct optionally with tagged fields and/or already set
values, then call the ReadInto function to read the values
set in the environment variables.
package main
import (
"fmt"
"github.com/Pacien/envcfg"
)
type Config struct {
Server struct {
Port int `env:"PORT" absenv:"true"`
}
User struct {
Username string
Password string
}
}
var cnf Config
func (c *Config) setDefaults() *Config {
c.User.Username = "root"
c.User.Password = "password"
return c
}
func init() {
cnf.setDefaults()
_, errs := envcfg.ReadInto(&cnf)
if len(errs) != 0 {
fmt.Println(errs)
}
}
func main() {
fmt.Println(cnf)
}Output of the previous program:
{{8080} {root S3cUrE}}See tests for other examples.
License
This program is published under the MIT License. See the LICENSE.txt file.
Other documents
LICENSE.txt
The MIT License (MIT) Copyright (c) 2015 Pacien TRAN-GIRARD Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.