-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmain.go
149 lines (121 loc) · 2.77 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/keltia/archive"
"github.com/pkg/errors"
)
var (
// MyName is the application
MyName = filepath.Base(os.Args[0])
// MyVersion is our version
MyVersion = "0.15.0,parallel"
// Author should be obvious
Author = "Ollivier Robert"
fDebug bool
fJobs int
fNoResolv bool
fSort string
fType string
fVerbose bool
fVersion bool
)
// Context is passed around rather than being a global var/struct
type Context struct {
r Resolver
jobs int
}
func init() {
flag.BoolVar(&fDebug, "D", false, "Debug mode")
flag.BoolVar(&fNoResolv, "N", false, "Do not resolve IPs")
flag.IntVar(&fJobs, "j", runtime.NumCPU(), "Parallel jobs")
flag.StringVar(&fSort, "S", `"Count" "dsc"`, "Sort results")
flag.StringVar(&fType, "t", "", "File type for stdin mode")
flag.BoolVar(&fVerbose, "v", false, "Verbose mode")
flag.BoolVar(&fVersion, "version", false, "Display version")
}
func Version() {
fmt.Printf("%s version %s/j%d archive/%s\n", MyName, MyVersion, fJobs, archive.Version())
}
// Setup creates our context and check stuff
func Setup(a []string) (*Context, error) {
// Exist early if -version
if fVersion {
Version()
return nil, nil
}
if fDebug {
fVerbose = true
debug("debug mode")
}
if len(a) < 1 {
return nil, fmt.Errorf("You must specify at least one file.")
}
ctx := &Context{RealResolver{}, fJobs}
// Make it easier to sub it out
if fNoResolv {
ctx.r = NullResolver{}
}
return ctx, nil
}
func SelectInput(file string) (io.ReadCloser, error) {
debug("file=%s", file)
if file == "-" {
if fType == "" {
return nil, errors.New("Wrong file type, use -t")
}
return os.Stdin, nil
}
// We have a filename
if !checkFilename(file) {
return nil, errors.New("bad filename")
}
// We want the full path
myfile, err := filepath.Abs(file)
if err != nil {
return nil, errors.Wrapf(err, "Abs(%s)", file)
}
return os.Open(myfile)
}
func realmain(args []string) error {
ctx, err := Setup(args)
if ctx == nil {
return errors.Wrap(err, "realmain")
}
var txt string
// Look for input file or stdin/"-"
file := args[0]
verbose("Analyzing %s", file)
var fType = filepath.Ext(file)
if strings.ToLower(fType) == ".zip" {
txt, err = HandleZipFile(ctx, file)
if err != nil {
return errors.Wrapf(err, "file %s:", file)
}
} else {
in, err := SelectInput(file)
if err != nil {
return errors.Wrap(err, "SelectInput")
}
defer in.Close()
typ := archive.Ext2Type(fType)
txt, err = HandleSingleFile(ctx, in, typ)
if err != nil {
return errors.Wrapf(err, "file %s:", file)
}
}
fmt.Println(txt)
return nil
}
func main() {
flag.Parse()
if err := realmain(flag.Args()); err != nil {
log.Fatalf("Error: %v\n", err)
}
}