blob: 79310dc200ba97e40314335207dfcf04fd25c756 [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 (
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
24type Cmd struct {
25 *exec.Cmd
26
27 Environment *Environment
28 Sandbox Sandbox
29
30 ctx Context
31 config Config
32 name string
Jeff Gaston809cc6f2017-05-25 15:44:36 -070033
34 // doneChannel closes to signal the command's termination
35 doneChannel chan bool
Dan Willemsen269a8c72017-05-03 17:15:47 -070036}
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 Gaston809cc6f2017-05-25 15:44:36 -070044 ctx: ctx,
45 config: config,
46 name: name,
47 doneChannel: make(chan bool),
Dan Willemsen269a8c72017-05-03 17:15:47 -070048 }
49
50 return ret
51}
52
53func (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 Gaston809cc6f2017-05-25 15:44:36 -070064func (c *Cmd) teardown() {
65 close(c.doneChannel)
66}
67
Dan Willemsen269a8c72017-05-03 17:15:47 -070068func (c *Cmd) Start() error {
69 c.prepare()
70 return c.Cmd.Start()
71}
72
73func (c *Cmd) Run() error {
74 c.prepare()
Jeff Gaston809cc6f2017-05-25 15:44:36 -070075 defer c.teardown()
76 err := c.Cmd.Run()
77 return err
Dan Willemsen269a8c72017-05-03 17:15:47 -070078}
79
80func (c *Cmd) Output() ([]byte, error) {
81 c.prepare()
Jeff Gaston809cc6f2017-05-25 15:44:36 -070082 defer c.teardown()
83 bytes, err := c.Cmd.Output()
84 return bytes, err
Dan Willemsen269a8c72017-05-03 17:15:47 -070085}
86
87func (c *Cmd) CombinedOutput() ([]byte, error) {
88 c.prepare()
Jeff Gaston809cc6f2017-05-25 15:44:36 -070089 defer c.teardown()
90 bytes, err := c.Cmd.CombinedOutput()
91 return bytes, err
Dan Willemsen269a8c72017-05-03 17:15:47 -070092}
93
94// StartOrFatal is equivalent to Start, but handles the error with a call to ctx.Fatal
95func (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 Willemsendb8457c2017-05-12 16:38:17 -0700101func (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 Willemsen269a8c72017-05-03 17:15:47 -0700112// RunOrFatal is equivalent to Run, but handles the error with a call to ctx.Fatal
113func (c *Cmd) RunOrFatal() {
Dan Willemsendb8457c2017-05-12 16:38:17 -0700114 c.reportError(c.Run())
Dan Willemsen269a8c72017-05-03 17:15:47 -0700115}
116
117// WaitOrFatal is equivalent to Wait, but handles the error with a call to ctx.Fatal
118func (c *Cmd) WaitOrFatal() {
Dan Willemsendb8457c2017-05-12 16:38:17 -0700119 c.reportError(c.Wait())
120}
121
122// OutputOrFatal is equivalent to Output, but handles the error with a call to ctx.Fatal
123func (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
131func (c *Cmd) CombinedOutputOrFatal() []byte {
132 ret, err := c.CombinedOutput()
133 c.reportError(err)
134 return ret
Dan Willemsen269a8c72017-05-03 17:15:47 -0700135}
Jeff Gaston809cc6f2017-05-25 15:44:36 -0700136
137// Done() tells whether this command has finished executing
138func (c *Cmd) Done() bool {
139 select {
140 case <-c.doneChannel:
141 return true
142 default:
143 return false
144 }
145}