-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.go
86 lines (77 loc) · 2.02 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"flag"
"fmt"
"os"
"strings"
"time"
"github.com/neko-neko/utmpdump/filter"
"github.com/neko-neko/utmpdump/printer"
"github.com/neko-neko/utmpdump/utmp"
)
func main() {
var (
filePath string
until string
since string
outputType string
)
flag.Usage = func() {
fmt.Fprintf(os.Stderr, `Usage of %s: %s [options]
-f, --file <file> load a specific file instead of utmp
-t, --until <YYYYMMDDHHMMSS> display the lines until the specified time
-s, --since <YYYYMMDDHHMMSS> display the lines since the specified time
-o, --output <json/tsv/csv> display the lines at specific format. Default format is json.
`, os.Args[0], os.Args[0])
}
// parse arguments
flag.StringVar(&filePath, "f", "", "file path")
flag.StringVar(&filePath, "file", "", "file path")
flag.StringVar(&until, "t", "", "until")
flag.StringVar(&until, "until", "", "until")
flag.StringVar(&since, "s", "", "since")
flag.StringVar(&since, "since", "", "since")
flag.StringVar(&outputType, "o", "", "output type")
flag.StringVar(&outputType, "output", "", "output type")
flag.Parse()
if filePath == "" {
flag.Usage()
os.Exit(1)
}
outputType = strings.ToLower(outputType)
if _, exist := printer.PrintTypes[outputType]; outputType != "" && !exist {
flag.Usage()
os.Exit(1)
}
sinceTime, _ := time.Parse("20060102150405", since)
untilTime, _ := time.Parse("20060102150405", until)
// read file
file, openErr := os.Open(filePath)
defer file.Close()
if openErr != nil {
fmt.Fprintln(os.Stderr, openErr)
os.Exit(1)
}
utmps, readErr := utmp.Read(file)
if readErr != nil {
fmt.Fprintln(os.Stderr, readErr)
os.Exit(1)
}
// to go utmp
var goUtmps []*utmp.GoUtmp
for _, gu := range utmps {
goUtmps = append(goUtmps, utmp.NewGoUtmp(gu))
}
// filter
filters := []filter.Filter{
&filter.TimeFilter{Since: sinceTime, Until: untilTime},
}
for _, filter := range filters {
goUtmps = filter.Filter(goUtmps)
}
// print
printer := printer.GetPrinter(outputType)
for _, u := range goUtmps {
printer.Print(u)
}
}