forked from abdfnx/gosh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshello.go
193 lines (168 loc) · 6.92 KB
/
shello.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// This file was modified from https://github.com/abdfnx/gosh/blob/trunk/gosh.go. The methods signatures are NOT compatible with the original.
package shello
import (
"bytes"
"fmt"
"log"
"os/exec"
"runtime"
"strings"
)
// ShellOutput function executes the provided shell command and returns the standard output, standard error, and any error encountered.
// Parameters:
// - command string: The shell command to be executed.
// Return values:
// - string: The standard output of the executed command.
// - string: The standard error of the executed command.
// - error: If an error occurs during execution, the corresponding error message is returned.
func ShellOutput(command string) (string, string, error) {
return ShellOutputWithDir(command, "")
}
// ShellOutputWithDir executes a shell command in a specified directory and returns the standard output, standard error, and any error encountered.
// Parameters:
// - command string: The shell command to be executed.
// - dir string: The directory in which the command should be executed.
// Return values:
// - string: The standard output of the executed command.
// - string: The standard error of the executed command.
// - error: If an error occurs during execution, the corresponding error message is returned.
func ShellOutputWithDir(command, dir string) (string, string, error) {
return Exec("bash", command, dir)
}
// ShellCommand executes the given shell command and prints the output.
// Parameters:
// - command string: The shell command to be executed.
// return value:
// - None. The function does not return any value.
func ShellCommand(command string) {
out, errout, err := ShellOutput(command)
if err != nil {
log.Printf("error: %v\n", err)
fmt.Print(errout)
}
fmt.Print(out)
}
// PowershellOutput is a function that executes a PowerShell command and returns its output.
// It takes a string 'command' as input which represents the PowerShell command to be executed.
// The function returns two strings: the first string represents the standard output of the command,
// the second string represents the standard error output of the command,
// and an error object which contains any error information that occurred during execution.
func PowershellOutput(command string) (string, string, error) {
return PowershellOutputWithDir(command, "")
}
// PowershellOutputWithDir function executes a PowerShell command within a specified directory and returns the output.
// Parameters:
// - command string: The PowerShell command to be executed.
// - dir string: The directory in which the command will be executed.
// Return values:
// - string: The standard output of the executed command.
// - string: The standard error output of the executed command.
// - error: If an error occurs during the execution, the corresponding error message is returned.
func PowershellOutputWithDir(command, dir string) (string, string, error) {
return Exec("powershell.exe", command, dir)
}
// PowershellCommand executes a given Powershell command and prints the output.
// Parameters:
// - command string: The Powershell command to be executed.
// return value:
// - None. The function does not return any value.
func PowershellCommand(command string) {
out, errout, err := PowershellOutput(command)
if err != nil {
log.Printf("error: %v\n", err)
fmt.Print(errout)
}
fmt.Print(out)
}
// Exec function executes a shell command and returns the standard output, standard error, and any error that occurred.
// Parameters:
// - shell string: The shell to be used for executing the command.
// - command string: The command to be executed.
// - dir string: The directory in which the command will be executed. If empty, the current directory will be used.
// Return values:
// - string: The standard output of the executed command.
// - string: The standard error of the executed command.
// - error: If an error occurs during the execution of the command, the corresponding error message is returned.
func Exec(shell, command, dir string) (string, string, error) {
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd := exec.Command(shell, "-c", command)
if dir != "" {
cmd.Dir = dir
}
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
return outputString(&stdout), outputString(&stderr), err
}
// Run function executes the given command.
// Parameters:
// - cmd string: The command to be executed.
func Run(cmd string) {
RunWithDir(cmd, "")
}
// RunWithDir runs a command in a specified directory. This function determines the operating system and calls the appropriate function to execute the command.
// If an error occurs during execution, the error message is logged and the error output is printed.
// Parameters:
// - cmd string: The command to be executed.
// - dir string: The directory in which the command will be executed.
// This function determines the operating system and calls the appropriate function to execute the command.
// If an error occurs during execution, the error message is logged and the error output is printed.
// Finally, the command output is printed.
func RunWithDir(cmd, dir string) {
var (
out string
errout string
err error
)
if runtime.GOOS == "windows" {
out, errout, err = PowershellOutputWithDir(cmd, dir)
} else {
out, errout, err = ShellOutputWithDir(cmd, dir)
}
if err != nil {
log.Printf("error: %v\n", err)
fmt.Print(errout)
}
fmt.Print(out)
}
// RunOutput runs the given command and returns its standard output, standard error, and any error encountered.
// If the command is successful, the error will be nil.
// Parameters:
// - command string: The command to be executed.
// Return values:
// - string: The standard output of the command.
// - string: The standard error of the command.
// - error: If an error occurs, the corresponding error message is returned.
func RunOutput(command string) (string, string, error) {
return RunOutputWithDir(command, "")
}
// RunOutputWithDir runs a command in a specified directory and returns its standard output, standard error, and any error encountered.
// Parameters:
// - command string: The command to be executed.
// - dir string: The directory in which the command should be executed. If empty, the current directory is used.
// Return values:
// - string: The standard output of the command.
// - string: The standard error of the command.
// - error: If an error occurs, the corresponding error message is returned.
func RunOutputWithDir(command, dir string) (string, string, error) {
if runtime.GOOS == "windows" {
return PowershellOutputWithDir(command, dir)
} else {
return ShellOutputWithDir(command, dir)
}
}
// outputString returns the string representation of a bytes.Buffer.
// If the buffer is nil, an empty string is returned.
// The output is trimmed if `TrimOutput` is set to true.
func outputString(buf *bytes.Buffer) string {
if buf == nil {
return ""
}
if TrimOutput {
return strings.TrimSpace(buf.String())
}
return buf.String()
}
// `TrimOutput` is a flag to trim the output of commands
var TrimOutput = true