blob: e435c53be4e3e86aff4a13589fd86e08d5cdf9b6 [file] [log] [blame]
Dan Willemsen269a8c72017-05-03 17:15:47 -07001// Copyright 2017 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package build
16
17import (
Colin Cross7b97ecd2019-06-19 13:17:59 -070018 "bufio"
19 "io"
Dan Willemsen269a8c72017-05-03 17:15:47 -070020 "os/exec"
Colin Cross7b97ecd2019-06-19 13:17:59 -070021 "strings"
Dan Willemsen269a8c72017-05-03 17:15:47 -070022)
23
24// Cmd is a wrapper of os/exec.Cmd that integrates with the build context for
25// logging, the config's Environment for simpler environment modification, and
26// implements hooks for sandboxing
27type Cmd struct {
28 *exec.Cmd
29
30 Environment *Environment
31 Sandbox Sandbox
32
33 ctx Context
34 config Config
35 name string
36}
37
38func Command(ctx Context, config Config, name string, executable string, args ...string) *Cmd {
39 ret := &Cmd{
40 Cmd: exec.CommandContext(ctx.Context, executable, args...),
41 Environment: config.Environment().Copy(),
42 Sandbox: noSandbox,
43
Jeff Gastona6697e82017-06-13 12:51:50 -070044 ctx: ctx,
45 config: config,
46 name: name,
Dan Willemsen269a8c72017-05-03 17:15:47 -070047 }
48
49 return ret
50}
51
52func (c *Cmd) prepare() {
53 if c.Env == nil {
54 c.Env = c.Environment.Environ()
55 }
56 if c.sandboxSupported() {
57 c.wrapSandbox()
58 }
59
60 c.ctx.Verboseln(c.Path, c.Args)
61}
62
63func (c *Cmd) Start() error {
64 c.prepare()
65 return c.Cmd.Start()
66}
67
68func (c *Cmd) Run() error {
69 c.prepare()
Jeff Gaston809cc6f2017-05-25 15:44:36 -070070 err := c.Cmd.Run()
71 return err
Dan Willemsen269a8c72017-05-03 17:15:47 -070072}
73
74func (c *Cmd) Output() ([]byte, error) {
75 c.prepare()
Jeff Gaston809cc6f2017-05-25 15:44:36 -070076 bytes, err := c.Cmd.Output()
77 return bytes, err
Dan Willemsen269a8c72017-05-03 17:15:47 -070078}
79
80func (c *Cmd) CombinedOutput() ([]byte, error) {
81 c.prepare()
Jeff Gaston809cc6f2017-05-25 15:44:36 -070082 bytes, err := c.Cmd.CombinedOutput()
83 return bytes, err
Dan Willemsen269a8c72017-05-03 17:15:47 -070084}
85
86// StartOrFatal is equivalent to Start, but handles the error with a call to ctx.Fatal
87func (c *Cmd) StartOrFatal() {
88 if err := c.Start(); err != nil {
89 c.ctx.Fatalf("Failed to run %s: %v", c.name, err)
90 }
91}
92
Dan Willemsendb8457c2017-05-12 16:38:17 -070093func (c *Cmd) reportError(err error) {
94 if err == nil {
95 return
96 }
97 if e, ok := err.(*exec.ExitError); ok {
98 c.ctx.Fatalf("%s failed with: %v", c.name, e.ProcessState.String())
99 } else {
100 c.ctx.Fatalf("Failed to run %s: %v", c.name, err)
101 }
102}
103
Dan Willemsen269a8c72017-05-03 17:15:47 -0700104// RunOrFatal is equivalent to Run, but handles the error with a call to ctx.Fatal
105func (c *Cmd) RunOrFatal() {
Dan Willemsendb8457c2017-05-12 16:38:17 -0700106 c.reportError(c.Run())
Dan Willemsen269a8c72017-05-03 17:15:47 -0700107}
108
109// WaitOrFatal is equivalent to Wait, but handles the error with a call to ctx.Fatal
110func (c *Cmd) WaitOrFatal() {
Dan Willemsendb8457c2017-05-12 16:38:17 -0700111 c.reportError(c.Wait())
112}
113
114// OutputOrFatal is equivalent to Output, but handles the error with a call to ctx.Fatal
115func (c *Cmd) OutputOrFatal() []byte {
116 ret, err := c.Output()
117 c.reportError(err)
118 return ret
119}
120
121// CombinedOutputOrFatal is equivalent to CombinedOutput, but handles the error with
122// a call to ctx.Fatal
123func (c *Cmd) CombinedOutputOrFatal() []byte {
124 ret, err := c.CombinedOutput()
125 c.reportError(err)
126 return ret
Dan Willemsen269a8c72017-05-03 17:15:47 -0700127}
Dan Willemsenb82471a2018-05-17 16:37:09 -0700128
129// RunAndPrintOrFatal will run the command, then after finishing
130// print any output, then handling any errors with a call to
131// ctx.Fatal
132func (c *Cmd) RunAndPrintOrFatal() {
133 ret, err := c.CombinedOutput()
134 st := c.ctx.Status.StartTool()
135 if len(ret) > 0 {
136 if err != nil {
137 st.Error(string(ret))
138 } else {
139 st.Print(string(ret))
140 }
141 }
142 st.Finish()
143 c.reportError(err)
144}
Colin Cross7b97ecd2019-06-19 13:17:59 -0700145
146// RunAndStreamOrFatal will run the command, while running print
147// any output, then handle any errors with a call to ctx.Fatal
148func (c *Cmd) RunAndStreamOrFatal() {
149 out, err := c.StdoutPipe()
150 if err != nil {
151 c.ctx.Fatal(err)
152 }
153 c.Stderr = c.Stdout
154
155 st := c.ctx.Status.StartTool()
156
157 c.StartOrFatal()
158
159 buf := bufio.NewReaderSize(out, 2*1024*1024)
160 for {
161 // Attempt to read whole lines, but write partial lines that are too long to fit in the buffer or hit EOF
162 line, err := buf.ReadString('\n')
163 if line != "" {
164 st.Print(strings.TrimSuffix(line, "\n"))
165 } else if err == io.EOF {
166 break
167 } else if err != nil {
168 c.ctx.Fatal(err)
169 }
170 }
171
172 err = c.Wait()
173 st.Finish()
174 c.reportError(err)
175}