-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexec.go
More file actions
41 lines (35 loc) · 788 Bytes
/
Copy pathexec.go
File metadata and controls
41 lines (35 loc) · 788 Bytes
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
package binary
import (
"context"
"fmt"
"os/exec"
"strings"
)
func (b *Binary) Env() []string {
env := []string{}
for k, v := range b.Envs {
env = append(env, fmt.Sprintf("%s=%s", k, v))
}
return env
}
func (b *Binary) Cmd(args ...string) *exec.Cmd {
if !b.BinaryExists() {
return nil
}
if b.Context == nil {
b.Context = context.Background()
}
cmd := exec.CommandContext(b.Context, b.BinaryPath(), args...)
// alsways set the environment
// if env is nil, it will use the user environment
cmd.Env = b.Env()
return cmd
}
func (b *Binary) Exec(args ...string) (string, error) {
cmd := b.Cmd(args...)
if cmd == nil {
return "", fmt.Errorf("binary %s does not exist", b.Name)
}
out, err := cmd.CombinedOutput()
return strings.TrimSpace(string(out)), err
}