Skip to content

Commit

Permalink
Merge pull request #92 from deploystackio/feat/default-values
Browse files Browse the repository at this point in the history
feat(env): enhance environment variable resolution with default values
  • Loading branch information
Lasim authored Jan 5, 2025
2 parents c2c5676 + b5a0204 commit f13c9a8
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 20 deletions.
13 changes: 11 additions & 2 deletions src/utils/normalizeEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,17 @@ type EnvironmentInput = string[] | { [key: string]: string } | string | undefine
function substituteEnvVariables(value: string, envVariables?: Record<string, string>): string {
if (!envVariables) return value;

return value.replace(/\${([^}]+)}/g, (match, varName) => {
return envVariables[varName] || match;
return value.replace(/\${([^}]+)}/g, (match, p1) => {
// Check if this is a variable with default value
const defaultMatch = p1.match(/([^:-]+):-(.+)/);

if (defaultMatch) {
const [, varName, defaultValue] = defaultMatch;
return envVariables[varName] || defaultValue;
}

// Regular variable without default
return envVariables[p1] || match;
});
}

Expand Down
27 changes: 18 additions & 9 deletions src/utils/parseEnvironmentVariables.ts
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;
}
28 changes: 19 additions & 9 deletions src/utils/processEnvironmentVariablesGeneration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,22 @@ export function processEnvironmentVariablesGeneration(
}

const result = { ...environment };
const imageConfig = config[image.repository];

// Try different repository name formats
const possibleRepoNames = [
image.repository,
`library/${image.repository}`,
`docker.io/library/${image.repository}`,
`docker.io/${image.repository}`
];

let imageConfig;
for (const repoName of possibleRepoNames) {
if (config[repoName]) {
imageConfig = config[repoName];
break;
}
}

if (!imageConfig) {
return result;
Expand All @@ -139,14 +154,9 @@ export function processEnvironmentVariablesGeneration(

const versionConfig = imageConfig.versions[matchingVersion];

// Process each environment variable
for (const [key, value] of Object.entries(result)) {
if (versionConfig.environment[key]) {
// If the value starts with ${ and ends with }, generate a new value
if (typeof value === 'string' && value.startsWith('${') && value.endsWith('}')) {
result[key] = generateValue(versionConfig.environment[key]);
}
}
// Process each environment variable that has a generation rule
for (const [envKey, genConfig] of Object.entries(versionConfig.environment)) {
result[envKey] = generateValue(genConfig);
}

return result;
Expand Down
22 changes: 22 additions & 0 deletions src/utils/resolveEnvironmentValue.ts
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;
}
34 changes: 34 additions & 0 deletions test/docker-compose-files/sample-docker-compose-6.yml
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

41 changes: 41 additions & 0 deletions test/docker-compose-files/sample-docker-compose-7.yml
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

0 comments on commit f13c9a8

Please sign in to comment.