1
1
// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm)
2
- // go-aah /config source code and usage is governed by a MIT style
2
+ // aahframework.org /config source code and usage is governed by a MIT style
3
3
// license that can be found in the LICENSE file.
4
4
5
5
// Package config is nice and handy layer built around `forge` config syntax;
@@ -14,18 +14,26 @@ import (
14
14
"fmt"
15
15
"strings"
16
16
17
- "aahframework.org/essentials.v0"
18
17
"aahframework.org/forge.v0"
18
+ "aahframework.org/vfs.v0"
19
19
)
20
20
21
21
var errKeyNotFound = errors .New ("config: not found" )
22
22
23
- // NewEmptyConfig method returns aah empty config instance.
24
- func NewEmptyConfig () * Config {
23
+ //‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
24
+ // Package methods
25
+ //______________________________________________________________________________
26
+
27
+ // NewEmpty method returns aah empty config instance.
28
+ func NewEmpty () * Config {
25
29
cfg , _ := ParseString ("" )
26
30
return cfg
27
31
}
28
32
33
+ //‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
34
+ // Config type and methods
35
+ //______________________________________________________________________________
36
+
29
37
// Config handles the configuration values and enables environment profile's,
30
38
// merge, etc. Also it provide nice and handly methods for accessing config values.
31
39
// Internally `aah config` uses `forge syntax` developed by `https://github.com/brettlangdon`.
@@ -63,6 +71,9 @@ func (c *Config) HasProfile(profile string) bool {
63
71
64
72
// IsProfileEnabled returns true of profile enabled otherwise false
65
73
func (c * Config ) IsProfileEnabled () bool {
74
+ if c == nil {
75
+ return false
76
+ }
66
77
return len (c .profile ) > 0
67
78
}
68
79
@@ -206,9 +217,9 @@ func (c *Config) Get(key string) (interface{}, bool) {
206
217
return c .get (key )
207
218
}
208
219
209
- //‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
220
+ //‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
210
221
// List methods
211
- //___________________________________
222
+ //______________________________________________________________________________
212
223
213
224
// StringList method returns the string slice value for the given key.
214
225
// Eaxmple:-
@@ -303,9 +314,9 @@ func (c *Config) Int64List(key string) ([]int64, bool) {
303
314
return values , true
304
315
}
305
316
306
- //‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
307
- // Setter methods
308
- //___________________________________
317
+ //‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
318
+ // Config Setter methods
319
+ //______________________________________________________________________________
309
320
310
321
// SetString sets the given value string for config key
311
322
// First it tries to get value within enabled profile
@@ -380,36 +391,33 @@ func (c *Config) ToJSON() string {
380
391
return "{}"
381
392
}
382
393
383
- //‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
384
- // Configuration loading methods
385
- //___________________________________
394
+ //‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
395
+ // Config load/parse methods
396
+ //______________________________________________________________________________
386
397
387
- // LoadFile loads the configuration given config file
398
+ // LoadFile loads the configuration from given config file.
388
399
func LoadFile (file string ) (* Config , error ) {
389
- if ! ess .IsFileExists (file ) {
390
- return nil , fmt .Errorf ("configuration does not exists: %v" , file )
391
- }
392
-
393
- setting , err := forge .ParseFile (file )
394
- if err != nil {
395
- return nil , err
396
- }
400
+ return VFSLoadFile (nil , file )
401
+ }
397
402
398
- return & Config {
399
- cfg : setting ,
400
- }, nil
403
+ // VFSLoadFile loads the configuration from given vfs and config file.
404
+ func VFSLoadFile (fs * vfs.VFS , file string ) (* Config , error ) {
405
+ setting , err := loadFile (fs , file )
406
+ return & Config {cfg : setting }, err
401
407
}
402
408
403
- // LoadFiles loads the configuration given config files and
404
- // does merging of configuration in the order they are given
409
+ // LoadFiles loads the configuration from given config files.
410
+ // It does merging of configuration in the order they are given.
405
411
func LoadFiles (files ... string ) (* Config , error ) {
412
+ return VFSLoadFiles (nil , files ... )
413
+ }
414
+
415
+ // VFSLoadFiles loads the configuration from given config vfs and files.
416
+ // It does merging of configuration in the order they are given.
417
+ func VFSLoadFiles (fs * vfs.VFS , files ... string ) (* Config , error ) {
406
418
settings := forge .NewSection ()
407
419
for _ , file := range files {
408
- if ! ess .IsFileExists (file ) {
409
- return nil , fmt .Errorf ("configuration does not exists: %v" , file )
410
- }
411
-
412
- setting , err := forge .ParseFile (file )
420
+ setting , err := loadFile (fs , file )
413
421
if err != nil {
414
422
return nil , err
415
423
}
@@ -419,9 +427,7 @@ func LoadFiles(files ...string) (*Config, error) {
419
427
}
420
428
}
421
429
422
- return & Config {
423
- cfg : settings ,
424
- }, nil
430
+ return & Config {cfg : settings }, nil
425
431
}
426
432
427
433
// ParseString parses the configuration values from string
@@ -430,15 +436,19 @@ func ParseString(cfg string) (*Config, error) {
430
436
if err != nil {
431
437
return nil , err
432
438
}
433
-
434
- return & Config {
435
- cfg : setting ,
436
- }, nil
439
+ return & Config {cfg : setting }, nil
437
440
}
438
441
439
- //‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
440
- // Unexported methods
441
- //___________________________________
442
+ //‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
443
+ // Config unexported methods
444
+ //______________________________________________________________________________
445
+
446
+ func loadFile (fs * vfs.VFS , file string ) (* forge.Section , error ) {
447
+ if _ , err := vfs .Stat (fs , file ); err != nil {
448
+ return nil , fmt .Errorf ("configuration does not exists: %v" , file )
449
+ }
450
+ return forge .VFSParseFile (fs , file )
451
+ }
442
452
443
453
func (c * Config ) prepareKey (key string ) string {
444
454
if c .IsProfileEnabled () {
@@ -475,6 +485,10 @@ func (c *Config) getListValue(key string) (*forge.List, bool) {
475
485
}
476
486
477
487
func (c * Config ) getraw (key string ) (forge.Value , bool ) {
488
+ if c == nil || c .cfg == nil {
489
+ return nil , false
490
+ }
491
+
478
492
v , err := c .cfg .Resolve (key )
479
493
if err != nil {
480
494
return nil , false // not found
0 commit comments