forked from u-root/u-root
/
tee.go
75 lines (61 loc) · 1.66 KB
/
tee.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright 2013-2018 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Tee transcribes the standard input to the standard output and makes copies
// in the files.
//
// Synopsis:
// tee [-ai] FILES...
//
// Options:
// -a, --append: append the output to the files rather than rewriting them
// -i, --ignore-interrupts: ignore the SIGINT signal
package main
import (
"fmt"
"io"
"log"
"os"
"os/signal"
flag "github.com/spf13/pflag"
)
const name = "tee"
var (
cat = flag.BoolP("append", "a", false, "append the output to the files rather than rewriting them")
ignore = flag.BoolP("ignore-interrupts", "i", false, "ignore the SIGINT signal")
)
// handeFlags parses all the flags and sets variables accordingly
func handleFlags() int {
flag.Parse()
oflags := os.O_WRONLY | os.O_CREATE
if *cat {
oflags |= os.O_APPEND
}
if *ignore {
signal.Ignore(os.Interrupt)
}
return oflags
}
func main() {
oflags := handleFlags()
files := make([]*os.File, 0, flag.NArg())
writers := make([]io.Writer, 0, flag.NArg()+1)
for _, fname := range flag.Args() {
f, err := os.OpenFile(fname, oflags, 0666)
if err != nil {
log.Fatalf("%s: error opening %s: %v", name, fname, err)
}
files = append(files, f)
writers = append(writers, f)
}
writers = append(writers, os.Stdout)
mw := io.MultiWriter(writers...)
if _, err := io.Copy(mw, os.Stdin); err != nil {
log.Fatalf("%s: error: %v", name, err)
}
for _, f := range files {
if err := f.Close(); err != nil {
fmt.Fprintf(os.Stderr, "%s: error closing file %q: %v\n", name, f.Name(), err)
}
}
}