-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathread.go
More file actions
61 lines (52 loc) · 1.17 KB
/
read.go
File metadata and controls
61 lines (52 loc) · 1.17 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
package command
import (
"io"
"strings"
"github.com/gotify/cli/v2/utils"
)
func readMessage(args []string, r io.Reader, output chan<- string, split *rune) {
msgArgs := strings.Join(args, " ")
if msgArgs != "" {
if utils.ProbeStdin(r) {
utils.Exit1With("message is set via arguments and stdin, use only one of them")
}
output <- msgArgs
close(output)
return
}
var buf strings.Builder
for {
var tmp [256]byte
n, err := r.Read(tmp[:])
if err != nil {
if err.Error() == "EOF" {
break
}
utils.Exit1With(err)
}
tmpStr := string(tmp[:n])
if split != nil {
// split the message on the null character
parts := strings.Split(tmpStr, string(*split))
if len(parts) == 1 {
buf.WriteString(parts[0])
continue
}
previous := buf.String()
// fuse previous with parts[0], send parts[1] .. parts[n-2] and set parts[n-1] as new previous
firstMsg := previous + parts[0]
output <- firstMsg
for _, part := range parts[1 : len(parts)-1] {
output <- part
}
buf.Reset()
buf.WriteString(parts[len(parts)-1])
} else {
buf.WriteString(tmpStr)
}
}
if buf.Len() > 0 {
output <- buf.String()
}
close(output)
}