U:调整数据集成
This commit is contained in:
1 parent
e5009ee984
commit
f15fb85251
1 file changed
+47
-15
@@ -5,6 +5,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
//nolint:typecheck
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
@@ -415,26 +416,57 @@ func getVipsVersion() (string, error) {
|
||||
return "", fmt.Errorf("LibVips path not set")
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
result, err := ExecuteCommand(ctx, VipsPath, "--version")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to get LibVips version: %w", err)
|
||||
// 这里不用 ExecuteCommand,避免在某些 vips.exe 构建上
|
||||
// 返回非零退出码时打出「命令执行失败」的错误日志。
|
||||
// 我们自己以较安静的方式尝试获取版本信息:先试 "--version",失败再试 "-v"。
|
||||
tryArgs := [][]string{
|
||||
{"--version"},
|
||||
{"-v"},
|
||||
}
|
||||
|
||||
// 解析版本信息
|
||||
output := string(result.Stdout)
|
||||
lines := bytes.Split(result.Stdout, []byte("\n"))
|
||||
if len(lines) > 0 {
|
||||
firstLine := string(lines[0])
|
||||
// 提取版本号 (例如: "vips-8.17.0")
|
||||
if strings.Contains(firstLine, "vips") {
|
||||
return strings.TrimSpace(firstLine), nil
|
||||
for idx, args := range tryArgs {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
cmd := exec.CommandContext(ctx, VipsPath, args...) //nolint:gosec
|
||||
var stdout, stderr bytes.Buffer
|
||||
cmd.Stdout = &stdout
|
||||
cmd.Stderr = &stderr
|
||||
|
||||
start := time.Now()
|
||||
err := cmd.Run()
|
||||
duration := time.Since(start)
|
||||
cancel()
|
||||
|
||||
if err != nil {
|
||||
// 首次失败只打 debug 日志并尝试下一种参数,最后一次失败再返回错误
|
||||
logger.Debug("获取 LibVips 版本失败",
|
||||
zap.String("program", VipsPath),
|
||||
zap.Strings("args", args),
|
||||
zap.Duration("duration", duration),
|
||||
zap.String("stdout", stdout.String()),
|
||||
zap.String("stderr", stderr.String()),
|
||||
zap.Error(err),
|
||||
)
|
||||
if idx == len(tryArgs)-1 {
|
||||
return "", fmt.Errorf("failed to get LibVips version: %w", err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
// 解析版本信息
|
||||
output := stdout.Bytes()
|
||||
lines := bytes.Split(output, []byte("\n"))
|
||||
if len(lines) > 0 {
|
||||
firstLine := string(lines[0])
|
||||
// 提取版本号 (例如: "vips-8.17.0")
|
||||
if strings.Contains(firstLine, "vips") {
|
||||
return strings.TrimSpace(firstLine), nil
|
||||
}
|
||||
}
|
||||
|
||||
return string(output), nil
|
||||
}
|
||||
|
||||
return output, nil
|
||||
return "", fmt.Errorf("failed to get LibVips version")
|
||||
}
|
||||
|
||||
// ensureExecutable 确保文件具有执行权限(仅适用于 Unix 系统)
|
||||
|
||||
Reference in new issue
Block a user