-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.go
51 lines (48 loc) · 1.16 KB
/
run.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
package kat
import (
"bufio"
"fmt"
"io"
"github.com/sean9999/go-flargs"
)
// Now we should have an array of real files.
// We can iterate over them and spit out the contents.
// We still report an error if there is a problem operating on these real files.
func (k *Konf) Run(env *flargs.Environment) error {
var lastKnownError error
line := 0
// stdin
if len(k.fileNames) == 0 {
if k.withNumbering {
scanner := bufio.NewScanner(env.InputStream)
for scanner.Scan() {
line++
fmt.Fprintf(env.OutputStream, "%d.\t%s\n", line, scanner.Text())
}
} else {
io.Copy(env.OutputStream, env.InputStream)
}
}
// files specified as args
for _, f := range k.files {
defer f.Close()
if k.withNumbering {
// output with numbers
// ex: 1. hello world
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line++
fmt.Fprintf(env.OutputStream, "%d.\t%s\n", line, scanner.Text())
}
} else {
_, err := io.Copy(env.OutputStream, f)
if err != nil {
lastKnownError = err
// should we panic and die, or continue on?
// let's be nice and continue on.
fmt.Fprintln(env.ErrorStream, err)
}
}
}
return lastKnownError
}