@@ -209,6 +209,8 @@ func readFile(filename string) (envMap map[string]string, err error) {
209
209
return Parse (file )
210
210
}
211
211
212
+ var exportRegex = regexp .MustCompile (`^\s*(?:export\s+)?(.*?)\s*$` )
213
+
212
214
func parseLine (line string , envMap map [string ]string ) (key string , value string , err error ) {
213
215
if len (line ) == 0 {
214
216
err = errors .New ("zero length string" )
@@ -258,26 +260,30 @@ func parseLine(line string, envMap map[string]string) (key string, value string,
258
260
}
259
261
key = strings .TrimSpace (key )
260
262
261
- re := regexp .MustCompile (`^\s*(?:export\s+)?(.*?)\s*$` )
262
- key = re .ReplaceAllString (splitString [0 ], "$1" )
263
+ key = exportRegex .ReplaceAllString (splitString [0 ], "$1" )
263
264
264
265
// Parse the value
265
266
value = parseValue (splitString [1 ], envMap )
266
267
return
267
268
}
268
269
270
+ var (
271
+ singleQuotesRegex = regexp .MustCompile (`\A'(.*)'\z` )
272
+ doubleQuotesRegex = regexp .MustCompile (`\A"(.*)"\z` )
273
+ escapeRegex = regexp .MustCompile (`\\.` )
274
+ unescapeCharsRegex = regexp .MustCompile (`\\([^$])` )
275
+ )
276
+
269
277
func parseValue (value string , envMap map [string ]string ) string {
270
278
271
279
// trim
272
280
value = strings .Trim (value , " " )
273
281
274
282
// check if we've got quoted values or possible escapes
275
283
if len (value ) > 1 {
276
- rs := regexp .MustCompile (`\A'(.*)'\z` )
277
- singleQuotes := rs .FindStringSubmatch (value )
284
+ singleQuotes := singleQuotesRegex .FindStringSubmatch (value )
278
285
279
- rd := regexp .MustCompile (`\A"(.*)"\z` )
280
- doubleQuotes := rd .FindStringSubmatch (value )
286
+ doubleQuotes := doubleQuotesRegex .FindStringSubmatch (value )
281
287
282
288
if singleQuotes != nil || doubleQuotes != nil {
283
289
// pull the quotes off the edges
@@ -286,7 +292,6 @@ func parseValue(value string, envMap map[string]string) string {
286
292
287
293
if doubleQuotes != nil {
288
294
// expand newlines
289
- escapeRegex := regexp .MustCompile (`\\.` )
290
295
value = escapeRegex .ReplaceAllStringFunc (value , func (match string ) string {
291
296
c := strings .TrimPrefix (match , `\` )
292
297
switch c {
@@ -299,8 +304,7 @@ func parseValue(value string, envMap map[string]string) string {
299
304
}
300
305
})
301
306
// unescape characters
302
- e := regexp .MustCompile (`\\([^$])` )
303
- value = e .ReplaceAllString (value , "$1" )
307
+ value = unescapeCharsRegex .ReplaceAllString (value , "$1" )
304
308
}
305
309
306
310
if singleQuotes == nil {
@@ -311,11 +315,11 @@ func parseValue(value string, envMap map[string]string) string {
311
315
return value
312
316
}
313
317
314
- func expandVariables (v string , m map [string ]string ) string {
315
- r := regexp .MustCompile (`(\\)?(\$)(\()?\{?([A-Z0-9_]+)?\}?` )
318
+ var expandVarRegex = regexp .MustCompile (`(\\)?(\$)(\()?\{?([A-Z0-9_]+)?\}?` )
316
319
317
- return r .ReplaceAllStringFunc (v , func (s string ) string {
318
- submatch := r .FindStringSubmatch (s )
320
+ func expandVariables (v string , m map [string ]string ) string {
321
+ return expandVarRegex .ReplaceAllStringFunc (v , func (s string ) string {
322
+ submatch := expandVarRegex .FindStringSubmatch (s )
319
323
320
324
if submatch == nil {
321
325
return s
0 commit comments