愉快的使用cursor
This commit is contained in:
@@ -9,6 +9,65 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// SingleLineCommandExecutor 执行单行shell命令,返回执行结果和输出内容
|
||||
// singleLineCommand: 命令及参数,如 []string{"ls", "-l"}
|
||||
// 返回值:
|
||||
//
|
||||
// bool - 命令是否执行成功(true为成功,false为失败)
|
||||
// []string - 合并后的标准输出和标准错误内容(按行分割)
|
||||
func SingleLineCommandExecutor(singleLineCommand []string) (ok bool, resultLog []string) {
|
||||
|
||||
log.Info("[Excutor] - start => %v", singleLineCommand)
|
||||
|
||||
if len(singleLineCommand) == 0 {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// 创建命令实例
|
||||
cmd := exec.Command(singleLineCommand[0], singleLineCommand[1:]...)
|
||||
|
||||
// 创建输出缓冲区
|
||||
var stdoutBuf, stderrBuf bytes.Buffer
|
||||
cmd.Stdout = &stdoutBuf
|
||||
cmd.Stderr = &stderrBuf
|
||||
|
||||
// 执行命令并获取错误信息
|
||||
err := cmd.Run()
|
||||
|
||||
// 合并输出结果
|
||||
output := mergeOutput(&stdoutBuf, &stderrBuf)
|
||||
|
||||
// 判断执行结果
|
||||
return err == nil, output
|
||||
}
|
||||
|
||||
// 若追求极致性能,可优化为流式合并
|
||||
func mergeOutput(stdout, stderr *bytes.Buffer) []string {
|
||||
combined := make([]string, 0, bytes.Count(stdout.Bytes(), []byte{'\n'})+bytes.Count(stderr.Bytes(), []byte{'\n'}))
|
||||
combined = append(combined, strings.Split(stdout.String(), "\n")...)
|
||||
combined = append(combined, strings.Split(stderr.String(), "\n")...)
|
||||
return combined
|
||||
}
|
||||
|
||||
// mergeOutput 合并标准输出和标准错误,按行分割
|
||||
//func mergeOutput(stdoutBuf, stderrBuf *bytes.Buffer) []string {
|
||||
// var output []string
|
||||
//
|
||||
// // 处理标准输出
|
||||
// scanner := bufio.NewScanner(stdoutBuf)
|
||||
// for scanner.Scan() {
|
||||
// output = append(output, scanner.Text())
|
||||
// }
|
||||
//
|
||||
// // 处理标准错误
|
||||
// scanner = bufio.NewScanner(stderrBuf)
|
||||
// for scanner.Scan() {
|
||||
// output = append(output, scanner.Text())
|
||||
// }
|
||||
//
|
||||
// return output
|
||||
//}
|
||||
|
||||
// PipeLineCommandExecutor 执行管道命令,返回执行结果和合并后的输出内容
|
||||
// pipeLineCommand: 管道命令组,如 [][]string{{"ps", "aux"}, {"grep", "nginx"}, {"wc", "-l"}}
|
||||
// 返回值:
|
||||
@@ -102,65 +161,6 @@ func PipeLineCommandExecutor(pipeLineCommand [][]string) (ok bool, resultLog []s
|
||||
return success, output
|
||||
}
|
||||
|
||||
// SingleLineCommandExecutor 执行单行shell命令,返回执行结果和输出内容
|
||||
// singleLineCommand: 命令及参数,如 []string{"ls", "-l"}
|
||||
// 返回值:
|
||||
//
|
||||
// bool - 命令是否执行成功(true为成功,false为失败)
|
||||
// []string - 合并后的标准输出和标准错误内容(按行分割)
|
||||
func SingleLineCommandExecutor(singleLineCommand []string) (ok bool, resultLog []string) {
|
||||
|
||||
log.Info("[Excutor] - start => %v", singleLineCommand)
|
||||
|
||||
if len(singleLineCommand) == 0 {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// 创建命令实例
|
||||
cmd := exec.Command(singleLineCommand[0], singleLineCommand[1:]...)
|
||||
|
||||
// 创建输出缓冲区
|
||||
var stdoutBuf, stderrBuf bytes.Buffer
|
||||
cmd.Stdout = &stdoutBuf
|
||||
cmd.Stderr = &stderrBuf
|
||||
|
||||
// 执行命令并获取错误信息
|
||||
err := cmd.Run()
|
||||
|
||||
// 合并输出结果
|
||||
output := mergeOutput(&stdoutBuf, &stderrBuf)
|
||||
|
||||
// 判断执行结果
|
||||
return err == nil, output
|
||||
}
|
||||
|
||||
// mergeOutput 合并标准输出和标准错误,按行分割
|
||||
//func mergeOutput(stdoutBuf, stderrBuf *bytes.Buffer) []string {
|
||||
// var output []string
|
||||
//
|
||||
// // 处理标准输出
|
||||
// scanner := bufio.NewScanner(stdoutBuf)
|
||||
// for scanner.Scan() {
|
||||
// output = append(output, scanner.Text())
|
||||
// }
|
||||
//
|
||||
// // 处理标准错误
|
||||
// scanner = bufio.NewScanner(stderrBuf)
|
||||
// for scanner.Scan() {
|
||||
// output = append(output, scanner.Text())
|
||||
// }
|
||||
//
|
||||
// return output
|
||||
//}
|
||||
|
||||
// 若追求极致性能,可优化为流式合并
|
||||
func mergeOutput(stdout, stderr *bytes.Buffer) []string {
|
||||
combined := make([]string, 0, bytes.Count(stdout.Bytes(), []byte{'\n'})+bytes.Count(stderr.Bytes(), []byte{'\n'}))
|
||||
combined = append(combined, strings.Split(stdout.String(), "\n")...)
|
||||
combined = append(combined, strings.Split(stderr.String(), "\n")...)
|
||||
return combined
|
||||
}
|
||||
|
||||
func main() {
|
||||
// 成功案例
|
||||
success, output := SingleLineCommandExecutor([]string{"ls", "-l"})
|
||||
|
||||
Reference in New Issue
Block a user