-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
44 lines (37 loc) · 848 Bytes
/
writer.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
package astilectron
import (
"encoding/json"
"fmt"
"io"
"github.com/asticode/go-astikit"
)
// writer represents an object capable of writing in the TCP server
type writer struct {
l astikit.SeverityLogger
w io.WriteCloser
}
// newWriter creates a new writer
func newWriter(w io.WriteCloser, l astikit.SeverityLogger) *writer {
return &writer{
l: l,
w: w,
}
}
// close closes the writer properly
func (w *writer) close() error {
return w.w.Close()
}
// write writes to the stdin
func (w *writer) write(e Event) (err error) {
// Marshal
var b []byte
if b, err = json.Marshal(e); err != nil {
return fmt.Errorf("marshaling %+v failed: %w", e, err)
}
// Write
w.l.Debugf("Sending to Astilectron: %s", b)
if _, err = w.w.Write(append(b, '\n')); err != nil {
return fmt.Errorf("writing %s failed: %w", b, err)
}
return
}