-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
2766694
commit 77ae9f1
Showing
6 changed files
with
245 additions
and
4 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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
name: Go | ||
on: [push] | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
go-version: ["1.16.x", "1.17.x", "1.18.x", "1.19.x"] | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Setup Go | ||
uses: actions/setup-go@v3 | ||
with: | ||
go-version: ${{ matrix.go-version }} | ||
- name: Install dependencies | ||
run: go get . | ||
- name: Test with Go | ||
run: go test -json > TestResults-${{ matrix.go-version }}.json | ||
- name: Upload Go test results | ||
uses: actions/upload-artifact@v3 | ||
with: | ||
name: Go-results-${{ matrix.go-version }} | ||
path: TestResults-${{ matrix.go-version }}.json |
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,3 +1,131 @@ | ||
# Easyconfig [](https://pkg.go.dev/github.com/night-codes/easyconfig) [](https://travis-ci.org/night-codes/easyconfig) | ||
# Easyconfig [](https://pkg.go.dev/github.com/night-codes/easyconfig) | ||
|
||
Multi-source configuration loading library in Go. | ||
Easy-to-use multi source configuration library in Go. | ||
|
||
## Documentation | ||
|
||
[Docs on pkg.go.dev](https://pkg.go.dev/github.com/night-codes/easyconfig) | ||
|
||
## How To Install | ||
|
||
```bash | ||
go get github.com/night-codes/easyconfig | ||
``` | ||
|
||
## Example | ||
|
||
The usage looks like this: | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/night-codes/easyconfig" | ||
) | ||
|
||
type ( | ||
Config struct { | ||
PostgresUser string `json:"postgresUser" yaml:"postgresUser" dir:"postgres-user" env:"APP_POSTGRES_USER"` | ||
PostgresPassword string `json:"postgresPassword" yaml:"postgresPassword"` | ||
PostgresHost string `json:"postgresHost" yaml:"postgresHost"` | ||
PostgresPort uint64 `json:"postgresPort" yaml:"postgresPort"` | ||
PostgresDBName string `json:"postgresDBName" yaml:"postgresDBName"` | ||
PostgresSSLMode string `json:"postgresSSLMode" yaml:"postgresSSLMode"` | ||
Slice []string `json:"-" yaml:"-" env:"slice,:"` // env list separator is ":" | ||
Version string `json:"-" yaml:"-" dir:"-" env:"-"` // ignore field | ||
} | ||
) | ||
|
||
func main() { | ||
config := &Config{ | ||
PostgresHost: "localhost", // default value | ||
} | ||
|
||
// Simple configuration load | ||
jsonLoader := easyconfig.JSONSource{Path: "./config.json"} | ||
if err := jsonLoader.Load(config); err != nil { | ||
fmt.Println(err) | ||
// use another loader, for example | ||
// .... | ||
} | ||
|
||
// Multi sources loader | ||
loader := easyconfig.NewLoader( | ||
[]easyconfig.Source{ | ||
// all sources are sorted in the order in which data loading from each tip will be attempted | ||
easyconfig.YAMLSource{Path: "./config.yaml"}, | ||
easyconfig.JSONSource{Path: "./config.json"}, | ||
easyconfig.TOMLSource{Path: "./config.toml"}, | ||
easyconfig.EnvFileSource{Prefix: "APP", Path: "./config.env"}, | ||
easyconfig.DirSource{Path: "./k8s-secret"}, // loads configuration from the mounted Secrets or ConfigMap kubernetes directory (file=value) | ||
easyconfig.EnvSource{Prefix: "APP"}, // loads configuration from environment variables | ||
easyconfig.FlagsSource{}, // loads configuration from flags | ||
}, | ||
) | ||
|
||
loader.Load(config) // collect data from each source | ||
fmt.Printf("%v\n", config) | ||
} | ||
``` | ||
|
||
You can use -help flag in your application: | ||
|
||
```bash | ||
❯ go run . -help | ||
|
||
Usage: | ||
app [arguments] | ||
|
||
The commands are: | ||
|
||
-postgresUser | ||
Set value of PostgresUser | ||
-postgresPassword | ||
Set value of PostgresPassword | ||
-postgresHost | ||
Set value of PostgresHost. Default: "localhost" | ||
-postgresPort | ||
Set number value of PostgresPort | ||
-postgresDBName | ||
Set value of PostgresDBName | ||
-postgresSSLMode | ||
Set value of PostgresSSLMode | ||
-slice | ||
Set value of Slice | ||
-version | ||
Set value of Version | ||
|
||
Environment variables to use: | ||
|
||
APP_POSTGRES_USER | ||
APP_POSTGRES_PASSWORD | ||
APP_POSTGRES_HOST | ||
APP_POSTGRES_PORT | ||
APP_POSTGRES_DB_NAME | ||
APP_POSTGRES_SSL_MODE | ||
APP_SLICE | ||
|
||
Configuration directory files to use: | ||
|
||
postgres-user | ||
postgres-password | ||
postgres-host | ||
postgres-port | ||
postgres-db-name | ||
postgres-ssl-mode | ||
slice | ||
``` | ||
|
||
## License | ||
|
||
MIT License | ||
|
||
Copyright (C) 2022 Oleksiy Chechel (alex.mirrr@gmail.com) | ||
|
||
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. |
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,16 @@ | ||
module easyconfig/tests/app | ||
|
||
go 1.18 | ||
|
||
require github.com/night-codes/easyconfig v0.0.0-20220929084351-276669438fa1 | ||
|
||
require ( | ||
github.com/BurntSushi/toml v1.2.0 // indirect | ||
github.com/fatih/color v1.13.0 // indirect | ||
github.com/iancoleman/strcase v0.2.0 // indirect | ||
github.com/joho/godotenv v1.4.0 // indirect | ||
github.com/mattn/go-colorable v0.1.9 // indirect | ||
github.com/mattn/go-isatty v0.0.14 // indirect | ||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect | ||
gopkg.in/yaml.v2 v2.4.0 // indirect | ||
) |
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,23 @@ | ||
github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0= | ||
github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= | ||
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w= | ||
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= | ||
github.com/iancoleman/strcase v0.2.0 h1:05I4QRnGpI0m37iZQRuskXh+w77mr6Z41lwQzuHLwW0= | ||
github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho= | ||
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg= | ||
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= | ||
github.com/mattn/go-colorable v0.1.9 h1:sqDoxXbdeALODt0DAeJCVp38ps9ZogZEAXjus69YV3U= | ||
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= | ||
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= | ||
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= | ||
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= | ||
github.com/night-codes/easyconfig v0.0.0-20220929084351-276669438fa1 h1:VSjYS5up2Flk+1Az+oTUXIbLvqzwAzegJt+GAtkbSq4= | ||
github.com/night-codes/easyconfig v0.0.0-20220929084351-276669438fa1/go.mod h1:sDCRRueXpPoBrHFAoyXL6vYOH8+W+0r7M+RPueTM+NE= | ||
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I= | ||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= | ||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= |
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,51 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/night-codes/easyconfig" | ||
) | ||
|
||
type ( | ||
Config struct { | ||
PostgresUser string `json:"postgresUser" yaml:"postgresUser" dir:"postgres-user" env:"APP_POSTGRES_USER"` | ||
PostgresPassword string `json:"postgresPassword" yaml:"postgresPassword"` | ||
PostgresHost string `json:"postgresHost" yaml:"postgresHost"` | ||
PostgresPort uint64 `json:"postgresPort" yaml:"postgresPort"` | ||
PostgresDBName string `json:"postgresDBName" yaml:"postgresDBName"` | ||
PostgresSSLMode string `json:"postgresSSLMode" yaml:"postgresSSLMode"` | ||
Slice []string `json:"-" yaml:"-" toml:"-" env:"slice,:"` // env list separator is ":" | ||
Version string `json:"-" yaml:"-" dir:"-" toml:"-" env:"-"` // ignore field for all sources | ||
} | ||
) | ||
|
||
func main() { | ||
config := &Config{ | ||
PostgresHost: "localhost", // default value | ||
} | ||
|
||
// Simple configuration load | ||
jsonLoader := easyconfig.JSONSource{Path: "../../tests/config.json"} | ||
if err := jsonLoader.Load(config); err != nil { | ||
fmt.Println(err) | ||
// use another loader, for example | ||
// .... | ||
} | ||
|
||
// Multi sources loader | ||
loader := easyconfig.NewLoader( | ||
[]easyconfig.Source{ | ||
// all sources are sorted in the order in which data loading from each tip will be attempted | ||
easyconfig.YAMLSource{Path: "../../tests/config.yaml"}, | ||
easyconfig.JSONSource{Path: "../../tests/config.json"}, | ||
easyconfig.TOMLSource{Path: "../../tests/config.toml"}, | ||
easyconfig.EnvFileSource{Prefix: "APP", Path: "../../tests/config.env"}, | ||
easyconfig.DirSource{Path: "../../tests/k8s-secret"}, // loads configuration from the mounted Secrets or ConfigMap kubernetes directory (file=value) | ||
easyconfig.EnvSource{Prefix: "APP"}, // loads configuration from environment variables | ||
easyconfig.FlagsSource{}, // loads configuration from flags | ||
}, | ||
) | ||
|
||
loader.Load(config) // collect data from each source | ||
fmt.Printf("%v\n", config) | ||
} |
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