forked from aluttik/go-crossplane
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.go
195 lines (170 loc) · 4.18 KB
/
build.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package crossplane
import (
"bytes"
"io"
"os"
"path/filepath"
"strings"
"unicode"
)
type BuildOptions struct {
Indent int
Tabs bool
Header bool
}
// BuildFiles builds all of the config files in a crossplane.Payload and
// writes them to disk.
func BuildFiles(payload Payload, dir string, options *BuildOptions) error {
if len(dir) == 0 {
cwd, err := os.Getwd()
if err != nil {
return err
}
dir = cwd
}
for _, config := range payload.Config {
path := config.File
if !filepath.IsAbs(path) {
path = filepath.Join(dir, path)
}
// make directories that need to be made for the config to be built
dirpath := filepath.Dir(path)
if err := os.MkdirAll(dirpath, os.ModeDir|os.ModePerm); err != nil {
return err
}
// build then create the nginx config file using the json payload
var buf bytes.Buffer
if err := Build(&buf, config, options); err != nil {
return err
}
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
output := append(bytes.TrimRightFunc(buf.Bytes(), unicode.IsSpace), '\n')
if _, err := f.Write(output); err != nil {
return err
}
}
return nil
}
// Build creates an NGINX config from a crossplane.Config.
func Build(w io.Writer, config Config, options *BuildOptions) error {
if options.Indent == 0 {
options.Indent = 4
}
head := ""
if options.Header {
head += "# This config was built from JSON using NGINX crossplane.\n"
head += "# If you encounter any bugs please report them here:\n"
head += "# https://github.com/nginxinc/crossplane/issues\n"
head += "\n"
}
body := ""
body = buildBlock(body, config.Parsed, 0, 0, options)
_, err := w.Write([]byte(head + body))
return err
}
func buildBlock(output string, block []Directive, depth int, lastLine int, options *BuildOptions) string {
for _, stmt := range block {
var built string
if stmt.IsComment() && stmt.Line == lastLine {
output += " #" + *stmt.Comment
continue
} else if stmt.IsComment() {
built = "#" + *stmt.Comment
} else {
directive := enquote(stmt.Directive)
args := []string{}
for _, arg := range stmt.Args {
args = append(args, enquote(arg))
}
if directive == "if" {
built = "if (" + strings.Join(args, " ") + ")"
} else if len(args) > 0 {
built = directive + " " + strings.Join(args, " ")
} else {
built = directive
}
if stmt.Block == nil {
built += ";"
} else {
built += " {"
built = buildBlock(built, *stmt.Block, depth+1, stmt.Line, options)
built += "\n" + margin(options, depth) + "}"
}
}
if len(output) > 0 {
output += "\n"
}
output += margin(options, depth) + built
lastLine = stmt.Line
}
return output
}
func margin(options *BuildOptions, depth int) string {
if options.Tabs {
return strings.Repeat("\t", depth)
}
return strings.Repeat(" ", options.Indent*depth)
}
func enquote(arg string) string {
if !needsQuotes(arg) {
return arg
}
quoted := strings.ReplaceAll(repr(arg), `\\`, `\`)
return quoted
}
func needsQuotes(s string) bool {
if s == "" {
return true
}
// lexer should throw an error when variable expansion syntax
// is messed up, but just wrap it in quotes for now I guess
var char string
chars := escape(s)
// arguments can't start with variable expansion syntax
char = <-chars
if isSpace(char) || char == "{" || char == "}" || char == ";" || char == `"` || char == "'" || char == "${" {
return true
}
expanding := false
for c := range chars {
char = c
if isSpace(char) || char == "{" || char == ";" || char == `"` || char == "'" {
return true
} else if (expanding && char == "${") || (!expanding && char == "}") {
return true
} else if (expanding && char == "}") || (!expanding && char == "${") {
expanding = !expanding
}
}
return expanding || char == "\\" || char == "$"
}
func escape(s string) chan string {
c := make(chan string)
go func() {
prev, char := "", ""
for _, r := range s {
char = string(r)
if prev == "\\" || prev+char == "${" {
prev += char
c <- prev
continue
}
if prev == "$" {
c <- prev
}
if char != "\\" && char != "$" {
c <- char
}
prev = char
}
if char == "\\" || char == "$" {
c <- char
}
close(c)
}()
return c
}