-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipe.go
56 lines (44 loc) · 997 Bytes
/
pipe.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
package ezquake
import (
"fmt"
"os"
"strings"
"sync"
)
type PipeWriter struct {
username string
mux sync.Mutex
}
func NewPipeWriter(username string) *PipeWriter {
return &PipeWriter{
username: username,
mux: sync.Mutex{},
}
}
func (w *PipeWriter) Write(value string) error {
trimmedValue := strings.TrimSpace(value)
if 0 == len(trimmedValue) {
return nil
}
terminatedValue := strings.TrimRight(trimmedValue, ";") + ";"
return w.writeToPipe(terminatedValue)
}
func (w *PipeWriter) writeToPipe(value string) error {
w.mux.Lock()
defer w.mux.Unlock()
file, errOpen := os.OpenFile(w.path(), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
defer file.Close()
if errOpen != nil {
return errOpen
}
_, errWrite := file.WriteString(value)
return errWrite
}
func (w *PipeWriter) path() string {
return fmt.Sprintf("/tmp/ezquake_fifo_%s", w.username)
}
func (w *PipeWriter) Clear() error {
w.mux.Lock()
defer w.mux.Unlock()
return os.Truncate(w.path(), 0)
}