-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtest.go
More file actions
63 lines (53 loc) · 1.7 KB
/
test.go
File metadata and controls
63 lines (53 loc) · 1.7 KB
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
package reporters
import (
"encoding/json"
"path/filepath"
"github.com/ActiveState/cli/internal/analytics/dimensions"
"github.com/ActiveState/cli/internal/config"
"github.com/ActiveState/cli/internal/constants"
"github.com/ActiveState/cli/internal/errs"
"github.com/ActiveState/cli/internal/fileutils"
"github.com/ActiveState/cli/internal/installation/storage"
"github.com/ActiveState/cli/internal/logging"
configMediator "github.com/ActiveState/cli/internal/mediators/config"
)
type TestReporter struct {
path string
cfg *config.Instance
}
const TestReportFilename = "analytics.log"
func TestReportFilepath() string {
appdata := storage.AppDataPath()
logging.Warning("Appdata path: %s", appdata)
return filepath.Join(appdata, TestReportFilename)
}
func NewTestReporter(path string, cfg *config.Instance) *TestReporter {
reporter := &TestReporter{path, cfg}
configMediator.AddListener(constants.AnalyticsPixelOverrideConfig, func() {
reporter.cfg = cfg
})
return reporter
}
func (r *TestReporter) ID() string {
return "TestReporter"
}
type TestLogEntry struct {
Category string
Action string
Source string
Label string
URL string
Dimensions *dimensions.Values
}
func (r *TestReporter) Event(category, action, source, label string, d *dimensions.Values) error {
url := r.cfg.GetString(constants.AnalyticsPixelOverrideConfig)
b, err := json.Marshal(TestLogEntry{category, action, source, label, url, d})
if err != nil {
return errs.Wrap(err, "Could not marshal test log entry")
}
b = append(b, []byte("\n\x00")...)
if err := fileutils.AmendFileLocked(r.path, b, fileutils.AmendByAppend); err != nil {
return errs.Wrap(err, "Could not write to file")
}
return nil
}