Selaa lähdekoodia

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

Jacky 2 viikkoa sitten
vanhempi
commit
92dc61ecb4
2 muutettua tiedostoa jossa 8 lisäystä ja 3 poistoa
  1. 2 0
      internal/nginx/format_code.go
  2. 6 3
      internal/nginx/parse.go

+ 2 - 0
internal/nginx/format_code.go

@@ -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 {

+ 6 - 3
internal/nginx/parse.go

@@ -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)
 		}
 	}