forked from tufanbarisyildirim/gonginx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdumper.go
169 lines (151 loc) · 3.72 KB
/
dumper.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package gonginx
import (
"bytes"
"fmt"
"os"
"path/filepath"
"sort"
"strings"
)
var (
//NoIndentStyle default style
NoIndentStyle = &Style{
SortDirectives: false,
StartIndent: 0,
Indent: 0,
}
//IndentedStyle default style
IndentedStyle = &Style{
SortDirectives: false,
StartIndent: 0,
Indent: 4,
}
//NoIndentSortedStyle default style
NoIndentSortedStyle = &Style{
SortDirectives: true,
StartIndent: 0,
Indent: 0,
}
//NoIndentSortedSpaceStyle default style
NoIndentSortedSpaceStyle = &Style{
SortDirectives: true,
SpaceBeforeBlocks: true,
StartIndent: 0,
Indent: 0,
}
)
// Style dumping style
type Style struct {
SortDirectives bool
SpaceBeforeBlocks bool
StartIndent int
Indent int
}
// NewStyle create new style
func NewStyle() *Style {
style := &Style{
SortDirectives: false,
StartIndent: 0,
Indent: 4,
}
return style
}
// Iterate interate the indentation for sub blocks
func (s *Style) Iterate() *Style {
newStyle := &Style{
SortDirectives: s.SortDirectives,
SpaceBeforeBlocks: s.SpaceBeforeBlocks,
StartIndent: s.StartIndent + s.Indent,
Indent: s.Indent,
}
return newStyle
}
// DumpDirective convert a directive to a string
func DumpDirective(d IDirective, style *Style) string {
var buf bytes.Buffer
if style.SpaceBeforeBlocks && d.GetBlock() != nil {
buf.WriteString("\n")
}
if len(d.GetComment()) > 0 {
for _, comment := range d.GetComment() {
buf.WriteString(fmt.Sprintf("%s%s\n", strings.Repeat(" ", style.StartIndent), comment))
}
}
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 {
buf.WriteRune(';')
} else {
buf.WriteString(" {\n")
buf.WriteString(DumpBlock(d.GetBlock(), style.Iterate()))
buf.WriteString(fmt.Sprintf("\n%s}", strings.Repeat(" ", style.StartIndent)))
}
return buf.String()
}
// DumpBlock convert a directive to a string
func DumpBlock(b IBlock, style *Style) string {
var buf bytes.Buffer
directives := b.GetDirectives()
if style.SortDirectives {
sort.SliceStable(directives, func(i, j int) bool {
return directives[i].GetName() < directives[j].GetName()
})
}
for i, directive := range directives {
buf.WriteString(DumpDirective(directive, style))
if i != len(directives)-1 {
buf.WriteString("\n")
}
}
return buf.String()
}
// DumpConfig dump whole config
func DumpConfig(c *Config, style *Style) string {
return DumpBlock(c.Block, style)
}
// DumpInclude dump(stringify) the included AST
func DumpInclude(i *Include, style *Style) map[string]string {
mp := make(map[string]string)
for _, cfg := range i.Configs {
mp[cfg.FilePath] = DumpConfig(cfg, style)
}
return mp
}
// WriteConfig writes config
func WriteConfig(c *Config, style *Style, writeInclude bool) error {
if writeInclude {
includes := c.FindDirectives("include")
for _, include := range includes {
i, ok := include.(*Include)
if !ok {
panic("bug in FindDirective")
}
// no config parsed
if len(i.Configs) == 0 {
continue
}
mp := DumpInclude(i, style)
for path, config := range mp {
// create parent directories, if not exit
dir, _ := filepath.Split(path)
err := os.MkdirAll(dir, 0755)
if err != nil {
return err
}
err = os.WriteFile(path, []byte(config), 0644)
if err != nil {
return err
}
}
}
}
// create parent directories, if not exit
dir, _ := filepath.Split(c.FilePath)
err := os.MkdirAll(dir, 0755)
if err != nil {
return err
}
return os.WriteFile(c.FilePath, []byte(DumpConfig(c, style)), 0644)
}