godotenv icon indicating copy to clipboard operation
godotenv copied to clipboard

Support parsing docker-compose style defaults, e.g. FOO="${FOO:-FOO_ENV_DEFAULT}"

Open charlesritchea opened this issue 4 years ago • 1 comments

The latest docker-compose v2 has a regression where it can't load defaults from .env files, which it could in v1, this is "blamed" on the use godotenv, specifically https://github.com/joho/godotenv/blob/ddf83eb33bbb136f62617a409142b74b91dbcff3/godotenv.go#L330

func expandVariables(v string, m map[string]string) string {
	return expandVarRegex.ReplaceAllStringFunc(v, func(s string) string {
		submatch := expandVarRegex.FindStringSubmatch(s)

		if submatch == nil {
			return s
		}
		if submatch[1] == "\\" || submatch[2] == "(" {
			return submatch[0][1:]
		} else if submatch[4] != "" {
			return m[submatch[4]]
		}
		return s
	})
}

See the discussion here: https://github.com/docker/compose-cli/issues/1900

.env

FOO="${FOO:-FOO_ENV_DEFAULT}"
BAR="${BAR:-BAR_ENV_DEFAULT}"

charlesritchea avatar Aug 13 '21 19:08 charlesritchea

Generally I've been resistant to accepting non-standard syntax that isn't present in the upstream/original ruby library

That said, docker is docker, and a flagship in the go ecosystem so I'd be happy to accept a PR supporting it and cut a fresh release (which I'm way overdue for)

joho avatar Aug 16 '21 22:08 joho