blob: 5c312bcd91f9b2afd2d2538c99ced2884c7b185b [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
33}
34
35func Command(ctx Context, config Config, name string, executable string, args ...string) *Cmd {
36 ret := &Cmd{
37 Cmd: exec.CommandContext(ctx.Context, executable, args...),
38 Environment: config.Environment().Copy(),
39 Sandbox: noSandbox,
40
Jeff Gastona6697e82017-06-13 12:51:50 -070041 ctx: ctx,
42 config: config,
43 name: name,
Dan Willemsen269a8c72017-05-03 17:15:47 -070044 }
45
46 return ret
47}
48
49func (c *Cmd) prepare() {
50 if c.Env == nil {
51 c.Env = c.Environment.Environ()
52 }
53 if c.sandboxSupported() {
54 c.wrapSandbox()
55 }
56
57 c.ctx.Verboseln(c.Path, c.Args)
58}
59
60func (c *Cmd) Start() error {
61 c.prepare()
62 return c.Cmd.Start()
63}
64
65func (c *Cmd) Run() error {
66 c.prepare()
Jeff Gaston809cc6f2017-05-25 15:44:36 -070067 err := c.Cmd.Run()
68 return err
Dan Willemsen269a8c72017-05-03 17:15:47 -070069}
70
71func (c *Cmd) Output() ([]byte, error) {
72 c.prepare()
Jeff Gaston809cc6f2017-05-25 15:44:36 -070073 bytes, err := c.Cmd.Output()
74 return bytes, err
Dan Willemsen269a8c72017-05-03 17:15:47 -070075}
76
77func (c *Cmd) CombinedOutput() ([]byte, error) {
78 c.prepare()
Jeff Gaston809cc6f2017-05-25 15:44:36 -070079 bytes, err := c.Cmd.CombinedOutput()
80 return bytes, err
Dan Willemsen269a8c72017-05-03 17:15:47 -070081}
82
83// StartOrFatal is equivalent to Start, but handles the error with a call to ctx.Fatal
84func (c *Cmd) StartOrFatal() {
85 if err := c.Start(); err != nil {
86 c.ctx.Fatalf("Failed to run %s: %v", c.name, err)
87 }
88}
89
Dan Willemsendb8457c2017-05-12 16:38:17 -070090func (c *Cmd) reportError(err error) {
91 if err == nil {
92 return
93 }
94 if e, ok := err.(*exec.ExitError); ok {
95 c.ctx.Fatalf("%s failed with: %v", c.name, e.ProcessState.String())
96 } else {
97 c.ctx.Fatalf("Failed to run %s: %v", c.name, err)
98 }
99}
100
Dan Willemsen269a8c72017-05-03 17:15:47 -0700101// RunOrFatal is equivalent to Run, but handles the error with a call to ctx.Fatal
102func (c *Cmd) RunOrFatal() {
Dan Willemsendb8457c2017-05-12 16:38:17 -0700103 c.reportError(c.Run())
Dan Willemsen269a8c72017-05-03 17:15:47 -0700104}
105
106// WaitOrFatal is equivalent to Wait, but handles the error with a call to ctx.Fatal
107func (c *Cmd) WaitOrFatal() {
Dan Willemsendb8457c2017-05-12 16:38:17 -0700108 c.reportError(c.Wait())
109}
110
111// OutputOrFatal is equivalent to Output, but handles the error with a call to ctx.Fatal
112func (c *Cmd) OutputOrFatal() []byte {
113 ret, err := c.Output()
114 c.reportError(err)
115 return ret
116}
117
118// CombinedOutputOrFatal is equivalent to CombinedOutput, but handles the error with
119// a call to ctx.Fatal
120func (c *Cmd) CombinedOutputOrFatal() []byte {
121 ret, err := c.CombinedOutput()
122 c.reportError(err)
123 return ret
Dan Willemsen269a8c72017-05-03 17:15:47 -0700124}
Dan Willemsenb82471a2018-05-17 16:37:09 -0700125
126// RunAndPrintOrFatal will run the command, then after finishing
127// print any output, then handling any errors with a call to
128// ctx.Fatal
129func (c *Cmd) RunAndPrintOrFatal() {
130 ret, err := c.CombinedOutput()
131 st := c.ctx.Status.StartTool()
132 if len(ret) > 0 {
133 if err != nil {
134 st.Error(string(ret))
135 } else {
136 st.Print(string(ret))
137 }
138 }
139 st.Finish()
140 c.reportError(err)
141}