/
envvar.go
247 lines (225 loc) · 5.05 KB
/
envvar.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package config
import (
"bytes"
"errors"
"io"
"os"
"strings"
"github.com/compose-spec/compose-go/template"
"github.com/compose-spec/compose-go/utils"
)
type trimmer struct {
input *bytes.Reader
output bytes.Buffer
}
func removeComments(buf []byte) ([]byte, error) {
t := &trimmer{
input: bytes.NewReader(buf),
output: bytes.Buffer{},
}
err := t.process()
return t.output.Bytes(), err
}
func (t *trimmer) process() error {
for {
// Read the next byte until EOF
c, err := t.input.ReadByte()
if err != nil {
if errors.Is(err, io.EOF) {
break
}
return err
}
// Switch states if we need to
switch c {
case '\\':
_ = t.input.UnreadByte()
err = t.escape()
case '\'':
_ = t.input.UnreadByte()
if t.hasNQuotes(c, 3) {
err = t.tripleSingleQuote()
} else {
err = t.singleQuote()
}
case '"':
_ = t.input.UnreadByte()
if t.hasNQuotes(c, 3) {
err = t.tripleDoubleQuote()
} else {
err = t.doubleQuote()
}
case '#':
err = t.comment()
default:
t.output.WriteByte(c)
continue
}
if err != nil {
if errors.Is(err, io.EOF) {
break
}
return err
}
}
return nil
}
func (t *trimmer) hasNQuotes(ref byte, limit int64) bool {
var count int64
// Look ahead check if the next characters are what we expect
for count = 0; count < limit; count++ {
c, err := t.input.ReadByte()
if err != nil || c != ref {
break
}
}
// We also need to unread the non-matching character
offset := -count
if count < limit {
offset--
}
// Unread the matched characters
_, _ = t.input.Seek(offset, io.SeekCurrent)
return count >= limit
}
func (t *trimmer) readWriteByte() (byte, error) {
c, err := t.input.ReadByte()
if err != nil {
return 0, err
}
return c, t.output.WriteByte(c)
}
func (t *trimmer) escape() error {
// Consumer the known starting backslash and quote
_, _ = t.readWriteByte()
// Read the next character which is the escaped one and exit
_, err := t.readWriteByte()
return err
}
func (t *trimmer) singleQuote() error {
// Consumer the known starting quote
_, _ = t.readWriteByte()
// Read bytes until EOF, line end or another single quote
for {
if c, err := t.readWriteByte(); err != nil || c == '\'' || c == '\n' {
return err
}
}
}
func (t *trimmer) tripleSingleQuote() error {
for i := 0; i < 3; i++ {
// Consumer the known starting quotes
_, _ = t.readWriteByte()
}
// Read bytes until EOF or another set of triple single quotes
for {
c, err := t.readWriteByte()
if err != nil {
return err
}
if c == '\'' && t.hasNQuotes('\'', 2) {
// Consumer the two additional ending quotes
_, _ = t.readWriteByte()
_, _ = t.readWriteByte()
return nil
}
}
}
func (t *trimmer) doubleQuote() error {
// Consumer the known starting quote
_, _ = t.readWriteByte()
// Read bytes until EOF, line end or another double quote
for {
c, err := t.input.ReadByte()
if err != nil {
return err
}
switch c {
case '\\':
// Found escaped character
_ = t.input.UnreadByte()
if err := t.escape(); err != nil {
return err
}
continue
case '"', '\n':
// Found terminator
return t.output.WriteByte(c)
}
t.output.WriteByte(c)
}
}
func (t *trimmer) tripleDoubleQuote() error {
for i := 0; i < 3; i++ {
// Consumer the known starting quotes
_, _ = t.readWriteByte()
}
// Read bytes until EOF or another set of triple double quotes
for {
c, err := t.input.ReadByte()
if err != nil {
return err
}
switch c {
case '\\':
// Found escaped character
_ = t.input.UnreadByte()
if err := t.escape(); err != nil {
return err
}
continue
case '"':
t.output.WriteByte(c)
if t.hasNQuotes('"', 2) {
// Consumer the two additional ending quotes
_, _ = t.readWriteByte()
_, _ = t.readWriteByte()
return nil
}
continue
}
t.output.WriteByte(c)
}
}
func (t *trimmer) comment() error {
// Read bytes until EOF or a line break
for {
c, err := t.input.ReadByte()
if err != nil {
return err
}
if c == '\n' {
return t.output.WriteByte(c)
}
}
}
func substituteEnvironment(contents []byte, oldReplacementBehavior bool) ([]byte, error) {
options := []template.Option{
template.WithReplacementFunction(func(s string, m template.Mapping, cfg *template.Config) (string, error) {
result, applied, err := template.DefaultReplacementAppliedFunc(s, m, cfg)
if err == nil && !applied {
// Keep undeclared environment-variable patterns to reproduce
// pre-v1.27 behavior
return s, nil
}
if err != nil && strings.HasPrefix(err.Error(), "Invalid template:") {
// Keep invalid template patterns to ignore regexp substitutions
// like ${1}
return s, nil
}
return result, err
}),
template.WithoutLogging,
}
if oldReplacementBehavior {
options = append(options, template.WithPattern(oldVarRe))
}
envMap := utils.GetAsEqualsMap(os.Environ())
retVal, err := template.SubstituteWithOptions(string(contents), func(k string) (string, bool) {
if v, ok := envMap[k]; ok {
return v, ok
}
return "", false
}, options...)
return []byte(retVal), err
}