Skip to content

Commit

Permalink
Merge branch 'master' of github.com:unchainio/pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
e-nikolov committed Aug 6, 2018
2 parents 543ab84 + a62266b commit c3195db
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
47 changes: 47 additions & 0 deletions xconfig/xconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
_ "github.com/spf13/viper/remote"
"github.com/unchainio/pkg/xlogger"
"github.com/unchainio/pkg/xpath"
"github.com/fsnotify/fsnotify"
"os"
)

func Load(cfg interface{}, optFuncs ...OptionFunc) error {
Expand Down Expand Up @@ -51,6 +53,42 @@ func Load(cfg interface{}, optFuncs ...OptionFunc) error {
})
}

if opts.watch {
if len(opts.paths) < 1 {
return errors.New("can only watch config changes when path flag is set")
}
go func() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
panic(err)
}
defer watcher.Close()

dir, err := os.Getwd()

for _, path := range opts.paths {
done := make(chan bool)

go func() {
for {
select {
case event := <-watcher.Events:
if event.Op&fsnotify.Write == fsnotify.Write || event.Op&fsnotify.Rename == fsnotify.Rename {
panic("CONFIG UPDATED - FORCING RESTART")
}
}
}
}()
err = watcher.Add(fmt.Sprintf(dir + "/" + path))
if err != nil {
panic(err)
}
<- done
}

}()
}

log.Printf("Loading config from %+v", opts.paths)

if len(opts.paths) != 0 {
Expand Down Expand Up @@ -100,6 +138,7 @@ type OptionFunc func(*Options) error

type Options struct {
verbose bool
watch bool

pathFlag *flag.Flag
paths []string
Expand All @@ -125,6 +164,14 @@ func Verbose(flag bool) OptionFunc {
}
}

func WithWatcher() OptionFunc {
return func(o *Options) error {
o.watch = true

return nil
}
}

func WithViper(v *viper.Viper) OptionFunc {
return func(o *Options) error {
o.viper = v
Expand Down
8 changes: 6 additions & 2 deletions xlogger/xlogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import (
)

type Config struct {
Level string
Format string
Level string
Format string
HideFName bool
}

type Logger struct {
Expand Down Expand Up @@ -87,6 +88,9 @@ func (l *Logger) Log(log *logrus.Logger) *logrus.Entry {

caller := fmt.Sprintf("%s:%v", file, line)

if l.cfg.HideFName {
return log.WithField("caller", caller)
}
return log.WithField("caller", caller).WithField("fName", fName)
}
return &logrus.Entry{}
Expand Down

0 comments on commit c3195db

Please sign in to comment.