2017-12-12 21:51:25 +07:00
## Introduction
2017-12-12 22:33:47 +07:00
**config** is a simple golang library and designed to read configurations from JSON, Yaml files, environment variables and command line. **config** depends on [go-yaml ](https://github.com/go-yaml/yaml ) to anlayze Yaml file and uses built-in golang library to handle JSON file.
2017-12-12 21:51:25 +07:00
## Installation
1. Install [Yaml ](https://github.com/go-yaml/yaml ) library first:
```
go get gopkg.in/yaml.v2
```
2017-12-12 22:18:55 +07:00
2. Install **config** library:
2017-12-12 21:51:25 +07:00
```
go get github.com/eschao/config
```
## Usage
2017-12-13 08:33:21 +07:00
### I. Define configuration name in structure tags
Like JSON, Yaml, **config** uses tags to define configurations:
| Tag | Example | Function |
|-----|---------|------|
| json | Host string `json:"host"` | Maps `Host` to a JSON field: **host** |
| yaml | Host string `yaml:"host"` | Maps `Host` to a Yaml field: **host** |
| env | Host string `env:"HOST"` | Maps `Host` to a Environment variable: **HOST** |
| cli | Host string `cli:"host database host"` | Maps `Host` to a command line argument: ** -host** or ** --host** |
| default | Port int `default:"8080"` | Defines the port with default value: **8080** |
2017-12-13 08:52:27 +07:00
| separator | Path string `json:"path" separator:";"` | Separator is used to split string to a slice |
2017-12-13 08:33:21 +07:00
#### 1. Data types
**config** supports the following golang data types:
2017-12-12 23:27:48 +07:00
* bool
* string
* int8, int16, int, int32, int64
* uint8, uint16, uint, uint32, uint64
* float32, float64
* slice type. e.g: []string, []int ...
2017-12-13 08:33:21 +07:00
#### 2. Defines **default** values
2017-12-13 08:52:27 +07:00
Using **default** keyword in structure tags to define default value:
2017-12-12 22:33:47 +07:00
```golang
type Log struct {
Path string `default:"/var/logs"`
Level string `default:"debug"`
}
```
2017-12-12 23:27:48 +07:00
After that, calls ```config.ParseDefault(interface{})``` to set structure instance with default values. Example codes:
2017-12-12 22:33:47 +07:00
```golang
logConfig := Log{}
config.ParseDefault(& logConfig)
```
2017-12-13 08:33:21 +07:00
#### 3. Defines configruation name for JSON
2017-12-13 08:52:27 +07:00
Like parsing JSON object, using **json** keyword to define configuration name:
2017-12-12 22:18:55 +07:00
```golang
type Database struct {
2017-12-12 22:33:47 +07:00
Host string `json:"host"`
Port int `json:"port"`
Username string `json:"username" default:"admin"`
Password string `json:"password" default:"admin"`
Log Log `json:"log"`
2017-12-12 22:18:55 +07:00
}
```
2017-12-13 08:52:27 +07:00
Corresponding JSON file:
```json
{
"host": "test.db.hostname"
"port": 8080
"username": "amdin"
"password": "admin"
"log": {
"path": "/var/logs/db"
"level": "debug"
}
}
```
2017-12-13 08:33:21 +07:00
#### 4. Defines configuration name for Yaml
Like parsing Yaml object, using **yaml** keyword to define configuration name
```golang
type Database struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
Username string `yaml:"username" default:"admin"`
Password string `yaml:"password" default:"admin"`
Log Log `yaml:"log"`
}
```
2017-12-13 08:52:27 +07:00
Corresponding Yaml file:
```yaml
host: test.db.hostname
port: 8080
username: amdin
password: admin
log:
path: /var/logs/db
level: debug
```
2017-12-13 08:33:21 +07:00
#### 5. Defines configuration name for Environment variable
Using **env** keyword to define configuration name
```golang
type Database struct {
Host string `env:"DB_HOST"`
Port int `env:"DB_PORT"`
Username string `env:"DB_USER" default:"admin"`
Password string `env:"DB_PASSWORD" default:"admin"`
Log Log `env:"DB_LOG_"`
}
```
2017-12-13 08:52:27 +07:00
Corresponding Environment variables:
```shell
export DB_HOST=test.db.hostname
export DB_PORT=8080
export DB_USER=admin
export DB_PASSWORD=admin
export DB_LOG_PATH=/var/logs/db
export DB_LOG_LEVEL=debug
```
2017-12-13 08:33:21 +07:00
#### 6. Defines configuration name for Command line
Using **cli** keyword to define configuration name
```golang
type Database struct {
Host string `cli:"host database host name"`
Port int `cli:"port database port"`
Username string `cli:"username database username" default:"admin"`
Password string `cli:"password database password" default:"admin"`
Log Log `cli:"log database log configurations"`
}
```
#### III. Reads configurations from JSON and Yaml files
##### 1. Using ```json``` to define configuration name
2017-12-12 23:27:48 +07:00
##### 2. Using ```yaml``` to define configuration name
```golang
type Database struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
Username string `yaml:"username" default:"admin"`
Password string `yaml:"password" default:"admin"`
Log Log `yaml:"log"`
}
```
2017-12-12 22:33:47 +07:00
Parsing configurations from JSON:
2017-12-12 22:18:55 +07:00
```golang
dbConfig := Database{}
2017-12-12 22:33:47 +07:00
config.ParseConfigFile(& dbConfig, "config.json")
2017-12-12 22:18:55 +07:00
```