-
-
Save Mistobaan/4146031 to your computer and use it in GitHub Desktop.
Tee not quite right
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = []io.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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment