-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
210 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,5 @@ env: | |
- GO111MODULE=on | ||
|
||
script: | ||
- go test -v | ||
- make test | ||
- make bench |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
test: ## Run unit tests | ||
go test -count=1 -short ./... | ||
test: | ||
go test -race -v ./... | ||
|
||
cover: dep | ||
bench: | ||
go test -run="^$$" -bench=. | ||
|
||
cover: | ||
go test $(shell go list ./... | grep -v /vendor/;) -cover -v | ||
|
||
dep: ## Get the dependencies | ||
GO111MODULE=on go mod vendor | ||
dep: | ||
GO111MODULE=on go mod vendor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,77 @@ | ||
# go-config [](https://travis-ci.org/Yalantis/go-config) | ||
|
||
go-config allows to initialize configuration in flexible way using from default, file, environment variables value. | ||
### Initialization | ||
Done in three steps: | ||
1. init with value from `default` tag | ||
2. merge with config file if `filepath` is provided | ||
3. override with environment variables which stored under `envconfig` tag | ||
|
||
### Supported file extensions | ||
- json | ||
|
||
### Supported types | ||
- Standard types: `bool`, `float`, `int`(`uint`), `slice`, `string` | ||
- `time.Duration`, `time.Time`: full support with aliases `config.Duration`, `config.Time` | ||
- Custom types, slice of custom types | ||
|
||
### Usage | ||
|
||
#### Default value | ||
```go | ||
type Server struct { | ||
Addr string `default:"localhost:8080"` | ||
} | ||
``` | ||
#### Environment value | ||
```go | ||
type Server struct { | ||
Addr string `envconfig:"SERVER_ADDR"` | ||
} | ||
``` | ||
#### Combined default, json, env | ||
```go | ||
type Server struct { | ||
Addr string `json:"addr" envconfig:"SERVER_ADDR" default:"localhost:8080"` | ||
} | ||
``` | ||
#### Slice | ||
Default strings separator is comma. | ||
``` | ||
REDIS_ADDR=127.0.0.1:6377,127.0.0.1:6378,127.0.0.1:6379 | ||
``` | ||
```go | ||
type Redis struct { | ||
Addrs []string `json:"addrs" envconfig:"REDIS_ADDR" default:"localhost:6378,localhost:6379"` | ||
} | ||
``` | ||
Slice of structs could be parsed from environment by defining `envprefix`. | ||
Every ENV group override element stored at `index` of slice or append new one. | ||
Sparse slices are not allowed. | ||
```go | ||
var cfg struct { | ||
... | ||
Replicas []Postgres `json:"replicas" envprefix:"REPLICAS"` | ||
... | ||
} | ||
``` | ||
Environment key should has next pattern: | ||
`${envprefix}_${index}_${envconfig}` or `${envprefix}_${index}_${StructFieldName}` | ||
``` | ||
REPLICAS_0_POSTGRES_USER=replica REPLICAS_2_USER=replica | ||
``` | ||
#### `time.Duration`, `time.Time` | ||
In case using json file you have to use aliases `config.Duration`, `config.Time`, that properly unmarshal it self | ||
```go | ||
type NATS struct { | ||
... | ||
ReconnectInterval config.Duration `json:"reconnect_interval" envconfig:"NATS_RECONNECT_INTERVAL" default:"2s"` | ||
} | ||
``` | ||
Otherwise `time.Duration`, `time.Time` might be used directly: | ||
```go | ||
var cfg struct { | ||
ReadTimeout time.Duration `envconfig:"READ_TIMEOUT" default:"1s"` | ||
WriteTimeout time.Duration `envconfig:"WRITE_TIMEOUT" default:"10s"` | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
package config | ||
|
||
import "syscall" | ||
import "os" | ||
|
||
var lookupEnv = syscall.Getenv | ||
var environ = syscall.Environ | ||
var Setenv = syscall.Setenv | ||
var Unsetenv = syscall.Unsetenv | ||
var lookupEnv = os.LookupEnv | ||
var environ = os.Environ | ||
var Setenv = os.Setenv | ||
var Unsetenv = os.Unsetenv | ||
|
||
//var ExpandEnv = os.ExpandEnv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package config_test | ||
|
||
import ( | ||
"fmt" | ||
"github.com/Yalantis/go-config" | ||
"log" | ||
"time" | ||
) | ||
|
||
func init() { | ||
// setup ENV | ||
_ = config.Setenv("VERSION", "0.0.1") | ||
_ = config.Setenv("REPLICAS_0_POSTGRES_USER", "replica0") | ||
_ = config.Setenv("REPLICAS_0_POSTGRES_PORT", "5433") | ||
_ = config.Setenv("REPLICAS_1_USER", "replica1") | ||
_ = config.Setenv("REPLICAS_1_PORT", "5433") | ||
_ = config.Setenv("REDIS_ADDR", "127.0.0.1:6377,127.0.0.1:6378,127.0.0.1:6379") | ||
_ = config.Setenv("READ_TIMEOUT", "30s") | ||
} | ||
|
||
func ExampleInit() { | ||
type Server struct { | ||
Addr string `json:"addr" envconfig:"SERVER_ADDR" default:"localhost:8080"` | ||
} | ||
|
||
type Postgres struct { | ||
Host string `json:"host" envconfig:"POSTGRES_HOST" default:"localhost"` | ||
Port string `json:"port" envconfig:"POSTGRES_PORT" default:"5432"` | ||
User string `json:"user" envconfig:"POSTGRES_USER" default:"postgres"` | ||
Password string `json:"password" envconfig:"POSTGRES_PASSWORD" default:"12345"` | ||
} | ||
|
||
type Redis struct { | ||
Addrs []string `json:"addrs" envconfig:"REDIS_ADDR" default:"localhost:6379"` | ||
} | ||
|
||
type NATS struct { | ||
ServerURL string `json:"server_url" envconfig:"NATS_SERVER_URL" default:"nats://localhost:4222"` | ||
MaxReconnectionAttempts int `json:"max_reconnection_attempts" envconfig:"NATS_MAX_RECONNECT_ATTEMPTS" default:"5"` | ||
ReconnectInterval config.Duration `json:"reconnect_interval" envconfig:"NATS_RECONNECT_INTERVAL" default:"2s"` | ||
} | ||
|
||
type Websocket struct { | ||
Port string `json:"port" envconfig:"WEBSOCKET_PORT" default:"9876"` | ||
} | ||
|
||
var cfg struct { | ||
Version string `envconfig:"VERSION" default:"0"` | ||
Server Server `json:"server"` | ||
Postgres Postgres `json:"postgres"` | ||
Replicas []Postgres `json:"replicas" envprefix:"REPLICAS"` | ||
Redis Redis `json:"redis"` | ||
NATS NATS `json:"nats"` | ||
Websocket Websocket `json:"websocket"` | ||
} | ||
|
||
if err := config.Init(&cfg, "testdata/config.json"); err != nil { | ||
log.Fatalln(err) | ||
} | ||
|
||
fmt.Println(cfg) | ||
// Output: {0.0.1 {localhost:8080} {localhost 5432 postgres 12345} [{localhost 5433 replica0 12345} {localhost 5433 replica1 12345}] {[127.0.0.1:6377 127.0.0.1:6378 127.0.0.1:6379]} {nats://localhost:4222 5 2000000000} {9876}} | ||
} | ||
|
||
func ExampleInitTimeout() { | ||
var cfg struct { | ||
ReadTimeout time.Duration `envconfig:"READ_TIMEOUT" default:"1s"` | ||
WriteTimeout time.Duration `envconfig:"WRITE_TIMEOUT" default:"10s"` | ||
} | ||
|
||
if err := config.Init(&cfg, ""); err != nil { | ||
log.Fatalln(err) | ||
} | ||
|
||
fmt.Println(cfg) | ||
// Output: {30s 10s} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
module gitlab.yalantis.com/gophers/config | ||
module github.com/Yalantis/go-config | ||
|
||
require github.com/stretchr/testify v1.3.0 | ||
go 1.13 | ||
|
||
go 1.11+ | ||
require github.com/stretchr/testify v1.4.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters