Skip to content

Commit

Permalink
add proper support for multiline environment entries
Browse files Browse the repository at this point in the history
  • Loading branch information
flamestro committed Jun 15, 2024
1 parent d3b0c79 commit 4d1e01f
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,36 @@ describe('all', () => {
await snapshot(app, __dirname, 'service_command_unescaped_string');
});

it('should compile service.environment on multiple lines without breaking it', async () => {
const app = new App('Service.Command');

class TestComposition extends Composition {
constructor(id: string, props: CompositionProps) {
super(app, id, props);
new Service(this, 'Service', {
image: 'redis',
environment: {
CONFIG_FILEPATH: "/tmp/config.yml",
CONSOLE_CONFIG_FILE: `|
kafka:
brokers: ["redpanda-0:9092"]
redpanda:
adminApi:
enabled: true
urls: ["http://redpanda-0:9644"]`
},
});
}
}

new TestComposition('Composition', {
version: '3.8',
name: 'composition',
});

await snapshot(app, __dirname, 'service_environment_multi_line');
});

it('should compile service.healthcheck properly', async () => {
const app = new App('Service.Healthcheck');

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: "composition"
services:
Service.CommandCompositionService:
image: "redis"
environment:
CONFIG_FILEPATH: /tmp/config.yml
CONSOLE_CONFIG_FILE: |
kafka:
brokers: ["redpanda-0:9092"]
redpanda:
adminApi:
enabled: true
urls: ["http://redpanda-0:9644"]
147 changes: 80 additions & 67 deletions packages/compose-as-code/src/compiler/compilerUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,96 +3,109 @@ import fs from 'fs';
const INDENTATION_CHARACTER = ' ';

export interface OutputFile {
fileName: string;
outputDir: string;
content: string;
fileName: string;
outputDir: string;
content: string;
}

export const createDirIfNotExisting = (dirname: string) => {
if (!fs.existsSync(dirname)) {
fs.mkdirSync(dirname);
}
if (!fs.existsSync(dirname)) {
fs.mkdirSync(dirname);
}
};

export const compileKeyValuePair = (
key: string,
value: string | number,
indentationDepth: number,
options?: { noQuotes?: boolean }
key: string,
value: string | number,
indentationDepth: number,
options?: { noQuotes?: boolean }
) => {
let result = '';
const quote = options?.noQuotes ? '' : '"';
result += indent(indentationDepth);
result += `${key}: ${value ? `${quote}${value}${quote}` : ''}\n`;
return result;
let result = '';
const quote = options?.noQuotes ? '' : '"';
result += indent(indentationDepth);
result += `${key}: ${value ? `${quote}${value}${quote}` : ''}\n`;
return result;
};

export const indent = (indentationDepth: number) => {
let indentationResult = '';
for (let i = 0; i < indentationDepth; i++) {
indentationResult += INDENTATION_CHARACTER;
}
return indentationResult;
let indentationResult = '';
for (let i = 0; i < indentationDepth; i++) {
indentationResult += INDENTATION_CHARACTER;
}
return indentationResult;
};

export const compileObject = (obj: object, baseIndentationDepth: number) => {
let result = '';
Object.keys(obj).forEach(key => {
const value = obj[key];
if (typeof value === 'number') {
result += `${indent(baseIndentationDepth)}${key}: ${value}\n`;
} else if (Array.isArray(value)) {
result += `${indent(baseIndentationDepth)}${key}: ${compileList(
value,
baseIndentationDepth + 1,
{ asSingleLineList: true }
)}\n`;
} else {
result += `${indent(baseIndentationDepth)}${key}: ${value}\n`;
}
});
return result;
let result = '';
Object.keys(obj).forEach(key => {
const value = obj[key];
if (typeof value === 'number') {
result += `${indent(baseIndentationDepth)}${key}: ${value}\n`;
} else if (Array.isArray(value)) {
result += `${indent(baseIndentationDepth)}${key}: ${compileList(
value,
baseIndentationDepth + 1,
{asSingleLineList: true}
)}\n`;
} else if (typeof value === 'string') {
if (value.includes("\n")) {
const lines = value.split("\n")
lines.forEach((line, index) => {
if(index === 0) {
result += `${indent(baseIndentationDepth)}${key}: ${line}\n`;
} else {
result += ` ${indent(baseIndentationDepth)}${line}\n`;
}
})
} else {
result += `${indent(baseIndentationDepth)}${key}: ${value}\n`;
}
} else {
result += `${indent(baseIndentationDepth)}${key}: ${value}\n`;
}
});
return result;
};

export const compileObjectListWithId = (
list: { id: string }[],
baseIndentationDepth: number
list: { id: string }[],
baseIndentationDepth: number
) => {
let result = '';
list.forEach(entry => {
result += indent(baseIndentationDepth);
result += `- ${entry.id}\n`;
});
return result;
let result = '';
list.forEach(entry => {
result += indent(baseIndentationDepth);
result += `- ${entry.id}\n`;
});
return result;
};

export const compileList = (
list: string[],
baseIndentationDepth: number,
options?: { asSingleLineList?: boolean }
list: string[],
baseIndentationDepth: number,
options?: { asSingleLineList?: boolean }
) => {
let result = '';
let result = '';

if (options?.asSingleLineList) {
result += '[';
list.forEach((entry, index) => {
result += `"${entry}"`;
if (index < list.length - 1) {
result += ', ';
}
});
result += ']';
} else {
list.forEach(entry => {
result += indent(baseIndentationDepth);
result += `- ${entry}\n`;
});
}
return result;
if (options?.asSingleLineList) {
result += '[';
list.forEach((entry, index) => {
result += `"${entry}"`;
if (index < list.length - 1) {
result += ', ';
}
});
result += ']';
} else {
list.forEach(entry => {
result += indent(baseIndentationDepth);
result += `- ${entry}\n`;
});
}
return result;
};

export const writeFile = (config: OutputFile) => {
createDirIfNotExisting(config.outputDir);
const resultFileName = `${config.outputDir}/${config.fileName}.yaml`;
fs.writeFileSync(resultFileName, config.content);
createDirIfNotExisting(config.outputDir);
const resultFileName = `${config.outputDir}/${config.fileName}.yaml`;
fs.writeFileSync(resultFileName, config.content);
};

0 comments on commit 4d1e01f

Please sign in to comment.