Skip to content

Commit

Permalink
Add Debug/Verbose Mode
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Oct 24, 2017
1 parent 6c40903 commit 6ecfe62
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ contacts:
email: [email protected]
```
## Debug Mode & Verbose Mode
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.

```go
// Enable debug mode or set env `CONFIGOR_DEBUG_MODE` to true when running your application
configor.New(&configor.Config{Debug: true}).Load(&Config, "config.json")

// Enable verbose mode or set env `CONFIGOR_VERBOSE_MODE` to true when running your application
configor.New(&configor.Config{Verbose: true}).Load(&Config, "config.json")
```
# Advanced Usage
* Load mutiple configurations
Expand Down
29 changes: 25 additions & 4 deletions configor.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package configor

import (
"fmt"
"os"
"regexp"
)
Expand All @@ -12,13 +13,24 @@ type Configor struct {
type Config struct {
Environment string
ENVPrefix string
Debug bool
Verbose bool
}

// New initialize a Configor
func New(config *Config) *Configor {
if config == nil {
config = &Config{}
}

if os.Getenv("CONFIGOR_DEBUG_MODE") != "" {
config.Debug = true
}

if os.Getenv("CONFIGOR_VERBOSE_MODE") != "" {
config.Verbose = true
}

return &Configor{Config: config}
}

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

// Load will unmarshal configurations to struct from files that you provide
func (configor *Configor) Load(config interface{}, files ...string) error {
defer func() {
if configor.Config.Debug || configor.Config.Verbose {
fmt.Printf("Configuration:\n %#v\n", config)
}
}()

for _, file := range configor.getConfigurationFiles(files...) {
if configor.Config.Debug || configor.Config.Verbose {
fmt.Printf("Loading configurations from file '%v'...\n", file)
}
if err := processFile(config, file); err != nil {
return err
}
}

if prefix := configor.getENVPrefix(config); prefix == "-" {
return processTags(config)
} else {
return processTags(config, prefix)
prefix := configor.getENVPrefix(config)
if prefix == "-" {
return configor.processTags(config)
}
return configor.processTags(config, prefix)
}

// ENV return environment
Expand Down
17 changes: 14 additions & 3 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ func getConfigurationFileWithENVPrefix(file, env string) (string, error) {
func (configor *Configor) getConfigurationFiles(files ...string) []string {
var results []string

if configor.Config.Debug || configor.Config.Verbose {
fmt.Printf("Current environment: '%v'\n", configor.GetEnvironment())
}

for i := len(files) - 1; i >= 0; i-- {
foundFile := false
file := files[i]
Expand Down Expand Up @@ -106,7 +110,7 @@ func getPrefixForStruct(prefixes []string, fieldStruct *reflect.StructField) []s
return append(prefixes, fieldStruct.Name)
}

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

if configor.Config.Verbose {
fmt.Printf("Trying to load struct `%v`'s field `%v` from env %v\n", configType.Name(), fieldStruct.Name, strings.Join(envNames, ", "))
}

// Load From Shell ENV
for _, env := range envNames {
if value := os.Getenv(env); value != "" {
if configor.Config.Debug || configor.Config.Verbose {
fmt.Printf("Loading configuration for struct `%v`'s field `%v` from env %v...\n", configType.Name(), fieldStruct.Name, env)
}
if err := yaml.Unmarshal([]byte(value), field.Addr().Interface()); err != nil {
return err
}
Expand All @@ -159,15 +170,15 @@ func processTags(config interface{}, prefixes ...string) error {
}

if field.Kind() == reflect.Struct {
if err := processTags(field.Addr().Interface(), getPrefixForStruct(prefixes, &fieldStruct)...); err != nil {
if err := configor.processTags(field.Addr().Interface(), getPrefixForStruct(prefixes, &fieldStruct)...); err != nil {
return err
}
}

if field.Kind() == reflect.Slice {
for i := 0; i < field.Len(); i++ {
if reflect.Indirect(field.Index(i)).Kind() == reflect.Struct {
if err := processTags(field.Index(i).Addr().Interface(), append(getPrefixForStruct(prefixes, &fieldStruct), fmt.Sprint(i))...); err != nil {
if err := configor.processTags(field.Index(i).Addr().Interface(), append(getPrefixForStruct(prefixes, &fieldStruct), fmt.Sprint(i))...); err != nil {
return err
}
}
Expand Down

0 comments on commit 6ecfe62

Please sign in to comment.