From 5699de00cb0cf8c1b865b749e3660c8695a93ea6 Mon Sep 17 00:00:00 2001 From: Martin Geno Date: Fri, 27 Oct 2017 13:07:12 +0200 Subject: [PATCH] [TASK] use golang-lib --- cmd/server.go | 31 +++++++++++-------- lib/config.go | 20 ------------- notify/config/config.go | 66 +++++++++++------------------------------ 3 files changed, 36 insertions(+), 81 deletions(-) diff --git a/cmd/server.go b/cmd/server.go index e06cb9e..b196bf0 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -9,6 +9,8 @@ import ( log "github.com/sirupsen/logrus" "github.com/spf13/cobra" + "github.com/genofire/golang-lib/file" + "github.com/genofire/golang-lib/worker" "github.com/genofire/logmania/bot" "github.com/genofire/logmania/lib" "github.com/genofire/logmania/notify" @@ -19,13 +21,14 @@ import ( ) var ( - configPath string - config *lib.Config - notifyState *configNotify.NotifyState - notifier notify.Notifier - receiver receive.Receiver - logChannel chan *log.Entry - logmaniaBot *bot.Bot + configPath string + config *lib.Config + notifyState *configNotify.NotifyState + stateSaveWorker *worker.Worker + notifier notify.Notifier + receiver receive.Receiver + logChannel chan *log.Entry + logmaniaBot *bot.Bot ) // serverCmd represents the serve command @@ -37,13 +40,14 @@ var serverCmd = &cobra.Command{ log.SetFormatter(&log.TextFormatter{ DisableTimestamp: true, }) - config, err := lib.ReadConfig(configPath) + config := &lib.Config{} + err := file.ReadTOML(configPath, config) if config == nil || err != nil { log.Panicf("Could not load '%s' for configuration.", configPath) } - notifyState := configNotify.ReadStateFile(config.Notify.StateFile) - go notifyState.Saver(config.Notify.StateFile) + notifyState = configNotify.ReadStateFile(config.Notify.StateFile) + stateSaveWorker = file.NewSaveJSONWorker(time.Minute, config.Notify.StateFile, notifyState) logmaniaBot = bot.NewBot(notifyState) @@ -87,6 +91,8 @@ var serverCmd = &cobra.Command{ } func quit() { + stateSaveWorker.Close() + file.SaveJSON(config.Notify.StateFile, notifyState) receiver.Close() notifier.Close() log.Info("quit of logmania") @@ -95,8 +101,9 @@ func quit() { func reload() { log.Info("reload config file") - config, err := lib.ReadConfig(configPath) - if config == nil || err != nil { + var config lib.Config + err := file.ReadTOML(configPath, &config) + if err != nil { log.Errorf("reload: could not load '%s' for new configuration. Skip reload.", configPath) return } diff --git a/lib/config.go b/lib/config.go index f83065c..c775d01 100644 --- a/lib/config.go +++ b/lib/config.go @@ -1,13 +1,5 @@ package lib -import ( - "io/ioutil" - - "github.com/BurntSushi/toml" - - log "github.com/sirupsen/logrus" -) - // Struct of the configuration // e.g. under github.com/genofire/logmania/logmania_example.conf type Config struct { @@ -44,15 +36,3 @@ type ReceiveConfig struct { Address string `toml:"address"` } `toml:"journald_json"` } - -// read configuration from a file (use toml as file-format) -func ReadConfig(path string) (*Config, error) { - log.Infof("load of configfile: %s", path) - var config Config - file, _ := ioutil.ReadFile(path) - err := toml.Unmarshal(file, &config) - if err != nil { - return nil, err - } - return &config, nil -} diff --git a/notify/config/config.go b/notify/config/config.go index 42fe78a..91844b3 100644 --- a/notify/config/config.go +++ b/notify/config/config.go @@ -1,11 +1,10 @@ package config import ( - "encoding/json" - "os" "regexp" "time" + "github.com/genofire/golang-lib/file" log "github.com/sirupsen/logrus" ) @@ -72,28 +71,25 @@ func (state *NotifyState) AddRegex(to, expression string) error { func ReadStateFile(path string) *NotifyState { var state NotifyState - if f, err := os.Open(path); err == nil { // transform data to legacy meshviewer - if err = json.NewDecoder(f).Decode(&state); err == nil { - log.Infof("loaded %d hosts", len(state.HostTo)) - if state.Lastseen == nil { - state.Lastseen = make(map[string]time.Time) - } - if state.LastseenNotify == nil { - state.LastseenNotify = make(map[string]time.Time) - } - if state.RegexIn == nil { - state.RegexIn = make(map[string]map[string]*regexp.Regexp) - } else { - for to, regexs := range state.RegexIn { - for exp, _ := range regexs { - state.AddRegex(to, exp) - } + + if err := file.ReadJSON(path, &state); err == nil { + log.Infof("loaded %d hosts", len(state.HostTo)) + if state.Lastseen == nil { + state.Lastseen = make(map[string]time.Time) + } + if state.LastseenNotify == nil { + state.LastseenNotify = make(map[string]time.Time) + } + if state.RegexIn == nil { + state.RegexIn = make(map[string]map[string]*regexp.Regexp) + } else { + for to, regexs := range state.RegexIn { + for exp, _ := range regexs { + state.AddRegex(to, exp) } } - return &state - } else { - log.Error("failed to unmarshal nodes:", err) } + return &state } else { log.Error("failed to open state notify file: ", path, ":", err) } @@ -107,14 +103,6 @@ func ReadStateFile(path string) *NotifyState { } } -func (state *NotifyState) Saver(path string) { - c := time.Tick(time.Minute) - - for range c { - state.SaveJSON(path) - } -} - func (state *NotifyState) Alert(expired time.Duration, send func(e *log.Entry) error) { c := time.Tick(time.Minute) @@ -134,23 +122,3 @@ func (state *NotifyState) Alert(expired time.Duration, send func(e *log.Entry) e } } } - -// SaveJSON to path -func (state *NotifyState) SaveJSON(outputFile string) { - tmpFile := outputFile + ".tmp" - - f, err := os.OpenFile(tmpFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) - if err != nil { - log.Panic(err) - } - - err = json.NewEncoder(f).Encode(state) - if err != nil { - log.Panic(err) - } - - f.Close() - if err := os.Rename(tmpFile, outputFile); err != nil { - log.Panic(err) - } -}