fix(nginx-config): parse custom config error #699

This commit is contained in:
Jacky 2025-04-14 02:41:42 +00:00
parent 502b656bc5
commit 92dc61ecb4
No known key found for this signature in database
GPG key ID: 215C21B10DF38B4D
2 changed files with 8 additions and 3 deletions

View file

@ -3,6 +3,7 @@ package nginx
import (
"github.com/tufanbarisyildirim/gonginx/dumper"
"github.com/tufanbarisyildirim/gonginx/parser"
"github.com/uozi-tech/cosy/logger"
)
func (c *NgxConfig) FmtCode() (fmtContent string) {
@ -11,6 +12,7 @@ func (c *NgxConfig) FmtCode() (fmtContent string) {
}
func FmtCode(content string) (fmtContent string, err error) {
logger.Debugf("content: %s", content)
p := parser.NewStringParser(content, parser.WithSkipValidDirectivesErr())
c, err := p.Parse()
if err != nil {

View file

@ -1,10 +1,11 @@
package nginx
import (
"strings"
"github.com/pkg/errors"
"github.com/tufanbarisyildirim/gonginx/config"
"github.com/tufanbarisyildirim/gonginx/parser"
"strings"
)
const (
@ -135,9 +136,11 @@ func (u *NgxUpstream) parseUpstream(directive config.IDirective) {
func (c *NgxConfig) parseCustom(directive config.IDirective) {
if directive.GetBlock() == nil {
// fix #699
c.Custom += ";\n"
return
}
c.Custom += "{\n"
c.Custom += "\n{\n"
for _, v := range directive.GetBlock().GetDirectives() {
var params []string
for _, param := range v.GetParameters() {
@ -183,7 +186,7 @@ func parse(block config.IBlock, ngxConfig *NgxConfig) (err error) {
params = append(params, param.Value)
}
ngxConfig.Custom += strings.Join(v.GetComment(), "\n") + "\n" +
v.GetName() + " " + strings.Join(params, " ") + "\n"
v.GetName() + " " + strings.Join(params, " ")
ngxConfig.parseCustom(v)
}
}