xref: /aosp_15_r20/external/bazelbuild-rules_go/go/tools/releaser/run.go (revision 9bb1b549b6a84214c53be0924760be030e66b93a)
1// Copyright 2021 The Bazel Authors. 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 main
16
17import (
18	"bytes"
19	"context"
20	"fmt"
21	"os"
22	"os/exec"
23	"strings"
24)
25
26// runForError runs a command without showing its output. If the command fails,
27// runForError returns an error containing its stderr.
28func runForError(ctx context.Context, dir string, name string, args ...string) error {
29	stderr := &bytes.Buffer{}
30	cmd := exec.CommandContext(ctx, name, args...)
31	cmd.Env = envWithoutBazel()
32	cmd.Dir = dir
33	cmd.Stdout = nil
34	cmd.Stderr = stderr
35	err := cmd.Run()
36	return cleanCmdError(err, name, args, stderr.Bytes())
37}
38
39// runForOutput runs a command and returns its output. If the command fails,
40// runForOutput returns an error containing its stderr. The command's output
41// is returned whether it failed or not.
42func runForOutput(ctx context.Context, dir string, name string, args ...string) ([]byte, error) {
43	stdout := &bytes.Buffer{}
44	stderr := &bytes.Buffer{}
45	cmd := exec.CommandContext(ctx, name, args...)
46	cmd.Env = envWithoutBazel()
47	cmd.Dir = dir
48	cmd.Stdout = stdout
49	cmd.Stderr = stderr
50	err := cmd.Run()
51	return stdout.Bytes(), cleanCmdError(err, name, args, stderr.Bytes())
52}
53
54// envWithoutBazel runs the current process's environment without variables
55// starting with "BUILD_" added by 'bazel run'. These can confuse subprocesses.
56func envWithoutBazel() []string {
57	env := os.Environ()
58	filtered := make([]string, 0, len(env))
59	for _, e := range env {
60		if strings.HasPrefix(e, "BUILD_") {
61			continue
62		}
63		filtered = append(filtered, e)
64	}
65	return filtered
66}
67
68// cleanCmdError wraps an error returned by exec.Cmd.Run with the command that
69// was run and its stderr output.
70func cleanCmdError(err error, name string, args []string, stderr []byte) error {
71	if err == nil {
72		return nil
73	}
74	return &commandError{
75		argv: append([]string{name}, args...),
76		err:  err,
77	}
78}
79
80type commandError struct {
81	argv   []string
82	stderr []byte
83	err    error
84}
85
86func (e *commandError) Error() string {
87	return fmt.Sprintf("running %s: %v\n%s", strings.Join(e.argv, " "), e.err, bytes.TrimSpace(e.stderr))
88}
89
90func (e *commandError) Unwrap() error {
91	return e.err
92}
93