-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
57 lines (48 loc) · 1.14 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
package main
import (
"fmt"
"os"
"sort"
"github.com/spf13/pflag"
)
var (
countByBytes = pflag.BoolP("bytes", "c", false, "count by bytes")
countByChars = pflag.BoolP("chars", "m", false, "count by chars")
countByWords = pflag.BoolP("words", "w", false, "count by words")
countByLines = pflag.BoolP("lines", "l", false, "count by lines")
files0From = pflag.String("files0-from", "", "files from file, - for stdin")
sortBy = pflag.String("sort", "fname", "sort by: chars, words, lines, fname")
)
func init() {
pflag.Parse()
if !*countByLines && !*countByBytes && !*countByChars {
*countByWords = true
}
}
func main() {
var (
files []string
err error
)
files = []string{"/dev/stdin"}
if len(*files0From) != 0 {
files, err = collectFiles(*files0From)
if err != nil {
fmt.Fprintf(os.Stderr, "collect files error: %v\n", err)
os.Exit(1)
}
}
var (
infos = FileStat{}
)
for _, f := range files {
info, err := countFile(f)
if err != nil {
fmt.Fprintf(os.Stderr, "count error, file: %s, err: %v", f, err)
}
//fmt.Printf("info: %v\n", info)
infos = append(infos, info)
}
sort.Sort(infos)
infos.Dump()
}