Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

Commit cd63ea8

Browse files
author
Sauyon Lee
committed
extractor: revamp argument parsing
1 parent 2da89c6 commit cd63ea8

File tree

1 file changed

+58
-10
lines changed

1 file changed

+58
-10
lines changed

extractor/cli/go-extractor/go-extractor.go

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,70 @@ func usage() {
2020
fmt.Fprintf(os.Stderr, "--help Print this help.\n")
2121
}
2222

23-
func parseFlags(args []string) ([]string, []string) {
23+
func parseFlags(args []string, mimic bool) ([]string, []string) {
2424
i := 0
2525
buildFlags := []string{}
26-
for i < len(args) && strings.HasPrefix(args[i], "-") {
26+
for ; i < len(args) && strings.HasPrefix(args[i], "-"); i++ {
2727
if args[i] == "--" {
2828
i++
2929
break
3030
}
3131

32-
if args[i] == "--help" {
33-
usage()
34-
os.Exit(0)
35-
} else {
36-
buildFlags = append(buildFlags, args[i])
32+
if !mimic {
33+
// we're not in mimic mode, try to parse our arguments
34+
switch args[i] {
35+
case "--help":
36+
usage()
37+
os.Exit(0)
38+
case "--mimic":
39+
if i+1 < len(args) {
40+
i++
41+
compiler := args[i]
42+
log.Printf("Compiler: %s", compiler)
43+
if i+1 < len(args) {
44+
i++
45+
command := args[i]
46+
if command == "build" || command == "install" || command == "run" {
47+
log.Printf("Intercepting build")
48+
return parseFlags(args[i+1:], true)
49+
} else {
50+
log.Printf("Non-build command '%s'; skipping", strings.Join(args[1:], " "))
51+
os.Exit(0)
52+
}
53+
} else {
54+
log.Printf("Non-build command '%s'; skipping", strings.Join(args[1:], " "))
55+
os.Exit(0)
56+
}
57+
} else {
58+
log.Fatalf("Invalid --mimic: no compiler specified")
59+
}
60+
}
3761
}
3862

39-
i++
63+
// parse go build flags
64+
switch args[i] {
65+
// skip `-o output` and `-i`, if applicable
66+
case "-o":
67+
if i+1 < len(args) {
68+
i++
69+
}
70+
case "-i":
71+
case "-p", "-asmflags", "-buildmode", "-compiler", "-gccgoflags", "-gcflags", "-installsuffix",
72+
"-ldflags", "-mod", "-modfile", "-pkgdir", "-tags", "-toolexec":
73+
if i+1 < len(args) {
74+
buildFlags = append(buildFlags, args[i], args[i+1])
75+
i++
76+
} else {
77+
buildFlags = append(buildFlags, args[i])
78+
}
79+
default:
80+
if strings.HasPrefix(args[i], "-") {
81+
buildFlags = append(buildFlags, args[i])
82+
} else {
83+
// stop parsing if the argument is not a flag (and so is positional)
84+
break
85+
}
86+
}
4087
}
4188

4289
cpuprofile = os.Getenv("CODEQL_EXTRACTOR_GO_CPU_PROFILE")
@@ -46,7 +93,7 @@ func parseFlags(args []string) ([]string, []string) {
4693
}
4794

4895
func main() {
49-
buildFlags, patterns := parseFlags(os.Args[1:])
96+
buildFlags, patterns := parseFlags(os.Args[1:], false)
5097

5198
if cpuprofile != "" {
5299
f, err := os.Create(cpuprofile)
@@ -63,9 +110,10 @@ func main() {
63110
if len(patterns) == 0 {
64111
log.Println("Nothing to extract.")
65112
} else {
113+
log.Printf("Build flags: '%s'; patterns: '%s'\n", strings.Join(buildFlags, " "), strings.Join(patterns, " "))
66114
err := extractor.ExtractWithFlags(buildFlags, patterns)
67115
if err != nil {
68-
log.Fatal(err)
116+
log.Fatalf("Error running go tooling: %s\n", err.Error())
69117
}
70118
}
71119

0 commit comments

Comments
 (0)