-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
63 lines (60 loc) · 1.33 KB
/
main.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
package main
import (
"bufio"
"compress/bzip2"
"compress/gzip"
"flag"
"fmt"
"io"
"log"
"os"
"path"
"reflect"
)
// TransparentExpandingReader creates a Reader that transparently decompresses based
// on filename. Supports gzip and bzip2; falls back to assuming uncompressed
func TransparentExpandingReader(key string, source io.ReadCloser) (io.Reader, error) {
ext := path.Ext(key)
var reader io.Reader
var err error
switch {
case ext == ".gz":
reader, err = gzip.NewReader(source)
if err != nil {
return nil, err
}
case ext == ".bz2":
reader = bzip2.NewReader(source)
default:
reader = bufio.NewReader(source)
}
return reader, nil
}
func main() {
summary := flag.Bool("summary", false, "summarize file types and line counts only")
flag.Parse()
for _,arg := range flag.Args() {
f,err := os.Open(arg)
defer f.Close()
if err != nil {
log.Printf("error opening %s: %v", arg, err)
continue
}
reader,err := TransparentExpandingReader(arg, f)
if err != nil {
log.Printf("error reading %s: %v", arg, err)
continue
}
scanner := bufio.NewScanner(reader)
n := 0
for scanner.Scan() {
n++
if ! *summary {
fmt.Println(scanner.Text())
}
}
if *summary {
fmt.Printf("%20s %10d %s\n", reflect.TypeOf(reader), n, arg)
}
}
}