2022-10-24 23:06:49 +08:00
|
|
|
package nginx
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2022-11-30 17:33:30 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/nginx/components"
|
2022-10-24 23:06:49 +08:00
|
|
|
"io/ioutil"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
IndentedStyle = &Style{
|
2022-12-01 00:41:50 +08:00
|
|
|
SpaceBeforeBlocks: false,
|
2022-10-24 23:06:49 +08:00
|
|
|
StartIndent: 0,
|
2022-11-30 17:33:30 +08:00
|
|
|
Indent: 4,
|
2022-10-24 23:06:49 +08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
type Style struct {
|
|
|
|
SpaceBeforeBlocks bool
|
|
|
|
StartIndent int
|
|
|
|
Indent int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Style) Iterate() *Style {
|
|
|
|
newStyle := &Style{
|
|
|
|
SpaceBeforeBlocks: s.SpaceBeforeBlocks,
|
|
|
|
StartIndent: s.StartIndent + s.Indent,
|
|
|
|
Indent: s.Indent,
|
|
|
|
}
|
|
|
|
return newStyle
|
|
|
|
}
|
|
|
|
|
|
|
|
func DumpDirective(d components.IDirective, style *Style) string {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
|
|
|
if style.SpaceBeforeBlocks && d.GetBlock() != nil {
|
|
|
|
buf.WriteString("\n")
|
|
|
|
}
|
|
|
|
buf.WriteString(fmt.Sprintf("%s%s", strings.Repeat(" ", style.StartIndent), d.GetName()))
|
|
|
|
if len(d.GetParameters()) > 0 {
|
|
|
|
buf.WriteString(fmt.Sprintf(" %s", strings.Join(d.GetParameters(), " ")))
|
|
|
|
}
|
|
|
|
if d.GetBlock() == nil {
|
|
|
|
if d.GetName() != "" {
|
|
|
|
buf.WriteRune(';')
|
|
|
|
buf.WriteString(" ")
|
|
|
|
}
|
|
|
|
if d.GetComment() != "" {
|
|
|
|
buf.WriteString(d.GetComment())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
buf.WriteString(" {")
|
|
|
|
if d.GetComment() != "" {
|
|
|
|
buf.WriteString(" ")
|
|
|
|
buf.WriteString(d.GetComment())
|
|
|
|
}
|
|
|
|
buf.WriteString("\n")
|
2022-11-30 17:33:30 +08:00
|
|
|
buf.WriteString(DumpBlock(d.GetBlock(), style.Iterate(), d.GetBlock().GetLine()))
|
2022-10-24 23:06:49 +08:00
|
|
|
buf.WriteString(fmt.Sprintf("\n%s}", strings.Repeat(" ", style.StartIndent)))
|
|
|
|
}
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2022-11-30 17:33:30 +08:00
|
|
|
func DumpBlock(b components.IBlock, style *Style, startLine int) string {
|
2022-10-24 23:06:49 +08:00
|
|
|
var buf bytes.Buffer
|
2022-11-30 17:33:30 +08:00
|
|
|
line := startLine
|
|
|
|
if b.GetLine() > startLine {
|
|
|
|
for i := 0; i < b.GetLine()-startLine; i++ {
|
|
|
|
buf.WriteString("\n")
|
|
|
|
}
|
|
|
|
line = b.GetLine()
|
|
|
|
}
|
2022-10-24 23:06:49 +08:00
|
|
|
|
|
|
|
directives := b.GetDirectives()
|
|
|
|
|
|
|
|
for i, directive := range directives {
|
2022-11-30 17:33:30 +08:00
|
|
|
|
|
|
|
if directive.GetLine() > line {
|
|
|
|
for i := 0; i < b.GetLine()-line; i++ {
|
|
|
|
buf.WriteString("\n")
|
|
|
|
}
|
|
|
|
line = b.GetLine()
|
|
|
|
}
|
|
|
|
|
2022-10-24 23:06:49 +08:00
|
|
|
buf.WriteString(DumpDirective(directive, style))
|
|
|
|
if i != len(directives)-1 {
|
|
|
|
buf.WriteString("\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func DumpConfig(c *components.Config, style *Style) string {
|
2022-11-30 17:33:30 +08:00
|
|
|
return DumpBlock(c.Block, style, 1)
|
2022-10-24 23:06:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func WriteConfig(c *components.Config, style *Style) error {
|
|
|
|
return ioutil.WriteFile(c.FilePath, []byte(DumpConfig(c, style)), 0644)
|
|
|
|
}
|