-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexec_tools.go
84 lines (65 loc) · 1.68 KB
/
exec_tools.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
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
package mttools
import (
"fmt"
"io"
"log"
"os"
"os/exec"
"runtime"
"strings"
)
// Executes cmd and returns its output or err
func ExecCmd(cmd_path string, args []string) (string, error) {
cmd := exec.Command(cmd_path, args...)
buffer, err := cmd.CombinedOutput()
return string(buffer), err
}
// Executes cmd and prints its output
func ExecCmdPrint(cmd_path string, args []string) error {
out, err := ExecCmd(cmd_path, args)
fmt.Print(out)
return err
}
// Executes cmd printing its stdout+stderr to current stdout during execution and returns its output after
func ExecCmdWaitAndPrint(cmd_path string, args []string) (output string, err error) {
cmd := exec.Command(cmd_path, args...)
//fmt.Println("CMD: " + cmd.String())
var sb strings.Builder
mw := io.MultiWriter(&sb, os.Stdout)
cmd.Stdout = mw
cmd.Stderr = mw
if err = cmd.Start(); err != nil {
return "", err
}
err = cmd.Wait()
output = sb.String()
return output, err
}
// Executes whole string in command-line shell. Returns its output and/or error.
func ExecCommandLine(command_line string) (string, error) {
var cmd_path string
var args []string
if IsWindows() {
cmd_path = "cmd.exe"
args = []string{"/C", command_line}
} else if IsLinux() {
cmd_path = "sh"
args = []string{"-c", command_line}
} else {
log.Panicln("Unknown platform in ExecCommandLine()")
}
return ExecCmd(cmd_path, args)
}
// Hides console (only under Windows)
//
// Thanks to SyncThing!
// https://github.com/syncthing/syncthing/blob/main/lib/osutil/hidden_windows.go
func HideConsole() {
hideConsoleHelper()
}
func IsWindows() bool {
return runtime.GOOS == "windows"
}
func IsLinux() bool {
return runtime.GOOS == "linux"
}