forked from cloudfoundry/bosh-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cat.go
60 lines (53 loc) · 1.08 KB
/
cat.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
57
58
59
60
package main
import (
"bytes"
"errors"
"flag"
"fmt"
"os"
"time"
)
var (
stdoutFlag string
stderrFlag string
)
func init() {
flag.StringVar(&stdoutFlag, "stdout", "", "value will be written to Stdout")
flag.StringVar(&stderrFlag, "stderr", "", "value will be written to Stderr")
}
func TimedReader(buf *bytes.Buffer) error {
errCh := make(chan error, 1)
go func() {
_, err := buf.ReadFrom(os.Stdin)
errCh <- err
}()
select {
case err := <-errCh:
return err
case <-time.After(time.Second):
return errors.New("timeout")
}
return errors.New("THIS SHOULD NEVER HAPPEN!")
}
func main() {
flag.Parse()
if stdoutFlag == "" && stderrFlag == "" {
var buf bytes.Buffer
if err := TimedReader(&buf); err != nil {
fmt.Fprintf(os.Stderr, "Error: reading stdin: %s\n", err)
os.Exit(1)
}
if _, err := buf.WriteTo(os.Stdout); err != nil {
fmt.Fprintf(os.Stderr, "Error: writing to stout: %s\n", err)
os.Exit(1)
}
os.Exit(0)
}
if stdoutFlag != "" {
fmt.Fprintln(os.Stdout, stdoutFlag)
}
if stderrFlag != "" {
fmt.Fprintln(os.Stderr, stderrFlag)
}
os.Exit(0)
}