summaryrefslogtreecommitdiff
path: root/internal/mapr
diff options
context:
space:
mode:
authorPaul Buetow <pbuetow@mimecast.com>2020-07-03 12:15:59 +0100
committerPaul Buetow <pbuetow@mimecast.com>2020-07-03 12:15:59 +0100
commiteb9c8d4ae7f8fb7e65f912ff4838c7737b5487d0 (patch)
treef5e6c0be15200b18f306878037c3896e1084cf53 /internal/mapr
parent912f7dce7222d345dc6cc6cc593a45ee7e2e15f8 (diff)
refactor mapr client
Diffstat (limited to 'internal/mapr')
-rw-r--r--internal/mapr/groupset.go19
-rw-r--r--internal/mapr/query.go4
2 files changed, 21 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.
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