소스 검색

fix(self-check): supports Windows #1046

Jacky 2 달 전
부모
커밋
5da4136fcd
3개의 변경된 파일23개의 추가작업 그리고 12개의 파일을 삭제
  1. 2 2
      internal/nginx/config_args.go
  2. 1 1
      internal/nginx/exec.go
  3. 20 9
      internal/self_check/nginx_conf.go

+ 2 - 2
internal/nginx/config_args.go

@@ -35,7 +35,7 @@ func getNginxExePath() string {
 }
 
 // Returns the directory containing the nginx executable
-func getNginxExeDir() string {
+func GetNginxExeDir() string {
 	return filepath.Dir(getNginxExePath())
 }
 
@@ -68,7 +68,7 @@ func resolvePath(path string) string {
 
 	// Handle relative paths on Windows
 	if runtime.GOOS == "windows" && !filepath.IsAbs(path) {
-		return filepath.Join(getNginxExeDir(), path)
+		return filepath.Join(GetNginxExeDir(), path)
 	}
 
 	return path

+ 1 - 1
internal/nginx/exec.go

@@ -20,7 +20,7 @@ func execCommand(name string, cmd ...string) (stdOut string, stdErr error) {
 	case false:
 		execCmd := exec.Command(name, cmd...)
 		// fix #1046
-		execCmd.Dir = getNginxExeDir()
+		execCmd.Dir = GetNginxExeDir()
 		bytes, err := execCmd.CombinedOutput()
 		stdOut = string(bytes)
 		if err != nil {

+ 20 - 9
internal/self_check/nginx_conf.go

@@ -3,6 +3,8 @@ package self_check
 import (
 	"fmt"
 	"os"
+	"path/filepath"
+	"runtime"
 	"strings"
 	"time"
 
@@ -12,6 +14,15 @@ import (
 	"github.com/tufanbarisyildirim/gonginx/parser"
 )
 
+func resolvePath(path ...string) string {
+	// fix #1046
+	if runtime.GOOS == "windows" {
+		return strings.TrimLeft(filepath.ToSlash(strings.ReplaceAll(nginx.GetConfPath(path...), nginx.GetNginxExeDir(), "")), "/")
+	}
+
+	return nginx.GetConfPath(path...)
+}
+
 // CheckNginxConfIncludeSites checks if nginx.conf include sites-enabled
 func CheckNginxConfIncludeSites() error {
 	path := nginx.GetConfEntryPath()
@@ -34,7 +45,7 @@ func CheckNginxConfIncludeSites() error {
 			// find include sites-enabled
 			for _, directive := range v.GetBlock().GetDirectives() {
 				if directive.GetName() == "include" && len(directive.GetParameters()) > 0 &&
-					directive.GetParameters()[0].Value == nginx.GetConfPath("sites-enabled/*") {
+					strings.Contains(directive.GetParameters()[0].Value, "sites-enabled/*") {
 					return nil
 				}
 			}
@@ -67,7 +78,7 @@ func CheckNginxConfIncludeStreams() error {
 			// find include sites-enabled
 			for _, directive := range v.GetBlock().GetDirectives() {
 				if directive.GetName() == "include" && len(directive.GetParameters()) > 0 &&
-					directive.GetParameters()[0].Value == nginx.GetConfPath("streams-enabled/*") {
+					strings.Contains(directive.GetParameters()[0].Value, "streams-enabled/*") {
 					return nil
 				}
 			}
@@ -107,7 +118,7 @@ func FixNginxConfIncludeSites() error {
 			// add include sites-enabled/* to http block
 			includeDirective := &config.Directive{
 				Name:       "include",
-				Parameters: []config.Parameter{{Value: nginx.GetConfPath("sites-enabled/*")}},
+				Parameters: []config.Parameter{{Value: resolvePath("sites-enabled/*")}},
 			}
 
 			realBlock := v.GetBlock().(*config.HTTP)
@@ -119,7 +130,7 @@ func FixNginxConfIncludeSites() error {
 	}
 
 	// if no http block, append http block with include sites-enabled/*
-	content = append(content, fmt.Appendf(nil, "\nhttp {\n\tinclude %s;\n}\n", nginx.GetConfPath("sites-enabled/*"))...)
+	content = append(content, fmt.Appendf(nil, "\nhttp {\n\tinclude %s;\n}\n", resolvePath("sites-enabled/*"))...)
 	return os.WriteFile(path, content, 0644)
 }
 
@@ -152,7 +163,7 @@ func FixNginxConfIncludeStreams() error {
 			// add include streams-enabled/* to stream block
 			includeDirective := &config.Directive{
 				Name:       "include",
-				Parameters: []config.Parameter{{Value: nginx.GetConfPath("streams-enabled/*")}},
+				Parameters: []config.Parameter{{Value: resolvePath("streams-enabled/*")}},
 			}
 			realBlock := v.GetBlock().(*config.Block)
 			realBlock.Directives = append(realBlock.Directives, includeDirective)
@@ -163,7 +174,7 @@ func FixNginxConfIncludeStreams() error {
 	}
 
 	// if no stream block, append stream block with include streams-enabled/*
-	content = append(content, fmt.Appendf(nil, "\nstream {\n\tinclude %s;\n}\n", nginx.GetConfPath("streams-enabled/*"))...)
+	content = append(content, fmt.Appendf(nil, "\nstream {\n\tinclude %s;\n}\n", resolvePath("streams-enabled/*"))...)
 	return os.WriteFile(path, content, 0644)
 }
 
@@ -189,7 +200,7 @@ func CheckNginxConfIncludeConfD() error {
 			// find include conf.d
 			for _, directive := range v.GetBlock().GetDirectives() {
 				if directive.GetName() == "include" && len(directive.GetParameters()) > 0 &&
-					strings.HasPrefix(directive.GetParameters()[0].Value, nginx.GetConfPath("conf.d")) {
+					strings.Contains(directive.GetParameters()[0].Value, "conf.d/*") {
 					return nil
 				}
 			}
@@ -229,7 +240,7 @@ func FixNginxConfIncludeConfD() error {
 			// add include conf.d/*.conf to http block
 			includeDirective := &config.Directive{
 				Name:       "include",
-				Parameters: []config.Parameter{{Value: nginx.GetConfPath("conf.d/*.conf")}},
+				Parameters: []config.Parameter{{Value: resolvePath("conf.d/*.conf")}},
 			}
 
 			realBlock := v.GetBlock().(*config.HTTP)
@@ -241,6 +252,6 @@ func FixNginxConfIncludeConfD() error {
 	}
 
 	// if no http block, append http block with include conf.d/*.conf
-	content = append(content, fmt.Appendf(nil, "\nhttp {\n\tinclude %s;\n}\n", nginx.GetConfPath("conf.d/*.conf"))...)
+	content = append(content, fmt.Appendf(nil, "\nhttp {\n\tinclude %s;\n}\n", resolvePath("conf.d/*.conf"))...)
 	return os.WriteFile(path, content, 0644)
 }