Skip to content

Commit

Permalink
Interface cleanup for logger and configuration init
Browse files Browse the repository at this point in the history
  • Loading branch information
markdicksonjr committed Jul 5, 2019
1 parent d25093b commit 9391598
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ err = app.Init(config, logger, extensions)
The app configuration can be created with this method:

```go
config, err := nibbler.LoadConfiguration(nil)
config, err := nibbler.LoadConfiguration()
```

If nil is provided to the core.LoadConfiguration method, it will use environment variables for
Expand Down
8 changes: 4 additions & 4 deletions application.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ type Extension interface {
}

type Logger interface {
Debug(message string)
Error(message string)
Info(message string)
Warn(message string)
Debug(message ...string)
Error(message ...string)
Info(message ...string)
Warn(message ...string)
}

type Configuration struct {
Expand Down
11 changes: 5 additions & 6 deletions configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ func GetConfigurationFromSources(sources []source.Source) (config.Config, error)
}

// "merging priority is in reverse order"
// if nil, environment source used
func LoadConfiguration(sources *[]source.Source) (*Configuration, error) {
// if nil or empty, file and environment sources used (file takes precendence)
func LoadConfiguration(sources ...source.Source) (*Configuration, error) {

// if sources are not provided
if sources == nil {
if len(sources) == 0 {
var envSources []source.Source

configFileExists := true
Expand All @@ -43,12 +43,11 @@ func LoadConfiguration(sources *[]source.Source) (*Configuration, error) {

// use environment variable source
envSources = append(envSources, env.NewSource())

sources = &envSources
sources = envSources
}

// load the app configuration
conf, err := GetConfigurationFromSources(*sources)
conf, err := GetConfigurationFromSources(sources)

// if an error occurred, return it
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ require (
github.com/gorilla/sessions v1.1.2
github.com/micro/go-micro v1.7.0
golang.org/x/crypto v0.0.0-20190618222545-ea8f1a30c443
golang.org/x/tools v0.0.0-20190620191750-1fa568393b23
)
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBn
golang.org/x/tools v0.0.0-20190506145303-2d16b83fe98c/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/tools v0.0.0-20190530171427-2b03ca6e44eb/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/tools v0.0.0-20190606124116-d0a3d012864b/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/tools v0.0.0-20190620191750-1fa568393b23 h1:9u6LpPnUW5I7Ou7w6TVLJo3ut+4NgW98U5yEgg6OAgo=
golang.org/x/tools v0.0.0-20190620191750-1fa568393b23/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
google.golang.org/api v0.6.0/go.mod h1:btoxGiFvQNVUZQ8W08zLtrVS08CNpINPEfxXxgJL1Q4=
Expand Down
29 changes: 16 additions & 13 deletions logger.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,38 @@
package nibbler

import "log"
import (
"log"
"strings"
)

type DefaultLogger struct{}

func (logger DefaultLogger) Error(message string) {
log.Println(message)
func (logger DefaultLogger) Error(message ...string) {
log.Println(strings.Join(message, ""))
}

func (logger DefaultLogger) Warn(message string) {
log.Println(message)
func (logger DefaultLogger) Warn(message ...string) {
log.Println(strings.Join(message, ""))
}

func (logger DefaultLogger) Info(message string) {
log.Println(message)
func (logger DefaultLogger) Info(message ...string) {
log.Println(strings.Join(message, ""))
}

func (logger DefaultLogger) Debug(message string) {
log.Println(message)
func (logger DefaultLogger) Debug(message ...string) {
log.Println(strings.Join(message, ""))
}

type SilentLogger struct{}

func (logger SilentLogger) Error(message string) {
func (logger SilentLogger) Error(message ...string) {
}

func (logger SilentLogger) Warn(message string) {
func (logger SilentLogger) Warn(message ...string) {
}

func (logger SilentLogger) Info(message string) {
func (logger SilentLogger) Info(message ...string) {
}

func (logger SilentLogger) Debug(message string) {
func (logger SilentLogger) Debug(message ...string) {
}
2 changes: 1 addition & 1 deletion sample/sample.application.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (s *SampleExtension) AddRoutes(context *nibbler.Application) error {
func main() {

// allocate configuration
config, err := nibbler.LoadConfiguration(nil)
config, err := nibbler.LoadConfiguration()

// any error is fatal at this point
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions session/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ func (s *Extension) Init(app *nibbler.Application) error {
return errors.New("session extension requires connector")
}

storeConnector := s.StoreConnector
errConnect, store := storeConnector.Connect()
errConnect, store := s.StoreConnector.Connect()

// save the store to the extension
s.store = &store
Expand Down

0 comments on commit 9391598

Please sign in to comment.