-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathread.go
More file actions
63 lines (55 loc) · 1.11 KB
/
read.go
File metadata and controls
63 lines (55 loc) · 1.11 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 command
import (
"bufio"
"errors"
"fmt"
"io"
"os"
"runtime"
"strings"
"github.com/gotify/cli/v2/utils"
"github.com/mattn/go-isatty"
)
func readMessage(args []string, r io.Reader, output chan<- string, splitOnNull bool) {
defer close(output)
if len(args) > 0 {
if utils.ProbeStdin(r) {
utils.Exit1With("message is set via arguments and stdin, use only one of them")
}
output <- strings.Join(args, " ")
return
}
if isatty.IsTerminal(os.Stdin.Fd()) {
eofKey := "Ctrl+D"
if runtime.GOOS == "windows" {
eofKey = "Ctrl+Z"
}
fmt.Fprintf(os.Stderr, "Enter your message, press Enter and then %s to finish:\n", eofKey)
}
if splitOnNull {
read := bufio.NewReader(r)
for {
s, err := read.ReadString('\x00')
if err != nil {
if !errors.Is(err, io.EOF) {
utils.Exit1With("read error", err)
}
if len(s) > 0 {
output <- s
}
return
} else {
if len(s) > 1 {
output <- strings.TrimSuffix(s, "\x00")
}
}
}
} else {
bytes, err := io.ReadAll(r)
if err != nil {
utils.Exit1With("cannot read", err)
}
output <- string(bytes)
return
}
}