-
Notifications
You must be signed in to change notification settings - Fork 407
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(Expand Variables): Custom variable expansion instead of Go's os.Expand #58
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -270,12 +270,19 @@ func parseValue(value string, envMap map[string]string) string { | |
|
||
// check if we've got quoted values or possible escapes | ||
if len(value) > 1 { | ||
first := string(value[0:1]) | ||
last := string(value[len(value)-1:]) | ||
if first == last && strings.ContainsAny(first, `"'`) { | ||
rs := regexp.MustCompile(`\A'(.*)'\z`) | ||
singleQuotes := rs.FindStringSubmatch(value) | ||
|
||
rd := regexp.MustCompile(`\A"(.*)"\z`) | ||
doubleQuotes := rd.FindStringSubmatch(value) | ||
|
||
if singleQuotes != nil || doubleQuotes != nil { | ||
// pull the quotes off the edges | ||
value = value[1 : len(value)-1] | ||
// handle escapes | ||
} | ||
|
||
if doubleQuotes != nil { | ||
// expand newlines | ||
escapeRegex := regexp.MustCompile(`\\.`) | ||
value = escapeRegex.ReplaceAllStringFunc(value, func(match string) string { | ||
c := strings.TrimPrefix(match, `\`) | ||
|
@@ -285,23 +292,38 @@ func parseValue(value string, envMap map[string]string) string { | |
case "r": | ||
return "\r" | ||
default: | ||
return c | ||
return match | ||
} | ||
}) | ||
// unescape characters | ||
e := regexp.MustCompile(`\\([^$])`) | ||
value = e.ReplaceAllString(value, "$1") | ||
} | ||
|
||
if singleQuotes == nil { | ||
value = expandVariables(value, envMap) | ||
} | ||
} | ||
|
||
// expand variables | ||
value = os.Expand(value, func(key string) string { | ||
if val, ok := envMap[key]; ok { | ||
return val | ||
return value | ||
} | ||
|
||
func expandVariables(v string, m map[string]string) string { | ||
r := regexp.MustCompile(`(\\)?(\$)(\()?\{?([A-Z0-9_]+)?\}?`) | ||
|
||
return r.ReplaceAllStringFunc(v, func(s string) string { | ||
submatch := r.FindStringSubmatch(s) | ||
|
||
if submatch == nil { | ||
return s | ||
} | ||
if val, ok := os.LookupEnv(key); ok { | ||
return val | ||
if submatch[1] == "\\" || submatch[2] == "(" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How can the second submatch be |
||
return submatch[0][1:] | ||
} else if submatch[4] != "" { | ||
return m[submatch[4]] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if the var was defined outide of the env file? shouln't this fallback to |
||
} | ||
return "" | ||
return s | ||
}) | ||
return value | ||
} | ||
|
||
func isIgnoredLine(line string) bool { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isn't a
(\))?
missing at the end of this regex?