mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-09 09:06:36 +08:00
feat: 优化 Nginx 版本解析逻辑,添加正则表达式匹配,更新测试用例以验证新逻辑
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -300,19 +301,15 @@ func detectVersion(ctx context.Context, options ExecutorOptions, runner CommandR
|
|||||||
}
|
}
|
||||||
|
|
||||||
func parseNginxVersion(output string) string {
|
func parseNginxVersion(output string) string {
|
||||||
start := strings.Index(output, "nginx/")
|
matches := nginxVersionPattern.FindStringSubmatch(output)
|
||||||
if start < 0 {
|
if len(matches) != 2 {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
version := output[start+len("nginx/"):]
|
return matches[1]
|
||||||
for i, r := range version {
|
|
||||||
if r == ' ' || r == '\n' || r == '\r' || r == '\t' {
|
|
||||||
return version[:i]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return version
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var nginxVersionPattern = regexp.MustCompile(`(?im)nginx version:\s*nginx/([^\s]+)`)
|
||||||
|
|
||||||
type backupState struct {
|
type backupState struct {
|
||||||
RouteExisted bool
|
RouteExisted bool
|
||||||
RouteData []byte
|
RouteData []byte
|
||||||
|
|||||||
@@ -244,6 +244,18 @@ func TestDetectVersionFromDockerImage(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestParseNginxVersionIgnoresDockerEntrypointPaths(t *testing.T) {
|
||||||
|
output := strings.Join([]string{
|
||||||
|
"/docker-entrypoint.sh: /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh: info: can not modify /etc/nginx/conf.d/default.conf (read-only file system?)",
|
||||||
|
"nginx version: nginx/1.27.4",
|
||||||
|
}, "\n")
|
||||||
|
|
||||||
|
version := parseNginxVersion(output)
|
||||||
|
if version != "1.27.4" {
|
||||||
|
t.Fatalf("unexpected version: %s", version)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestManagerApplyWritesSupportFilesAndReplacesPlaceholder(t *testing.T) {
|
func TestManagerApplyWritesSupportFilesAndReplacesPlaceholder(t *testing.T) {
|
||||||
tempDir := t.TempDir()
|
tempDir := t.TempDir()
|
||||||
manager := &Manager{
|
manager := &Manager{
|
||||||
|
|||||||
Reference in New Issue
Block a user