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 ( |
| 18 | "os/exec" |
| 19 | ) |
| 20 | |
| 21 | // Cmd is a wrapper of os/exec.Cmd that integrates with the build context for |
| 22 | // logging, the config's Environment for simpler environment modification, and |
| 23 | // implements hooks for sandboxing |
| 24 | type Cmd struct { |
| 25 | *exec.Cmd |
| 26 | |
| 27 | Environment *Environment |
| 28 | Sandbox Sandbox |
| 29 | |
| 30 | ctx Context |
| 31 | config Config |
| 32 | name string |
Jeff Gaston | 809cc6f | 2017-05-25 15:44:36 -0700 | [diff] [blame^] | 33 | |
| 34 | // doneChannel closes to signal the command's termination |
| 35 | doneChannel chan bool |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 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 | 809cc6f | 2017-05-25 15:44:36 -0700 | [diff] [blame^] | 44 | ctx: ctx, |
| 45 | config: config, |
| 46 | name: name, |
| 47 | doneChannel: make(chan bool), |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | return ret |
| 51 | } |
| 52 | |
| 53 | func (c *Cmd) prepare() { |
| 54 | if c.Env == nil { |
| 55 | c.Env = c.Environment.Environ() |
| 56 | } |
| 57 | if c.sandboxSupported() { |
| 58 | c.wrapSandbox() |
| 59 | } |
| 60 | |
| 61 | c.ctx.Verboseln(c.Path, c.Args) |
| 62 | } |
| 63 | |
Jeff Gaston | 809cc6f | 2017-05-25 15:44:36 -0700 | [diff] [blame^] | 64 | func (c *Cmd) teardown() { |
| 65 | close(c.doneChannel) |
| 66 | } |
| 67 | |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 68 | func (c *Cmd) Start() error { |
| 69 | c.prepare() |
| 70 | return c.Cmd.Start() |
| 71 | } |
| 72 | |
| 73 | func (c *Cmd) Run() error { |
| 74 | c.prepare() |
Jeff Gaston | 809cc6f | 2017-05-25 15:44:36 -0700 | [diff] [blame^] | 75 | defer c.teardown() |
| 76 | err := c.Cmd.Run() |
| 77 | return err |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | func (c *Cmd) Output() ([]byte, error) { |
| 81 | c.prepare() |
Jeff Gaston | 809cc6f | 2017-05-25 15:44:36 -0700 | [diff] [blame^] | 82 | defer c.teardown() |
| 83 | bytes, err := c.Cmd.Output() |
| 84 | return bytes, err |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | func (c *Cmd) CombinedOutput() ([]byte, error) { |
| 88 | c.prepare() |
Jeff Gaston | 809cc6f | 2017-05-25 15:44:36 -0700 | [diff] [blame^] | 89 | defer c.teardown() |
| 90 | bytes, err := c.Cmd.CombinedOutput() |
| 91 | return bytes, err |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | // StartOrFatal is equivalent to Start, but handles the error with a call to ctx.Fatal |
| 95 | func (c *Cmd) StartOrFatal() { |
| 96 | if err := c.Start(); err != nil { |
| 97 | c.ctx.Fatalf("Failed to run %s: %v", c.name, err) |
| 98 | } |
| 99 | } |
| 100 | |
Dan Willemsen | db8457c | 2017-05-12 16:38:17 -0700 | [diff] [blame] | 101 | func (c *Cmd) reportError(err error) { |
| 102 | if err == nil { |
| 103 | return |
| 104 | } |
| 105 | if e, ok := err.(*exec.ExitError); ok { |
| 106 | c.ctx.Fatalf("%s failed with: %v", c.name, e.ProcessState.String()) |
| 107 | } else { |
| 108 | c.ctx.Fatalf("Failed to run %s: %v", c.name, err) |
| 109 | } |
| 110 | } |
| 111 | |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 112 | // RunOrFatal is equivalent to Run, but handles the error with a call to ctx.Fatal |
| 113 | func (c *Cmd) RunOrFatal() { |
Dan Willemsen | db8457c | 2017-05-12 16:38:17 -0700 | [diff] [blame] | 114 | c.reportError(c.Run()) |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | // WaitOrFatal is equivalent to Wait, but handles the error with a call to ctx.Fatal |
| 118 | func (c *Cmd) WaitOrFatal() { |
Dan Willemsen | db8457c | 2017-05-12 16:38:17 -0700 | [diff] [blame] | 119 | c.reportError(c.Wait()) |
| 120 | } |
| 121 | |
| 122 | // OutputOrFatal is equivalent to Output, but handles the error with a call to ctx.Fatal |
| 123 | func (c *Cmd) OutputOrFatal() []byte { |
| 124 | ret, err := c.Output() |
| 125 | c.reportError(err) |
| 126 | return ret |
| 127 | } |
| 128 | |
| 129 | // CombinedOutputOrFatal is equivalent to CombinedOutput, but handles the error with |
| 130 | // a call to ctx.Fatal |
| 131 | func (c *Cmd) CombinedOutputOrFatal() []byte { |
| 132 | ret, err := c.CombinedOutput() |
| 133 | c.reportError(err) |
| 134 | return ret |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 135 | } |
Jeff Gaston | 809cc6f | 2017-05-25 15:44:36 -0700 | [diff] [blame^] | 136 | |
| 137 | // Done() tells whether this command has finished executing |
| 138 | func (c *Cmd) Done() bool { |
| 139 | select { |
| 140 | case <-c.doneChannel: |
| 141 | return true |
| 142 | default: |
| 143 | return false |
| 144 | } |
| 145 | } |