@@ -55,11 +55,17 @@ type Config struct {
5555
5656 Redis redis.Config
5757 Database database.Config
58+
59+ // the flags plugin allows capturing a single Command after the flags.
60+ // so you can run myprogram -flag=value -s -blah=bleh stop|start|stop and so on.
61+ Mode string ` default:"start" flag:",command" usage:"run|start|stop"`
5862}
5963
6064var files = uconfig.Files {
61- {" config.json" , json.Unmarshal , true },
62- // you can of course add as many files
65+ {Path: " /etc/demo-app/config.json" , Unmarshal: json.Unmarshal , Optional: true },
66+ {Path: " config.json" , Unmarshal: json.Unmarshal , Optional: true },
67+ // or short form {"config.json", json.Unmarshal, true},
68+ // And, of course, you can of course add as many files
6369 // as you want, and they will be applied
6470 // in the given order.
6571}
@@ -84,22 +90,31 @@ Now lets run our program:
8490
8591``` sh
8692$ go run main.go -h
93+ Usage:
94+ main [flags] [command]
95+
96+ Configurations:
97+ FIELD FLAG ENV DEFAULT USAGE
98+ ----- ----- ----- ------- -----
99+ Hosts -hosts HOSTS localhost,localhost.local the ip or domains to bind to
100+ Redis.Address -redis-address REDIS_ADDRESS redis-master
101+ Redis.Port -redis-port REDIS_PORT 6379
102+ Redis.Password -redis-password REDIS_PASSWORD
103+ Redis.DB -redis-db REDIS_DB 0
104+ Redis.Expire -redis-expire REDIS_EXPIRE 5s
105+ Database.Address -database-address DATABASE_ADDRESS localhost
106+ Database.Port -database-port SERVICE_PORT 28015
107+ Database.Database -database-database DB my-project
108+ Mode [command] MODE start run| start| stop
109+
110+ Configuration Files:
111+ /etc/demo-app/config.json
112+ config.json
87113
88- Supported Fields:
89- FIELD FLAG ENV DEFAULT USAGE
90- ----- ----- ----- ------- -----
91- Hosts -hosts HOSTS localhost,localhost.local the ip or domains to bind to
92- Redis.Address -redis-address REDIS_ADDRESS redis-master
93- Redis.Port -redis-port REDIS_PORT 6379
94- Redis.Password -redis-password REDIS_PASSWORD
95- Redis.DB -redis-db REDIS_DB 0
96- Redis.Expire -redis-expire REDIS_EXPIRE 5s
97- Database.Address -database-address DATABASE_ADDRESS localhost
98- Database.Port -database-port DATABASE_PORT 28015
99- Database.Database -database-database DATABASE_DATABASE my-project
100-
114+ ```
101115$ go run main.go
102116
117+ ``` json
103118{
104119 "Hosts" : [
105120 " localhost" ,
@@ -116,7 +131,8 @@ $ go run main.go
116131 "Address" : " localhost" ,
117132 "Port" : " 28015" ,
118133 "Database" : " my-project"
119- }
134+ },
135+ "Mode" : " start"
120136}
121137
122138```
@@ -171,19 +187,23 @@ type Config struct {
171187
172188Which should give you the following settings:
173189
174- ```
175- Supported Fields:
176- FIELD FLAG ENV DEFAULT USAGE
177- ----- ----- ----- ------- -----
178- Hosts -hosts HOSTS localhost,localhost.local the ip or domains to bind to
179- Redis.Port -redis-port REDIS_PORT 6379
180- Redis.Password -redis-password REDIS_PASSWORD
181- Redis.DB -redis-db REDIS_DB 0
182- Redis.Expire -redis-expire REDIS_EXPIRE 5s
183- Database.Address -database-address DATABASE_ADDRESS localhost
184- Database.Service.Port -database-service-port DATABASE_SERVICE_PORT 28015
185- Database.Database -main-db-db DB_NAME my-project
186- exit status 1
190+ ``` sh
191+ $ go run main.go -h
192+ Usage:
193+ main [flags] [command]
194+
195+ Configurations:
196+ FIELD FLAG ENV DEFAULT USAGE
197+ ----- ----- ----- ------- -----
198+ Hosts -hosts HOSTS localhost,localhost.local the ip or domains to bind to
199+ Redis.Address -redis-address REDIS_ADDRESS redis-master
200+ Redis.Port -redis-port REDIS_PORT 6379
201+ Redis.Password -redis-password REDIS_PASSWORD
202+ Redis.DB -redis-db REDIS_DB 0
203+ Redis.Expire -redis-expire REDIS_EXPIRE 5s
204+ Database.Address -database-address DATABASE_ADDRESS localhost
205+ Database.Database -main-db-db DB_NAME my-project
206+ Database.Service.Port -database-service-port DATABASE_SERVICE_PORT 28015
187207```
188208
189209
@@ -209,52 +229,49 @@ Unlike most other plugins, secret requires explicit `secret:""` tag, this is bec
209229package main
210230
211231import (
212- " encoding/json"
213- " fmt"
232+ " encoding/json"
233+ " fmt"
214234
215- " github.com/omeid/uconfig"
216- " github.com/omeid/uconfig/examples/secrets/secretsource"
217- " github.com/omeid/uconfig/plugins/secret"
235+ " github.com/omeid/uconfig"
236+ " github.com/omeid/uconfig/plugins/secret"
237+
238+ " github.com/omeid/uconfig/examples/secrets/secretsource"
218239)
219240
220241// Creds is an example of a config struct that uses secret values.
221242type Creds struct {
222- // by default, secret plugin will generate a name that is identical
223- // to env plugin, SCREAM_SNAKE_CASE, so in this case it will be
224- // APIKEY however, following the standard uConfig nesting rules
225- // in Config struct below, it becomes CREDS_APIKEY.
226- APIKey string ` secret:""`
227- // or you can provide your own name, which will not be impacted
228- // by nesting or the field name.
229- APIToken string ` secret:"API_TOKEN"`
243+ // by default, secret plugin will generate a name that is identical
244+ // to env plugin, SCREAM_SNAKE_CASE, so in this case it will be
245+ // APIKEY however, following the standard uConfig nesting rules
246+ // in Config struct below, it becomes CREDS_APIKEY.
247+ APIKey string ` secret:""`
248+ // or you can provide your own name, which will not be impacted
249+ // by nesting or the field name.
250+ APIToken string ` secret:"API_TOKEN"`
230251}
231252
232253type Config struct {
233- Creds Creds
234- }
235-
236- var files = uconfig.Files {
237- {" config.json" , json.Unmarshal , false },
254+ Creds Creds
238255}
239256
240257var secrets = secret.New (func (name string ) (string , error ) {
241- // you're free to grab the secret based on the name from wherever
242- // you please, aws secrets-manager, hashicorp vault, or wherever.
243- value , ok := secretsource.Get (name)
244- if !ok {
245- return " " , secret.ErrSecretNotFound
246- }
247-
248- return value, nil
258+ // you're free to grab the secret based on the name from wherever
259+ // you please, aws secrets-manager, hashicorp vault, or wherever.
260+ value , ok := secretsource.Get (name)
261+ if !ok {
262+ return " " , secret.ErrSecretNotFound
263+ }
264+
265+ return value, nil
249266})
250267
251268func main () {
252- // then you can use the secretPlugin with uConfig like any other plugin.
253- // Lucky, uconfig.Classic allows passing more plugins, which means
254- // you can simply do the following for flags, envs, files, and secrets!
255- conf := uconfig.Classic [Config](files , secrets).Run ()
269+ // then you can use the secretPlugin with uConfig like any other plugin.
270+ // Lucky, uconfig.Classic allows passing more plugins, which means
271+ // you can simply do the following for flags, envs, files, and secrets!
272+ conf := uconfig.Classic [Config](nil , secrets).Run ()
256273
257- fmt.Printf (" we got an API Key: %s \n " , conf.Creds .APIKey )
274+ fmt.Printf (" we got an API Key: %s \n " , conf.Creds .APIKey )
258275}
259276```
260277
0 commit comments