forked from whyrusleeping/ipfs-counter
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathoutput.go
64 lines (58 loc) · 1.23 KB
/
output.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"encoding/json"
"os"
)
// Output writes recorded output to a file at filePath
func Output(filePath string, r *Recorder) error {
if r.Client != nil {
return r.Finish()
}
if err := OutputNodes(filePath+".nodes.json", r); err != nil {
return err
}
return OutputTrials(filePath+".trials.json", r)
}
// OutputNodes writes recorded output to a file at filePath
func OutputNodes(filePath string, r *Recorder) error {
f, err := os.Create(filePath)
defer func() {
if err := f.Close(); err != nil {
r.log.Warnf("Could not close %s: %v", filePath, err)
}
}()
if err != nil {
return err
}
e := json.NewEncoder(f)
for _, rec := range r.records {
err := e.Encode(rec)
if err != nil {
return err
}
}
return nil
}
// OutputTrials writes recorded output to a file at filePath
func OutputTrials(filePath string, r *Recorder) error {
f, err := os.Create(filePath)
defer func() {
if err := f.Close(); err != nil {
r.log.Warnf("Could not close %s: %v", filePath, err)
}
}()
if err != nil {
return err
}
e := json.NewEncoder(f)
var lastErr error
r.dials.Range(func(k, v interface{}) bool {
err := e.Encode(v)
if err != nil {
lastErr = err
return false
}
return true
})
return lastErr
}