/
proc.go
57 lines (47 loc) 路 1.3 KB
/
proc.go
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
package utils
import (
"os"
"os/exec"
)
// Exec executes a command in the specified directory.
//
// It takes the following parameters:
// - dir: the directory in which the command will be executed.
// - name: the name of the command to be executed.
// - arg: optional arguments to be passed to the command.
//
// It returns an error if the command execution fails.
func Exec(dir, name string, arg ...string) error {
cmd := exec.Command(name, arg...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if dir != "" {
cmd.Dir = dir
}
err := cmd.Run()
if err != nil {
return err
}
return nil
}
// ExecOutput executes a command with the given arguments and returns its output as a string.
//
// It takes the following parameters:
// - dir: the directory in which the command should be executed.
// - name: the name of the command to be executed.
// - arg: a variadic parameter representing the arguments to be passed to the command.
//
// It returns a string representing the output of the command and an error if any occurred.
func ExecOutput(dir, name string, arg ...string) (string, error) {
cmd := exec.Command(name, arg...)
cmd.Stderr = os.Stderr
if dir != "" {
cmd.Dir = dir
}
outputByte, err := cmd.Output()
if err != nil {
return "", err
}
output := string(outputByte)
return output, err
}