-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathmain.go
45 lines (40 loc) · 1.19 KB
/
main.go
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
package main
import (
"fmt"
"log"
"github.com/tufanbarisyildirim/gonginx/config"
"github.com/tufanbarisyildirim/gonginx/dumper"
"github.com/tufanbarisyildirim/gonginx/parser"
)
func updateServerListenPort(filePath string, oldPort string, newPort string) (string, error) {
p, err := parser.NewParser(filePath)
if err != nil {
return "", fmt.Errorf("failed to create parser: %w", err)
}
conf, err := p.Parse()
if err != nil {
return "", fmt.Errorf("failed to parse config: %w", err)
}
servers := conf.FindDirectives("server")
for _, server := range servers {
listens := server.GetBlock().FindDirectives("listen")
for _, listen := range listens {
if listen.GetParameters()[0].GetValue() == oldPort {
listenDirective := listen.(*config.Directive)
listenDirective.Parameters[0].SetValue(newPort)
}
}
}
changedConf := dumper.DumpConfig(conf, dumper.IndentedStyle)
return changedConf, nil
}
func main() {
filePath := "../../testdata/full_conf/nginx.conf"
oldPort := "80"
newPort := "8080"
if changedConf, err := updateServerListenPort(filePath, oldPort, newPort); err != nil {
log.Fatalf("Error updating server listen port: %v", err)
} else {
fmt.Println(changedConf)
}
}