-
Notifications
You must be signed in to change notification settings - Fork 0
/
toolexec.go
68 lines (54 loc) · 1.51 KB
/
toolexec.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
// (c) Copyright IBM Corp. 2021
// (c) Copyright Instana Inc. 2021
package main
import (
"fmt"
"os/exec"
"strings"
)
// parseToolchainCmd returns an exec.Cmd to execute the Go toolchain command
// passed as an argument string. It may return nil in case there were no args
// provided.
func parseToolchainCmd(args []string) *exec.Cmd {
if len(args) == 0 {
return nil
}
path, args := args[0], args[1:]
cmd := exec.Command(path, args...)
return cmd
}
type toolchainCompileArgs struct {
Output string
Package string
Files []string
}
// Complete returns whether $GOTOOLDIR/compile has been called to compile a single package
func (f toolchainCompileArgs) Complete() bool {
return f.Output != "" && f.Package != ""
}
// parseToolchainCompileArgs parses the $GOTOOLDIR/compile args and extracts the list
// of files to compile
func parseToolchainCompileArgs(args []string) (toolchainCompileArgs, error) {
var flags toolchainCompileArgs
for i := range args {
switch args[i] {
case "-o":
if i+1 >= len(args) || strings.HasPrefix(args[i+1], "-") {
return flags, fmt.Errorf("compile tool -o flag missing mandatory value")
}
flags.Output = args[i+1]
case "-p":
if i+1 >= len(args) || strings.HasPrefix(args[i+1], "-") {
return flags, fmt.Errorf("compile tool -p flag missing mandatory value")
}
flags.Package = args[i+1]
}
}
for i := len(args) - 1; i >= 0; i-- {
if !strings.HasSuffix(args[i], ".go") {
break
}
flags.Files = append(flags.Files, args[i])
}
return flags, nil
}