Skip to content

Commit 5da4136

Browse files
committed
fix(self-check): supports Windows #1046
1 parent b055c54 commit 5da4136

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

internal/nginx/config_args.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func getNginxExePath() string {
3535
}
3636

3737
// Returns the directory containing the nginx executable
38-
func getNginxExeDir() string {
38+
func GetNginxExeDir() string {
3939
return filepath.Dir(getNginxExePath())
4040
}
4141

@@ -68,7 +68,7 @@ func resolvePath(path string) string {
6868

6969
// Handle relative paths on Windows
7070
if runtime.GOOS == "windows" && !filepath.IsAbs(path) {
71-
return filepath.Join(getNginxExeDir(), path)
71+
return filepath.Join(GetNginxExeDir(), path)
7272
}
7373

7474
return path

internal/nginx/exec.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func execCommand(name string, cmd ...string) (stdOut string, stdErr error) {
2020
case false:
2121
execCmd := exec.Command(name, cmd...)
2222
// fix #1046
23-
execCmd.Dir = getNginxExeDir()
23+
execCmd.Dir = GetNginxExeDir()
2424
bytes, err := execCmd.CombinedOutput()
2525
stdOut = string(bytes)
2626
if err != nil {

internal/self_check/nginx_conf.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package self_check
33
import (
44
"fmt"
55
"os"
6+
"path/filepath"
7+
"runtime"
68
"strings"
79
"time"
810

@@ -12,6 +14,15 @@ import (
1214
"github.com/tufanbarisyildirim/gonginx/parser"
1315
)
1416

17+
func resolvePath(path ...string) string {
18+
// fix #1046
19+
if runtime.GOOS == "windows" {
20+
return strings.TrimLeft(filepath.ToSlash(strings.ReplaceAll(nginx.GetConfPath(path...), nginx.GetNginxExeDir(), "")), "/")
21+
}
22+
23+
return nginx.GetConfPath(path...)
24+
}
25+
1526
// CheckNginxConfIncludeSites checks if nginx.conf include sites-enabled
1627
func CheckNginxConfIncludeSites() error {
1728
path := nginx.GetConfEntryPath()
@@ -34,7 +45,7 @@ func CheckNginxConfIncludeSites() error {
3445
// find include sites-enabled
3546
for _, directive := range v.GetBlock().GetDirectives() {
3647
if directive.GetName() == "include" && len(directive.GetParameters()) > 0 &&
37-
directive.GetParameters()[0].Value == nginx.GetConfPath("sites-enabled/*") {
48+
strings.Contains(directive.GetParameters()[0].Value, "sites-enabled/*") {
3849
return nil
3950
}
4051
}
@@ -67,7 +78,7 @@ func CheckNginxConfIncludeStreams() error {
6778
// find include sites-enabled
6879
for _, directive := range v.GetBlock().GetDirectives() {
6980
if directive.GetName() == "include" && len(directive.GetParameters()) > 0 &&
70-
directive.GetParameters()[0].Value == nginx.GetConfPath("streams-enabled/*") {
81+
strings.Contains(directive.GetParameters()[0].Value, "streams-enabled/*") {
7182
return nil
7283
}
7384
}
@@ -107,7 +118,7 @@ func FixNginxConfIncludeSites() error {
107118
// add include sites-enabled/* to http block
108119
includeDirective := &config.Directive{
109120
Name: "include",
110-
Parameters: []config.Parameter{{Value: nginx.GetConfPath("sites-enabled/*")}},
121+
Parameters: []config.Parameter{{Value: resolvePath("sites-enabled/*")}},
111122
}
112123

113124
realBlock := v.GetBlock().(*config.HTTP)
@@ -119,7 +130,7 @@ func FixNginxConfIncludeSites() error {
119130
}
120131

121132
// if no http block, append http block with include sites-enabled/*
122-
content = append(content, fmt.Appendf(nil, "\nhttp {\n\tinclude %s;\n}\n", nginx.GetConfPath("sites-enabled/*"))...)
133+
content = append(content, fmt.Appendf(nil, "\nhttp {\n\tinclude %s;\n}\n", resolvePath("sites-enabled/*"))...)
123134
return os.WriteFile(path, content, 0644)
124135
}
125136

@@ -152,7 +163,7 @@ func FixNginxConfIncludeStreams() error {
152163
// add include streams-enabled/* to stream block
153164
includeDirective := &config.Directive{
154165
Name: "include",
155-
Parameters: []config.Parameter{{Value: nginx.GetConfPath("streams-enabled/*")}},
166+
Parameters: []config.Parameter{{Value: resolvePath("streams-enabled/*")}},
156167
}
157168
realBlock := v.GetBlock().(*config.Block)
158169
realBlock.Directives = append(realBlock.Directives, includeDirective)
@@ -163,7 +174,7 @@ func FixNginxConfIncludeStreams() error {
163174
}
164175

165176
// if no stream block, append stream block with include streams-enabled/*
166-
content = append(content, fmt.Appendf(nil, "\nstream {\n\tinclude %s;\n}\n", nginx.GetConfPath("streams-enabled/*"))...)
177+
content = append(content, fmt.Appendf(nil, "\nstream {\n\tinclude %s;\n}\n", resolvePath("streams-enabled/*"))...)
167178
return os.WriteFile(path, content, 0644)
168179
}
169180

@@ -189,7 +200,7 @@ func CheckNginxConfIncludeConfD() error {
189200
// find include conf.d
190201
for _, directive := range v.GetBlock().GetDirectives() {
191202
if directive.GetName() == "include" && len(directive.GetParameters()) > 0 &&
192-
strings.HasPrefix(directive.GetParameters()[0].Value, nginx.GetConfPath("conf.d")) {
203+
strings.Contains(directive.GetParameters()[0].Value, "conf.d/*") {
193204
return nil
194205
}
195206
}
@@ -229,7 +240,7 @@ func FixNginxConfIncludeConfD() error {
229240
// add include conf.d/*.conf to http block
230241
includeDirective := &config.Directive{
231242
Name: "include",
232-
Parameters: []config.Parameter{{Value: nginx.GetConfPath("conf.d/*.conf")}},
243+
Parameters: []config.Parameter{{Value: resolvePath("conf.d/*.conf")}},
233244
}
234245

235246
realBlock := v.GetBlock().(*config.HTTP)
@@ -241,6 +252,6 @@ func FixNginxConfIncludeConfD() error {
241252
}
242253

243254
// if no http block, append http block with include conf.d/*.conf
244-
content = append(content, fmt.Appendf(nil, "\nhttp {\n\tinclude %s;\n}\n", nginx.GetConfPath("conf.d/*.conf"))...)
255+
content = append(content, fmt.Appendf(nil, "\nhttp {\n\tinclude %s;\n}\n", resolvePath("conf.d/*.conf"))...)
245256
return os.WriteFile(path, content, 0644)
246257
}

0 commit comments

Comments
 (0)