mirror of https://github.com/joho/godotenv.git
commit
b09de681dc
30
godotenv.go
30
godotenv.go
|
@ -209,6 +209,8 @@ func readFile(filename string) (envMap map[string]string, err error) {
|
||||||
return Parse(file)
|
return Parse(file)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var exportRegex = regexp.MustCompile(`^\s*(?:export\s+)?(.*?)\s*$`)
|
||||||
|
|
||||||
func parseLine(line string, envMap map[string]string) (key string, value string, err error) {
|
func parseLine(line string, envMap map[string]string) (key string, value string, err error) {
|
||||||
if len(line) == 0 {
|
if len(line) == 0 {
|
||||||
err = errors.New("zero length string")
|
err = errors.New("zero length string")
|
||||||
|
@ -258,14 +260,20 @@ func parseLine(line string, envMap map[string]string) (key string, value string,
|
||||||
}
|
}
|
||||||
key = strings.TrimSpace(key)
|
key = strings.TrimSpace(key)
|
||||||
|
|
||||||
re := regexp.MustCompile(`^\s*(?:export\s+)?(.*?)\s*$`)
|
key = exportRegex.ReplaceAllString(splitString[0], "$1")
|
||||||
key = re.ReplaceAllString(splitString[0], "$1")
|
|
||||||
|
|
||||||
// Parse the value
|
// Parse the value
|
||||||
value = parseValue(splitString[1], envMap)
|
value = parseValue(splitString[1], envMap)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
singleQuotesRegex = regexp.MustCompile(`\A'(.*)'\z`)
|
||||||
|
doubleQuotesRegex = regexp.MustCompile(`\A"(.*)"\z`)
|
||||||
|
escapeRegex = regexp.MustCompile(`\\.`)
|
||||||
|
unescapeCharsRegex = regexp.MustCompile(`\\([^$])`)
|
||||||
|
)
|
||||||
|
|
||||||
func parseValue(value string, envMap map[string]string) string {
|
func parseValue(value string, envMap map[string]string) string {
|
||||||
|
|
||||||
// trim
|
// trim
|
||||||
|
@ -273,11 +281,9 @@ func parseValue(value string, envMap map[string]string) string {
|
||||||
|
|
||||||
// check if we've got quoted values or possible escapes
|
// check if we've got quoted values or possible escapes
|
||||||
if len(value) > 1 {
|
if len(value) > 1 {
|
||||||
rs := regexp.MustCompile(`\A'(.*)'\z`)
|
singleQuotes := singleQuotesRegex.FindStringSubmatch(value)
|
||||||
singleQuotes := rs.FindStringSubmatch(value)
|
|
||||||
|
|
||||||
rd := regexp.MustCompile(`\A"(.*)"\z`)
|
doubleQuotes := doubleQuotesRegex.FindStringSubmatch(value)
|
||||||
doubleQuotes := rd.FindStringSubmatch(value)
|
|
||||||
|
|
||||||
if singleQuotes != nil || doubleQuotes != nil {
|
if singleQuotes != nil || doubleQuotes != nil {
|
||||||
// pull the quotes off the edges
|
// pull the quotes off the edges
|
||||||
|
@ -286,7 +292,6 @@ func parseValue(value string, envMap map[string]string) string {
|
||||||
|
|
||||||
if doubleQuotes != nil {
|
if doubleQuotes != nil {
|
||||||
// expand newlines
|
// expand newlines
|
||||||
escapeRegex := regexp.MustCompile(`\\.`)
|
|
||||||
value = escapeRegex.ReplaceAllStringFunc(value, func(match string) string {
|
value = escapeRegex.ReplaceAllStringFunc(value, func(match string) string {
|
||||||
c := strings.TrimPrefix(match, `\`)
|
c := strings.TrimPrefix(match, `\`)
|
||||||
switch c {
|
switch c {
|
||||||
|
@ -299,8 +304,7 @@ func parseValue(value string, envMap map[string]string) string {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
// unescape characters
|
// unescape characters
|
||||||
e := regexp.MustCompile(`\\([^$])`)
|
value = unescapeCharsRegex.ReplaceAllString(value, "$1")
|
||||||
value = e.ReplaceAllString(value, "$1")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if singleQuotes == nil {
|
if singleQuotes == nil {
|
||||||
|
@ -311,11 +315,11 @@ func parseValue(value string, envMap map[string]string) string {
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
|
|
||||||
func expandVariables(v string, m map[string]string) string {
|
var expandVarRegex = regexp.MustCompile(`(\\)?(\$)(\()?\{?([A-Z0-9_]+)?\}?`)
|
||||||
r := regexp.MustCompile(`(\\)?(\$)(\()?\{?([A-Z0-9_]+)?\}?`)
|
|
||||||
|
|
||||||
return r.ReplaceAllStringFunc(v, func(s string) string {
|
func expandVariables(v string, m map[string]string) string {
|
||||||
submatch := r.FindStringSubmatch(s)
|
return expandVarRegex.ReplaceAllStringFunc(v, func(s string) string {
|
||||||
|
submatch := expandVarRegex.FindStringSubmatch(s)
|
||||||
|
|
||||||
if submatch == nil {
|
if submatch == nil {
|
||||||
return s
|
return s
|
||||||
|
|
Loading…
Reference in New Issue