forked from superfly/flyctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstdin.go
46 lines (38 loc) · 842 Bytes
/
stdin.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
package helpers
import (
"bufio"
"fmt"
"io"
"os"
"strings"
)
func IsTerminal() bool {
if fileInfo, _ := os.Stdout.Stat(); (fileInfo.Mode() & os.ModeCharDevice) != 0 {
return true
}
return false
}
func ReadStdin(maxLength int) (string, error) {
reader := bufio.NewReader(os.Stdin)
var output []rune
bytesRead := 0
for {
input, size, err := reader.ReadRune()
if err != nil && err == io.EOF {
break
} else if err != nil {
return "", err
}
bytesRead += size
if bytesRead > maxLength {
return "", fmt.Errorf("Input exceeded max length of %d bytes", maxLength)
}
output = append(output, input)
}
return strings.TrimSpace(string(output)), nil
}
// HasPipedStdin returns if stdin has piped input
func HasPipedStdin() bool {
stat, _ := os.Stdin.Stat()
return (stat.Mode() & os.ModeCharDevice) == 0
}