public
Created

Tee not quite right

  • Download Gist
tee.go
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
package main
 
import (
"io"
"bufio"
"os"
"flag"
"log"
)
 
var appendPtr = flag.Bool("a", false, "Append the output to the files rather than overwriting them.")
var ignoreCasePtr = flag.Bool("i", false, "Ignore the SIGINT signal.")
 
func main() {
var err error
 
flag.Parse()
 
bufferedStdin := bufio.NewReader(os.Stdin)
bufferedStdout := bufio.NewWriter(os.Stdout)
 
var writers = []bufio.Writer{*bufferedStdout}
 
for _, teeTo := range flag.Args() {
var w io.Writer
if *appendPtr {
w, err = os.OpenFile(teeTo, os.O_CREATE|os.O_APPEND, 0644)
} else {
w, err = os.OpenFile(teeTo, os.O_CREATE, 0644)
}
 
if err != nil {
log.Fatal(err)
}
 
bufwriter := bufio.NewWriter(w)
writers = append(writers, *bufwriter)
}
 
teeWriter := io.MultiWriter(writers)
 
io.Copy(teeWriter, bufferedStdin)
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.