-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
97 lines (82 loc) · 1.97 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
package main
import (
"bytes"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path"
"strings"
"./analyzer"
)
func main() {
var (
_type string
config string
outputDir string
)
flag.StringVar(&_type, "type", "", "analyzer type")
flag.StringVar(&config, "config", "", "args for the specified analyzer")
flag.StringVar(&outputDir, "output-dir", "artifacts/analysis", "directory to store analysis output")
flag.Parse()
cliArgs := analyzer.CliArgs{Type: _type, Config: config}
az, err := analyzer.GetAnalyzer(cliArgs)
if err != nil {
log.Fatalf("invalid analyzer type %v", cliArgs.Type)
}
cmd := exec.Command(flag.Arg(0), flag.Args()[1:]...)
var stdoutBuf, stderrBuf, combinedBuf bytes.Buffer
cmd.Stdout = io.MultiWriter(os.Stdout, &stdoutBuf, &combinedBuf)
cmd.Stderr = io.MultiWriter(os.Stderr, &stderrBuf, &combinedBuf)
cmd.Run()
stdoutStr, stderrStr, stdoutStderrStr := string(stdoutBuf.Bytes()), string(stderrBuf.Bytes()), string(combinedBuf.Bytes())
code := cmd.ProcessState.ExitCode()
result := az.Run(
analyzer.Args{
Exitcode: code,
StdoutStr: stdoutStr,
StderrStr: stderrStr,
StdoutStderrStr: stdoutStderrStr,
},
)
var outputDirAbs string
if path.IsAbs(outputDir) {
outputDirAbs = outputDir
} else {
cwd, _ := os.Getwd()
outputDirAbs = fmt.Sprintf("%s/%s", cwd, outputDir)
}
os.RemoveAll(outputDirAbs)
err = os.MkdirAll(outputDirAbs, 0755)
if err != nil {
log.Fatalf("could not create dir at %v", outputDirAbs)
}
ioutil.WriteFile(
fmt.Sprintf("%s/output", outputDirAbs),
[]byte(
strings.TrimSpace(
fmt.Sprintf("%s %s", result.Category, result.Subcategory),
),
),
0644,
)
ioutil.WriteFile(
fmt.Sprintf("%s/stdout", outputDirAbs),
[]byte(stdoutStr),
0644,
)
ioutil.WriteFile(
fmt.Sprintf("%s/stderr", outputDirAbs),
[]byte(stderrStr),
0644,
)
ioutil.WriteFile(
fmt.Sprintf("%s/stdoutStderr", outputDirAbs),
[]byte(stdoutStderrStr),
0644,
)
os.Exit(code)
}