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