diff options
| author | Paul Buetow <pbuetow@mimecast.com> | 2020-07-03 12:15:59 +0100 |
|---|---|---|
| committer | Paul Buetow <pbuetow@mimecast.com> | 2020-07-03 12:15:59 +0100 |
| commit | eb9c8d4ae7f8fb7e65f912ff4838c7737b5487d0 (patch) | |
| tree | f5e6c0be15200b18f306878037c3896e1084cf53 /internal/mapr/groupset.go | |
| parent | 912f7dce7222d345dc6cc6cc593a45ee7e2e15f8 (diff) | |
refactor mapr client
Diffstat (limited to 'internal/mapr/groupset.go')
| -rw-r--r-- | internal/mapr/groupset.go | 19 |
1 files changed, 17 insertions, 2 deletions
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. |
