-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstream_linux_386.go
More file actions
74 lines (66 loc) · 2.2 KB
/
Copy pathstream_linux_386.go
File metadata and controls
74 lines (66 loc) · 2.2 KB
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
//go:build linux && 386
// +build linux,386
package stream
import (
"embed"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
//go:embed bin/stream-linux-386
var binFiles embed.FS
func GetStream() (streamCmd string, tempFile string, err error) {
var errors []string
// 首先尝试系统中已安装的 stream
if path, lookErr := exec.LookPath("stream"); lookErr == nil {
output, runErr := exec.Command(path).CombinedOutput()
if runErr == nil || strings.Contains(string(output), "STREAM") {
return "stream", "", nil
} else {
errors = append(errors, fmt.Sprintf("stream 直接运行失败: %v\n输出: %s", runErr, string(output)))
}
} else {
errors = append(errors, fmt.Sprintf("无法找到 stream: %v", lookErr))
}
// 创建临时目录来存放嵌入的二进制文件
tempDir, tempErr := os.MkdirTemp("", "streamwrapper")
if tempErr != nil {
return "", "", fmt.Errorf("创建临时目录失败: %v", tempErr)
}
binName := "stream-linux-386"
binPath := filepath.Join("bin", binName)
fileContent, readErr := binFiles.ReadFile(binPath)
if readErr == nil {
tempFile = filepath.Join(tempDir, binName)
writeErr := os.WriteFile(tempFile, fileContent, 0755)
if writeErr == nil {
// 测试嵌入的二进制文件是否可运行
output, runErr := exec.Command(tempFile).CombinedOutput()
if runErr == nil || strings.Contains(string(output), "STREAM") {
return tempFile, tempFile, nil
} else {
errors = append(errors, fmt.Sprintf("%s 运行失败: %v\n输出: %s", tempFile, runErr, string(output)))
}
} else {
errors = append(errors, fmt.Sprintf("写入临时文件失败 (%s): %v", tempFile, writeErr))
}
} else {
errors = append(errors, fmt.Sprintf("读取嵌入的 stream 版本失败: %v", readErr))
}
return "", "", fmt.Errorf("无法找到可用的 stream 命令:\n%s", strings.Join(errors, "\n"))
}
func ExecuteStream(streamPath string, args []string) error {
var cmd *exec.Cmd
if streamPath == "stream" {
cmd = exec.Command(streamPath, args...)
} else {
allArgs := append([]string{streamPath}, args...)
cmd = exec.Command("sh", "-c", strings.Join(allArgs, " "))
}
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}