-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec.go
More file actions
61 lines (48 loc) · 1.24 KB
/
Copy pathexec.go
File metadata and controls
61 lines (48 loc) · 1.24 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
/*
* Copyright (c) The Kowabunga Project
* Apache License, Version 2.0 (see LICENSE or https://www.apache.org/licenses/LICENSE-2.0.txt)
* SPDX-License-Identifier: Apache-2.0
*/
package common
import (
"io"
"os"
"os/exec"
"strings"
"github.com/kowabunga-cloud/common/klog"
)
func LookupBinary(bin string) (string, error) {
path, err := exec.LookPath(bin)
if err != nil {
klog.Errorf("%s executable can't be found in $PATH", bin)
return "", err
}
return path, nil
}
func binExec(bin, dir string, args, envs []string, stdout io.Writer) error {
var cmd exec.Cmd
binArgs := append([]string{bin}, args...)
binEnvs := append(os.Environ(), envs...)
cmd.Path = bin
if dir != "" {
cmd.Dir = dir
}
cmd.Args = binArgs
cmd.Env = binEnvs
cmd.Stdout = stdout
cmd.Stdin = os.Stdin
cmd.Stderr = stdout
klog.Debugf("Running %s %s", strings.Join(envs[:], " "), strings.Join(cmd.Args[:], " "))
if err := cmd.Run(); err != nil {
return err
}
return nil
}
func BinExec(bin, dir string, args, envs []string) error {
return binExec(bin, dir, args, envs, os.Stdout)
}
func BinExecOut(bin, dir string, args, envs []string) (string, error) {
var outbuf strings.Builder
err := binExec(bin, dir, args, envs, &outbuf)
return outbuf.String(), err
}