-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
89 lines (77 loc) · 1.9 KB
/
main.go
File metadata and controls
89 lines (77 loc) · 1.9 KB
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
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"os"
"strings"
"time"
"checker/engine"
"checker/ui"
)
func main() {
var (
targetPath = flag.String("path", "", "target file or directory")
outPath = flag.String("out", "", "output file path")
format = flag.String("format", "json", "output format: json|csv")
noGUI = flag.Bool("cli", false, "run in CLI mode")
subdirs = flag.Bool("subdirs", true, "scan subdirectories")
)
flag.Parse()
if *noGUI || strings.TrimSpace(*targetPath) != "" {
if err := runCLI(*targetPath, *outPath, *format, *subdirs); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
return
}
ui.Run()
}
func runCLI(targetPath, outPath, format string, subdirs bool) error {
if strings.TrimSpace(targetPath) == "" {
return fmt.Errorf("missing -path")
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
start := time.Now()
report, err := engine.ScanPath(ctx, targetPath, engine.Options{
IncludeSubdirs: subdirs,
})
if err != nil {
return err
}
if outPath == "" {
switch strings.ToLower(format) {
case "csv":
return engine.WriteCSV(os.Stdout, report)
case "json":
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
return enc.Encode(report)
default:
return fmt.Errorf("unsupported -format: %s", format)
}
}
f, err := os.Create(outPath)
if err != nil {
return err
}
defer f.Close()
switch strings.ToLower(format) {
case "csv":
if err := engine.WriteCSV(f, report); err != nil {
return err
}
case "json":
enc := json.NewEncoder(f)
enc.SetIndent("", " ")
if err := enc.Encode(report); err != nil {
return err
}
default:
return fmt.Errorf("unsupported -format: %s", format)
}
fmt.Fprintf(os.Stdout, "done: %d findings, %d files, %s\n", len(report.Findings), report.Stats.FilesScanned, time.Since(start).Truncate(time.Millisecond))
return nil
}