Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ncrypthic/goconf
Wrapper around github.com/spf13/viper to work with struct tag
https://github.com/ncrypthic/goconf
Last synced: 13 days ago
JSON representation
Wrapper around github.com/spf13/viper to work with struct tag
- Host: GitHub
- URL: https://github.com/ncrypthic/goconf
- Owner: ncrypthic
- Created: 2017-01-05T10:54:27.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2017-01-14T04:51:38.000Z (almost 8 years ago)
- Last Synced: 2024-10-11T18:32:47.006Z (about 1 month ago)
- Language: Go
- Size: 8.79 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Golang configuration library
This library uses [spf13/viper](http://github.com/spf13/viper) configuration library
to reading configuration from files dan environmnet variables.## Usage
- Create configuration file
```yaml
# ./config.yaml
mysql:
username: "a user"
password: "a password"
connections:
- {name: "default", port: 3306}
- {name: "test", port: 13306}
```- Get the package using `go get github.com/ncrypthic/goconf` or `glide get github.com/ncrypthic/goconf`
- Import the module
```golang
package mainimport (
"fmt"
"github.com/ncrypthic/goconf"
)
```- Create struct with `config` tag
```golang
type Connection struct {
Name string `config:"name"` // "name" config for every mysql.connections element
Port int `config:"port"` // "port" value for every mysql.connections element
}type MySQL struct {
Username string `config:"mysql.hostname"`
Password string `config:"mysql.hort"`
Connections []Connection `config:"mysql.connections"`
}
```- Explicitly call `GetConfig` method to get configuration values
```golang
func main() {
mysql := new(MySQL)
config.GetConfig(mysql)
fmt.Println(mysql.Username)
fmt.Println(mysql.Password)
fmt.Println(mysql.Connections)
// Outputs
// a user
// a password
// [{default, 1306}, {test, 13306}]
}
```- The module will search for environment variables with prefix `CONF_` followed by configuration path (all uppercased and any `.` in path must be replaced by `_`)
```sh
CONF_MYSQL_USERNAME="test" go run main.go
```will give output:
```
test
a password
[{default, 3306}, {test, 13306}]
```## Test
Run `glide up` in the module directory to install depedency. Then run `go test` to test the module