-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #92 from deploystackio/feat/default-values
feat(env): enhance environment variable resolution with default values
- Loading branch information
Showing
6 changed files
with
145 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,38 @@ | ||
export function parseEnvironmentVariables(environment: Record<string, any> | string[] | undefined): Record<string, string | number> { | ||
const environmentVariables: Record<string, string | number> = {}; | ||
import { resolveEnvironmentValue } from './resolveEnvironmentValue'; | ||
|
||
export function parseEnvironmentVariables( | ||
environment: Record<string, any> | string[] | undefined, | ||
environmentVariables?: Record<string, string> | ||
): Record<string, string | number> { | ||
const environmentResult: Record<string, string | number> = {}; | ||
|
||
if (!environment) { | ||
return environmentVariables; | ||
return environmentResult; | ||
} | ||
|
||
if (Array.isArray(environment)) { | ||
// Handle array format: ["KEY=value", "OTHER_KEY=othervalue"] | ||
environment.forEach(env => { | ||
if (typeof env === 'string' && env.includes('=')) { | ||
const [key, value] = env.split('='); | ||
environmentVariables[key.trim()] = value.trim(); | ||
environmentResult[key.trim()] = resolveEnvironmentValue(value.trim(), environmentVariables); | ||
} | ||
}); | ||
} else { | ||
// Handle object format: { KEY: "value", OTHER_KEY: "othervalue" } | ||
Object.entries(environment).forEach(([key, value]) => { | ||
if (typeof value === 'string' && value.includes('=')) { | ||
const [splitKey, splitValue] = value.split('='); | ||
environmentVariables[splitKey.trim()] = splitValue.trim(); | ||
if (typeof value === 'string') { | ||
if (value.includes('=')) { | ||
const [splitKey, splitValue] = value.split('='); | ||
environmentResult[splitKey.trim()] = resolveEnvironmentValue(splitValue.trim(), environmentVariables); | ||
} else { | ||
environmentResult[key.trim()] = resolveEnvironmentValue(value, environmentVariables); | ||
} | ||
} else { | ||
environmentVariables[key.trim()] = value?.toString() || ''; | ||
environmentResult[key.trim()] = value?.toString() || ''; | ||
} | ||
}); | ||
} | ||
|
||
return environmentVariables; | ||
return environmentResult; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
export function resolveEnvironmentValue( | ||
value: string, | ||
environmentVariables?: Record<string, string> | ||
): string { | ||
|
||
// Check if value uses ${VAR:-default} syntax | ||
const defaultValueMatch = value.match(/\${([^:-]+):-([^}]+)}/); | ||
|
||
if (defaultValueMatch) { | ||
const [, varName, defaultValue] = defaultValueMatch; | ||
|
||
// Check if the variable exists in provided environment variables | ||
if (environmentVariables && varName in environmentVariables) { | ||
return environmentVariables[varName]; | ||
} | ||
|
||
// No environment variable found, use default | ||
return defaultValue; | ||
} | ||
|
||
return value; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
version: '3.3' | ||
networks: | ||
yabin-network: | ||
name: yabin-network | ||
ipam: | ||
driver: default | ||
services: | ||
yabin: | ||
container_name: yabin | ||
ports: | ||
- '${PORT:-3000}:${PORT:-3000}' | ||
image: 'yureien/yabin:latest' | ||
env_file: .env | ||
depends_on: | ||
- db | ||
networks: | ||
- yabin-network | ||
db: | ||
container_name: yabin-db | ||
restart: always | ||
image: postgres:15-alpine | ||
env_file: .env | ||
expose: | ||
- '5432' | ||
environment: | ||
POSTGRES_USER: ${DB_USER:-yabin_user} | ||
POSTGRES_PASSWORD: ${DB_USER_PASS:-123} | ||
POSTGRES_DB: ${DB_NAME:-yabin_db} | ||
POSTGRES_HOST_AUTH_METHOD: "trust" | ||
volumes: | ||
- ./db-data:/var/lib/postgresql/data | ||
networks: | ||
- yabin-network | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
version: '3.3' | ||
|
||
volumes: | ||
db-data: | ||
|
||
services: | ||
typebot-db: | ||
image: postgres:16 | ||
restart: always | ||
volumes: | ||
- db-data:/var/lib/postgresql/data | ||
environment: | ||
- POSTGRES_DB=typebot | ||
- POSTGRES_PASSWORD=typebot | ||
healthcheck: | ||
test: ["CMD-SHELL", "pg_isready -U postgres"] | ||
interval: 5s | ||
timeout: 5s | ||
retries: 5 | ||
typebot-builder: | ||
image: baptistearno/typebot-builder:latest | ||
restart: always | ||
depends_on: | ||
typebot-db: | ||
condition: service_healthy | ||
ports: | ||
- '8080:3000' | ||
extra_hosts: | ||
- 'host.docker.internal:host-gateway' | ||
env_file: .env | ||
|
||
typebot-viewer: | ||
image: baptistearno/typebot-viewer:latest | ||
depends_on: | ||
typebot-db: | ||
condition: service_healthy | ||
restart: always | ||
ports: | ||
- '8081:3000' | ||
env_file: .env | ||
|