-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
41 lines (35 loc) · 977 Bytes
/
main.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
//go:generate go-bindata -o gopherart/gopherart.go -pkg gopherart gopherart/gopher.ascii
package main
import (
"bufio"
"os"
"strings"
"github.com/adamryman/gophersay/gopher"
"golang.org/x/crypto/ssh/terminal"
)
func main() {
// If there are any command line arguments, join them together with spaces
// and output them as the saying
if len(os.Args) > 1 {
gopher.Say(os.Stdout, strings.Join(os.Args[1:], " "))
return
}
// If there is no data on stdin
if terminal.IsTerminal(int(os.Stdin.Fd())) {
gopher.Proverb(os.Stdout)
return
}
// We found data on stdin
data := os.Stdin
scan := bufio.NewScanner(data)
var stdInSlice []string
for scan.Scan() {
stdInSlice = append(stdInSlice, scan.Text()+"\n")
}
// Take the newline off the last line
lastLine := stdInSlice[len(stdInSlice)-1]
lastLine = strings.TrimSuffix(lastLine, "\n")
stdInSlice[len(stdInSlice)-1] = lastLine
// Join the lines
gopher.Say(os.Stdout, strings.Join(stdInSlice, ""))
}