-
Notifications
You must be signed in to change notification settings - Fork 272
Expand file tree
/
Copy pathrun.go
More file actions
119 lines (108 loc) · 2.96 KB
/
run.go
File metadata and controls
119 lines (108 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package internal
import (
"bytes"
"fmt"
"log"
"os"
"os/exec"
"runtime"
"strings"
// DEPRECATED: The ioutil package was deprecated in Go 1.16.
// TODO: Replace ioutil.Discard with io.Discard when minimum Go version
// is raised to 1.16+. See: https://go.dev/doc/go1.16#ioutil
"io/ioutil"
)
var debug *log.Logger = log.New(ioutil.Discard, "", 0)
func SetDebug(l *log.Logger) {
debug = l
}
func RunDebug(cmd string, args ...string) error {
env, err := EnvWithCurrentGOOS()
if err != nil {
return err
}
buf := &bytes.Buffer{}
errbuf := &bytes.Buffer{}
debug.Println("running", cmd, strings.Join(args, " "))
c := exec.Command(cmd, args...)
c.Env = env
c.Stderr = errbuf
c.Stdout = buf
if err := c.Run(); err != nil {
debug.Print("error running '", cmd, strings.Join(args, " "), "': ", err, ": ", errbuf)
return err
}
debug.Println(buf)
return nil
}
func OutputDebug(cmd string, args ...string) (string, error) {
env, err := EnvWithCurrentGOOS()
if err != nil {
return "", err
}
buf := &bytes.Buffer{}
errbuf := &bytes.Buffer{}
debug.Println("running", cmd, strings.Join(args, " "))
c := exec.Command(cmd, args...)
c.Env = env
c.Stderr = errbuf
c.Stdout = buf
if err := c.Run(); err != nil {
errMsg := strings.TrimSpace(errbuf.String())
debug.Print("error running '", cmd, strings.Join(args, " "), "': ", err, ": ", errMsg)
return "", fmt.Errorf("error running \"%s %s\": %s\n%s", cmd, strings.Join(args, " "), err, errMsg)
}
return strings.TrimSpace(buf.String()), nil
}
// SplitEnv takes the results from os.Environ() (a []string of foo=bar values)
// and makes a map[string]string out of it.
func SplitEnv(env []string) (map[string]string, error) {
out := map[string]string{}
for _, s := range env {
parts := strings.SplitN(s, "=", 2)
if len(parts) != 2 {
return nil, fmt.Errorf("badly formatted environment variable: %v", s)
}
out[parts[0]] = parts[1]
}
return out, nil
}
// joinEnv converts the given map into a list of foo=bar environment variables,
// such as that outputted by os.Environ().
func joinEnv(env map[string]string) []string {
vals := make([]string, 0, len(env))
for k, v := range env {
vals = append(vals, k+"="+v)
}
return vals
}
// EnvWithCurrentGOOS returns a copy of os.Environ with the GOOS and GOARCH set
// to runtime.GOOS and runtime.GOARCH.
func EnvWithCurrentGOOS() ([]string, error) {
vals, err := SplitEnv(os.Environ())
if err != nil {
return nil, err
}
vals["GOOS"] = runtime.GOOS
vals["GOARCH"] = runtime.GOARCH
return joinEnv(vals), nil
}
// EnvWithGOOS retuns the os.Environ() values with GOOS and/or GOARCH either set
// to their runtime value, or the given value if non-empty.
func EnvWithGOOS(goos, goarch string) ([]string, error) {
env, err := SplitEnv(os.Environ())
if err != nil {
return nil, err
}
if goos == "" {
env["GOOS"] = runtime.GOOS
} else {
env["GOOS"] = goos
}
if goarch == "" {
env["GOARCH"] = runtime.GOARCH
} else {
env["GOARCH"] = goarch
}
return joinEnv(env), nil
}