diff --git a/backend/util-common.ts b/backend/util-common.ts index 71fd4d8b..97f8c8d1 100644 --- a/backend/util-common.ts +++ b/backend/util-common.ts @@ -340,3 +340,51 @@ export function parseDockerPort(input : string, defaultHostname : string = "loca display: display, }; } + +/** + * Finds the value of a variable in the contents of the .env file + * @param key ENV Var to search for + * @param defaultValue Default + * @param fromEnv The contents, not the path + * @returns string value + */ +export function getEnvFileValue(key : string, defaultValue : string, fromEnv : string) { + let pattern = RegExp(`^${key}\\s*[=:]\\s*(?.*(?=\\s#)|.*$)`, "mgi"); + let match = pattern.exec(fromEnv); + let value = defaultValue; + if (match) { + value = match[1].trim(); // remove outer whitespace + // If wrapped in quotes, remove and unescape matching quotes + match = /^(['"])(.*(?.*?)\}|(?\w+))/g; + let match; + while ((match = pattern.exec(value)) !== null) { + pattern.lastIndex = 0; + let name = match.groups?.name; + let defaultValue = ""; + if (!name) { + name = match.groups?.fullname.match(/^\w+/)![0]; + let defaultValueMatch = match.groups?.fullname.match(/-(.*)$/); + if (defaultValueMatch) { + defaultValue = defaultValueMatch[1]; + } + } + let envValue = getEnvFileValue(name!, defaultValue, fromEnv); + value = value.substring(0, match.index) + envValue + value.substring(match.index + match[0].length); + } + return value; +} diff --git a/frontend/src/components/Container.vue b/frontend/src/components/Container.vue index 6993b76d..d19b923f 100644 --- a/frontend/src/components/Container.vue +++ b/frontend/src/components/Container.vue @@ -137,7 +137,7 @@