Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package build |
| 16 | |
| 17 | import ( |
Colin Cross | 7b97ecd | 2019-06-19 13:17:59 -0700 | [diff] [blame^] | 18 | "bufio" |
| 19 | "io" |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 20 | "os/exec" |
Colin Cross | 7b97ecd | 2019-06-19 13:17:59 -0700 | [diff] [blame^] | 21 | "strings" |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 22 | ) |
| 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 |
| 27 | type Cmd struct { |
| 28 | *exec.Cmd |
| 29 | |
| 30 | Environment *Environment |
| 31 | Sandbox Sandbox |
| 32 | |
| 33 | ctx Context |
| 34 | config Config |
| 35 | name string |
| 36 | } |
| 37 | |
| 38 | func 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 Gaston | a6697e8 | 2017-06-13 12:51:50 -0700 | [diff] [blame] | 44 | ctx: ctx, |
| 45 | config: config, |
| 46 | name: name, |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | return ret |
| 50 | } |
| 51 | |
| 52 | func (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 | |
| 63 | func (c *Cmd) Start() error { |
| 64 | c.prepare() |
| 65 | return c.Cmd.Start() |
| 66 | } |
| 67 | |
| 68 | func (c *Cmd) Run() error { |
| 69 | c.prepare() |
Jeff Gaston | 809cc6f | 2017-05-25 15:44:36 -0700 | [diff] [blame] | 70 | err := c.Cmd.Run() |
| 71 | return err |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | func (c *Cmd) Output() ([]byte, error) { |
| 75 | c.prepare() |
Jeff Gaston | 809cc6f | 2017-05-25 15:44:36 -0700 | [diff] [blame] | 76 | bytes, err := c.Cmd.Output() |
| 77 | return bytes, err |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | func (c *Cmd) CombinedOutput() ([]byte, error) { |
| 81 | c.prepare() |
Jeff Gaston | 809cc6f | 2017-05-25 15:44:36 -0700 | [diff] [blame] | 82 | bytes, err := c.Cmd.CombinedOutput() |
| 83 | return bytes, err |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | // StartOrFatal is equivalent to Start, but handles the error with a call to ctx.Fatal |
| 87 | func (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 Willemsen | db8457c | 2017-05-12 16:38:17 -0700 | [diff] [blame] | 93 | func (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 Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 104 | // RunOrFatal is equivalent to Run, but handles the error with a call to ctx.Fatal |
| 105 | func (c *Cmd) RunOrFatal() { |
Dan Willemsen | db8457c | 2017-05-12 16:38:17 -0700 | [diff] [blame] | 106 | c.reportError(c.Run()) |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | // WaitOrFatal is equivalent to Wait, but handles the error with a call to ctx.Fatal |
| 110 | func (c *Cmd) WaitOrFatal() { |
Dan Willemsen | db8457c | 2017-05-12 16:38:17 -0700 | [diff] [blame] | 111 | c.reportError(c.Wait()) |
| 112 | } |
| 113 | |
| 114 | // OutputOrFatal is equivalent to Output, but handles the error with a call to ctx.Fatal |
| 115 | func (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 |
| 123 | func (c *Cmd) CombinedOutputOrFatal() []byte { |
| 124 | ret, err := c.CombinedOutput() |
| 125 | c.reportError(err) |
| 126 | return ret |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 127 | } |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 128 | |
| 129 | // RunAndPrintOrFatal will run the command, then after finishing |
| 130 | // print any output, then handling any errors with a call to |
| 131 | // ctx.Fatal |
| 132 | func (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 Cross | 7b97ecd | 2019-06-19 13:17:59 -0700 | [diff] [blame^] | 145 | |
| 146 | // RunAndStreamOrFatal will run the command, while running print |
| 147 | // any output, then handle any errors with a call to ctx.Fatal |
| 148 | func (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 | } |