Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improved profiles ++ added example ++ updated readme
- Loading branch information
Hasan Ozgan
committed
May 9, 2021
1 parent
99ded1c
commit 8e65b9b
Showing
14 changed files
with
164 additions
and
32 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
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,9 @@ | ||
database: | ||
host: "db" | ||
port: 5432 | ||
name: "users" | ||
username: "admin" | ||
password: "postgres" | ||
kafka: | ||
host: | ||
- "kafka" |
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,9 @@ | ||
database: | ||
host: "sqlite:file.db" | ||
port: -1 | ||
name: "users" | ||
username: "" | ||
password: "" | ||
kafka: | ||
host: | ||
- "embedded:kafka" |
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,10 @@ | ||
database: | ||
host: "db.prod.example.com" | ||
port: 5432 | ||
name: "${DATABASE_NAME:users}" | ||
username: "admin" | ||
password: "S3cr3t-P455w0rd" | ||
kafka: | ||
host: | ||
- "kafka1.prod.example.com" | ||
- "kafka2.prod.example.com" |
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,63 @@ | ||
package profile | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/kkyr/fig" | ||
) | ||
|
||
type Config struct { | ||
Database struct { | ||
Host string `fig:"host" validate:"required"` | ||
Port int `fig:"port"` | ||
Name string `fig:"name" validate:"required"` | ||
Username string `fig:"username"` | ||
Password string `fig:"password"` | ||
} | ||
Kafka struct { | ||
Host []string `fig:"host" validate:"required"` | ||
} | ||
} | ||
|
||
func ExampleLoad() { | ||
var cfg Config | ||
if err := fig.Load(&cfg); err == nil { | ||
fmt.Printf("%+v", cfg) | ||
} | ||
|
||
// Output: | ||
// {Database:{Host:db.prod.example.com Port:5432 Name:users Username:admin Password:S3cr3t-P455w0rd} Kafka:{Host:[kafka1.prod.example.com kafka2.prod.example.com]}} | ||
} | ||
|
||
func ExampleLoad_with_environment_in_config_file() { | ||
os.Setenv("DATABASE_NAME", "users-readonly") | ||
|
||
var cfg Config | ||
if err := fig.Load(&cfg); err == nil { | ||
fmt.Printf("%+v", cfg) | ||
} | ||
|
||
// Output: | ||
// {Database:{Host:db.prod.example.com Port:5432 Name:users-readonly Username:admin Password:S3cr3t-P455w0rd} Kafka:{Host:[kafka1.prod.example.com kafka2.prod.example.com]}} | ||
} | ||
|
||
func ExampleLoad_with_multi_profile() { | ||
var cfg Config | ||
if err := fig.Load(&cfg, fig.Profiles("test", "integration"), fig.ProfileLayout("config-test.yaml")); err == nil { | ||
fmt.Printf("%+v", cfg) | ||
} | ||
|
||
// Output: | ||
// {Database:{Host:db Port:5432 Name:users Username:admin Password:postgres} Kafka:{Host:[kafka]}} | ||
} | ||
|
||
func ExampleLoad_with_single_profile() { | ||
var cfg Config | ||
if err := fig.Load(&cfg, fig.Profiles("test"), fig.ProfileLayout("config-test.yaml")); err == nil { | ||
fmt.Printf("%+v", cfg) | ||
} | ||
|
||
// Output: | ||
// {Database:{Host:sqlite:file.db Port:-1 Name:users Username: Password:} Kafka:{Host:[embedded:kafka]}} | ||
} |
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,6 +1,8 @@ | ||
{ | ||
"host": "${SERVICE_HOST:0.0.0.0}", | ||
"logger": { | ||
"log_level": "debug" | ||
} | ||
"log_level": "debug", | ||
"appender": "file" | ||
}, | ||
"replicas": [ "abc","xyz" ] | ||
} |
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,7 @@ | ||
{ | ||
"host": "192.168.0.256" | ||
"host": "192.168.0.256", | ||
"logger": { | ||
"log_level": "error" | ||
}, | ||
"replicas": [ "xyz" ] | ||
} |
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,8 @@ | ||
host = "192.168.0.256" | ||
host = "192.168.0.256" | ||
replicas = [ | ||
"xyz" | ||
] | ||
|
||
[logger] | ||
log_level = "error" | ||
|
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,5 @@ | ||
host: "192.168.0.256" | ||
host: "192.168.0.256" | ||
logger: | ||
log_level: "error" | ||
replicas: | ||
- "xyz" |
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,4 +1,8 @@ | ||
host = "${SERVICE_HOST:0.0.0.0}" | ||
replicas = [ | ||
"abc", "xyz" | ||
] | ||
|
||
[logger] | ||
log_level = "debug" | ||
log_level = "debug" | ||
appender = "file" |
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,4 +1,9 @@ | ||
host: "${SERVICE_HOST:0.0.0.0}" | ||
|
||
logger: | ||
log_level: "debug" | ||
log_level: "debug" | ||
appender: "file" | ||
|
||
replicas: | ||
- abc | ||
- xyz |