-
Notifications
You must be signed in to change notification settings - Fork 8
/
exec.go
169 lines (155 loc) · 4.33 KB
/
exec.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package util
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
log "github.com/sirupsen/logrus"
)
func ExecCmd(cmd *exec.Cmd) error {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
log.Debugf("Running command: %v", cmd.Args)
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to exec %#v: %v", cmd.Args, err)
}
return nil
}
// GoCmdOptions is the base option set for "go" subcommands.
type GoCmdOptions struct {
// BinName is the name of the compiled binary, passed to -o.
BinName string
// Args are args passed to "go {cmd}", aside from "-o {bin_name}" and
// test binary args.
// These apply to build, clean, get, install, list, run, and test.
Args []string
// PackagePath is the path to the main (go build) or test (go test) packages.
PackagePath string
// Env is a list of environment variables to pass to the cmd;
// exec.Command.Env is set to this value.
Env []string
// Dir is the dir to run "go {cmd}" in; exec.Command.Dir is set to this value.
Dir string
}
// GoTestOptions is the set of options for "go test".
type GoTestOptions struct {
GoCmdOptions
// TestBinaryArgs are args passed to the binary compiled by "go test".
TestBinaryArgs []string
}
var validVendorCmds = map[string]struct{}{
"build": struct{}{},
"clean": struct{}{},
"get": struct{}{},
"install": struct{}{},
"list": struct{}{},
"run": struct{}{},
"test": struct{}{},
}
// GoBuild runs "go build" configured with opts.
func GoBuild(opts GoCmdOptions) error {
return GoCmd("build", opts)
}
// GoTest runs "go test" configured with opts.
func GoTest(opts GoTestOptions) error {
bargs, err := opts.getGeneralArgsWithCmd("test")
if err != nil {
return err
}
bargs = append(bargs, opts.TestBinaryArgs...)
c := exec.Command("go", bargs...)
opts.setCmdFields(c)
return ExecCmd(c)
}
// GoCmd runs "go {cmd}".
func GoCmd(cmd string, opts GoCmdOptions) error {
bargs, err := opts.getGeneralArgsWithCmd(cmd)
if err != nil {
return err
}
c := exec.Command("go", bargs...)
opts.setCmdFields(c)
return ExecCmd(c)
}
func (opts GoCmdOptions) getGeneralArgsWithCmd(cmd string) ([]string, error) {
// Go subcommands with more than one child command must be passed as
// multiple arguments instead of a spaced string, ex. "go mod init".
bargs := []string{}
for _, c := range strings.Split(cmd, " ") {
if ct := strings.TrimSpace(c); ct != "" {
bargs = append(bargs, ct)
}
}
if len(bargs) == 0 {
return nil, fmt.Errorf("the go binary cannot be run without subcommands")
}
if opts.BinName != "" {
bargs = append(bargs, "-o", opts.BinName)
}
bargs = append(bargs, opts.Args...)
if goModOn, err := GoModOn(); err != nil {
return nil, err
} else if goModOn {
// Does vendor exist?
info, err := os.Stat("vendor")
if err != nil && !os.IsNotExist(err) {
return nil, err
}
// Does the first "go" subcommand accept -mod=vendor?
_, ok := validVendorCmds[bargs[0]]
if err == nil && info.IsDir() && ok {
bargs = append(bargs, "-mod=vendor")
}
}
if opts.PackagePath != "" {
bargs = append(bargs, opts.PackagePath)
}
return bargs, nil
}
func (opts GoCmdOptions) setCmdFields(c *exec.Cmd) {
c.Env = append(c.Env, os.Environ()...)
if len(opts.Env) != 0 {
c.Env = append(c.Env, opts.Env...)
}
if opts.Dir != "" {
c.Dir = opts.Dir
}
}
// From https://github.com/golang/go/wiki/Modules:
// You can activate module support in one of two ways:
// - Invoke the go command in a directory outside of the $GOPATH/src tree,
// with a valid go.mod file in the current directory or any parent of it and
// the environment variable GO111MODULE unset (or explicitly set to auto).
// - Invoke the go command with GO111MODULE=on environment variable set.
//
// GoModOn returns true if Go modules are on in one of the above two ways.
func GoModOn() (bool, error) {
v, ok := os.LookupEnv(GoModEnv)
if v == "off" {
return false, nil
}
if v == "on" {
return true, nil
}
inSrc, err := WdInGoPathSrc()
if err != nil {
return false, err
}
return !inSrc && (!ok || v == "" || v == "auto"), nil
}
func WdInGoPathSrc() (bool, error) {
wd, err := os.Getwd()
if err != nil {
return false, err
}
goPath, ok := os.LookupEnv(GoPathEnv)
if !ok || goPath == "" {
hd, err := getHomeDir()
if err != nil {
return false, err
}
goPath = filepath.Join(hd, "go")
}
return strings.HasPrefix(wd, filepath.Join(goPath, "src")), nil
}