1 Star 0 Fork 0

yangwood/novacore

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
executor.go 1.34 KB
一键复制 编辑 原始数据 按行查看 历史
linyuan 提交于 2025-12-19 10:27 +08:00 . fix: concat media file
package tools
import (
"bytes"
"context"
"os/exec"
"go.uber.org/zap"
)
// CommandExecutor 用于执行命令并捕获输出
type CommandExecutor struct {
Command string
Args []string
Stdout bytes.Buffer
Stderr bytes.Buffer
}
// NewCommandExecutor 创建一个新的 CommandExecutor 实例
func NewCommandExecutor() *CommandExecutor {
return &CommandExecutor{}
}
// Run 执行命令并等待其完成
func (ce *CommandExecutor) Run(ctx context.Context, command string, args ...string) error {
ce.Command = command
ce.Args = args
WithTraceID(ctx.Value(TraceIDKey{}).(string)).Info("args", zap.Any("args", args))
cmd := exec.Command(ce.Command, ce.Args...)
cmd.Stdout = &ce.Stdout
cmd.Stderr = &ce.Stderr
WithTraceID(ctx.Value(TraceIDKey{}).(string)).Info("Command:", zap.String("command", cmd.String()))
if err := cmd.Start(); err != nil {
WithTraceID(ctx.Value(TraceIDKey{}).(string)).Error("Error starting command:", zap.Error(err))
return err
}
if err := cmd.Wait(); err != nil {
WithTraceID(ctx.Value(TraceIDKey{}).(string)).Error("Command finished with error:", zap.Error(err))
return err
}
return nil
}
// Output 获取标准输出
func (ce *CommandExecutor) Output() string {
return ce.Stdout.String()
}
// StderrOutput 获取标准错误输出
func (ce *CommandExecutor) StderrOutput() string {
return ce.Stderr.String()
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/yangwood/novacore.git
git@gitee.com:yangwood/novacore.git
yangwood
novacore
novacore
v1.2.92

搜索帮助