From eb9c8d4ae7f8fb7e65f912ff4838c7737b5487d0 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Fri, 3 Jul 2020 12:15:59 +0100 Subject: refactor mapr client --- internal/mapr/groupset.go | 19 +++++++++++++++++-- internal/mapr/query.go | 4 ++++ 2 files changed, 21 insertions(+), 2 deletions(-) (limited to 'internal/mapr') diff --git a/internal/mapr/groupset.go b/internal/mapr/groupset.go index e9e0d37..6ee2811 100644 --- a/internal/mapr/groupset.go +++ b/internal/mapr/groupset.go @@ -5,9 +5,12 @@ import ( "errors" "fmt" "io/ioutil" + "os" "sort" "strconv" "strings" + + "github.com/mimecast/dtail/internal/io/logger" ) // GroupSet represents a map of aggregate sets. The group sets @@ -60,7 +63,7 @@ func (g *GroupSet) Result(query *Query) (string, int, error) { // WriteResult writes the result to an outfile. func (g *GroupSet) WriteResult(query *Query) error { - if query.Outfile == "" { + if !query.HasOutfile() { return errors.New("No outfile specified") } @@ -70,7 +73,19 @@ func (g *GroupSet) WriteResult(query *Query) error { return err } - return ioutil.WriteFile(query.Outfile, []byte(result), 0644) + logger.Info("Writing outfile", query.Outfile) + tmpOutfile := fmt.Sprintf("%s.tmp", query.Outfile) + + if err := ioutil.WriteFile(tmpOutfile, []byte(result), 0644); err != nil { + return err + } + + if err := os.Rename(tmpOutfile, query.Outfile); err != nil { + os.Remove(tmpOutfile) + return err + } + + return nil } // Return a nicely formated result of the query from the group set. diff --git a/internal/mapr/query.go b/internal/mapr/query.go index be766d1..6dff792 100644 --- a/internal/mapr/query.go +++ b/internal/mapr/query.go @@ -68,6 +68,10 @@ func NewQuery(queryStr string) (*Query, error) { return &q, err } +func (q *Query) HasOutfile() bool { + return q.Outfile != "" +} + func (q *Query) parse(tokens []token) error { var found []token var err error -- cgit v1.2.3