refactor: nginx conf parser #45

This commit is contained in:
0xJacky 2023-01-04 14:01:14 +08:00
parent b7d5590714
commit 5cc9068f5f
No known key found for this signature in database
GPG key ID: B6E4A6E4A561BAF0
27 changed files with 942 additions and 1040 deletions

View file

@ -0,0 +1,49 @@
package nginx
import (
"fmt"
"github.com/tufanbarisyildirim/gonginx"
"github.com/tufanbarisyildirim/gonginx/parser"
"strings"
"testing"
)
func TestNgxConfParse(t *testing.T) {
p, err := parser.NewParser("conf/nextcloud_ngx.conf")
if err != nil {
fmt.Println(err)
return
}
n := p.Parse()
fn(n.Block, 0)
c, err := ParseNgxConfig("conf/nextcloud_ngx.conf")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(c)
c, err = ParseNgxConfig("conf/test.conf")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(c)
}
func fn(block gonginx.IBlock, deep int) {
if block == nil {
return
}
for _, v := range block.GetDirectives() {
if len(v.GetComment()) > 0 {
for _, c := range v.GetComment() {
fmt.Println(strings.Repeat("\t", deep), c)
}
}
fmt.Println(fmt.Sprintf("%s%s %s", strings.Repeat("\t", deep), v.GetName(), strings.Join(v.GetParameters(), " ")))
fn(v.GetBlock(), deep+1)
}
}