Skip to content

Commit 6ecfe62

Browse files
committed
Add Debug/Verbose Mode
1 parent 6c40903 commit 6ecfe62

File tree

3 files changed

+51
-7
lines changed

3 files changed

+51
-7
lines changed

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ contacts:
5050
5151
```
5252
53+
## Debug Mode & Verbose Mode
54+
55+
Debug/Verbose mode is helpful when debuging your application, `debug mode` will let you know how `configor` loaded your configurations, like from which file, shell env, `verbose mode` will tell you even more, like those shell environments `configor` tried to load.
56+
57+
```go
58+
// Enable debug mode or set env `CONFIGOR_DEBUG_MODE` to true when running your application
59+
configor.New(&configor.Config{Debug: true}).Load(&Config, "config.json")
60+
61+
// Enable verbose mode or set env `CONFIGOR_VERBOSE_MODE` to true when running your application
62+
configor.New(&configor.Config{Verbose: true}).Load(&Config, "config.json")
63+
```
64+
5365
# Advanced Usage
5466
5567
* Load mutiple configurations

configor.go

+25-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package configor
22

33
import (
4+
"fmt"
45
"os"
56
"regexp"
67
)
@@ -12,13 +13,24 @@ type Configor struct {
1213
type Config struct {
1314
Environment string
1415
ENVPrefix string
16+
Debug bool
17+
Verbose bool
1518
}
1619

1720
// New initialize a Configor
1821
func New(config *Config) *Configor {
1922
if config == nil {
2023
config = &Config{}
2124
}
25+
26+
if os.Getenv("CONFIGOR_DEBUG_MODE") != "" {
27+
config.Debug = true
28+
}
29+
30+
if os.Getenv("CONFIGOR_VERBOSE_MODE") != "" {
31+
config.Verbose = true
32+
}
33+
2234
return &Configor{Config: config}
2335
}
2436

@@ -40,17 +52,26 @@ func (configor *Configor) GetEnvironment() string {
4052

4153
// Load will unmarshal configurations to struct from files that you provide
4254
func (configor *Configor) Load(config interface{}, files ...string) error {
55+
defer func() {
56+
if configor.Config.Debug || configor.Config.Verbose {
57+
fmt.Printf("Configuration:\n %#v\n", config)
58+
}
59+
}()
60+
4361
for _, file := range configor.getConfigurationFiles(files...) {
62+
if configor.Config.Debug || configor.Config.Verbose {
63+
fmt.Printf("Loading configurations from file '%v'...\n", file)
64+
}
4465
if err := processFile(config, file); err != nil {
4566
return err
4667
}
4768
}
4869

49-
if prefix := configor.getENVPrefix(config); prefix == "-" {
50-
return processTags(config)
51-
} else {
52-
return processTags(config, prefix)
70+
prefix := configor.getENVPrefix(config)
71+
if prefix == "-" {
72+
return configor.processTags(config)
5373
}
74+
return configor.processTags(config, prefix)
5475
}
5576

5677
// ENV return environment

utils.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ func getConfigurationFileWithENVPrefix(file, env string) (string, error) {
4545
func (configor *Configor) getConfigurationFiles(files ...string) []string {
4646
var results []string
4747

48+
if configor.Config.Debug || configor.Config.Verbose {
49+
fmt.Printf("Current environment: '%v'\n", configor.GetEnvironment())
50+
}
51+
4852
for i := len(files) - 1; i >= 0; i-- {
4953
foundFile := false
5054
file := files[i]
@@ -106,7 +110,7 @@ func getPrefixForStruct(prefixes []string, fieldStruct *reflect.StructField) []s
106110
return append(prefixes, fieldStruct.Name)
107111
}
108112

109-
func processTags(config interface{}, prefixes ...string) error {
113+
func (configor *Configor) processTags(config interface{}, prefixes ...string) error {
110114
configValue := reflect.Indirect(reflect.ValueOf(config))
111115
if configValue.Kind() != reflect.Struct {
112116
return errors.New("invalid config, should be struct")
@@ -132,9 +136,16 @@ func processTags(config interface{}, prefixes ...string) error {
132136
envNames = []string{envName}
133137
}
134138

139+
if configor.Config.Verbose {
140+
fmt.Printf("Trying to load struct `%v`'s field `%v` from env %v\n", configType.Name(), fieldStruct.Name, strings.Join(envNames, ", "))
141+
}
142+
135143
// Load From Shell ENV
136144
for _, env := range envNames {
137145
if value := os.Getenv(env); value != "" {
146+
if configor.Config.Debug || configor.Config.Verbose {
147+
fmt.Printf("Loading configuration for struct `%v`'s field `%v` from env %v...\n", configType.Name(), fieldStruct.Name, env)
148+
}
138149
if err := yaml.Unmarshal([]byte(value), field.Addr().Interface()); err != nil {
139150
return err
140151
}
@@ -159,15 +170,15 @@ func processTags(config interface{}, prefixes ...string) error {
159170
}
160171

161172
if field.Kind() == reflect.Struct {
162-
if err := processTags(field.Addr().Interface(), getPrefixForStruct(prefixes, &fieldStruct)...); err != nil {
173+
if err := configor.processTags(field.Addr().Interface(), getPrefixForStruct(prefixes, &fieldStruct)...); err != nil {
163174
return err
164175
}
165176
}
166177

167178
if field.Kind() == reflect.Slice {
168179
for i := 0; i < field.Len(); i++ {
169180
if reflect.Indirect(field.Index(i)).Kind() == reflect.Struct {
170-
if err := processTags(field.Index(i).Addr().Interface(), append(getPrefixForStruct(prefixes, &fieldStruct), fmt.Sprint(i))...); err != nil {
181+
if err := configor.processTags(field.Index(i).Addr().Interface(), append(getPrefixForStruct(prefixes, &fieldStruct), fmt.Sprint(i))...); err != nil {
171182
return err
172183
}
173184
}

0 commit comments

Comments
 (0)