Skip to content

Commit 33c47ae

Browse files
committed
feat: Added log buffer and get logs
1 parent cdb518e commit 33c47ae

7 files changed

Lines changed: 42 additions & 9 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@codifycli/plugin-core",
3-
"version": "1.1.0-beta20",
3+
"version": "1.1.0-beta23",
44
"description": "TypeScript library for building Codify plugins to manage system resources (applications, CLI tools, settings) through infrastructure-as-code",
55
"main": "dist/index.js",
66
"typings": "dist/index.d.ts",

src/common/errors.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ export class ApplyValidationError extends Error {
44
resourceType: string;
55
resourceName?: string;
66
plan: Plan<any>;
7+
logs: string[];
78

8-
constructor(plan: Plan<any>) {
9+
constructor(plan: Plan<any>, logs: string[] = []) {
910
super(`Failed to apply changes to resource: "${plan.resourceId}". Additional changes are needed to complete apply.\nChanges remaining:\n${ApplyValidationError.prettyPrintPlan(plan)}`);
1011

1112
this.resourceType = plan.coreParameters.type;
1213
this.resourceName = plan.coreParameters.name;
1314
this.plan = plan;
15+
this.logs = logs;
1416
}
1517

1618
private static prettyPrintPlan(plan: Plan<any>): string {

src/messages/handlers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ export class MessageHandler {
174174
errorPayload = {
175175
errorType: 'apply_validation',
176176
message: e.message,
177-
data: { plan: e.plan.toResponse() },
177+
data: { plan: e.plan.toResponse(), logs: e.logs },
178178
};
179179
} else {
180180
const isDebug = process.env.DEBUG?.includes('*') ?? false;

src/plugin/plugin.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,11 @@ export class Plugin {
250250
throw new Error('Malformed plan with resource that cannot be found');
251251
}
252252

253-
await ptyLocalStorage.run(new SequentialPty(), async () => resource.apply(plan))
253+
let applyLogs: string[] = [];
254+
await ptyLocalStorage.run(new SequentialPty(), async () => {
255+
await resource.apply(plan);
256+
applyLogs = getPty().getLogs();
257+
});
254258

255259
// Validate using desired/desired. If the apply was successful, no changes should be reported back.
256260
// Default back desired back to current if it is not defined (for destroys only)
@@ -268,7 +272,7 @@ export class Plugin {
268272
})
269273

270274
if (validationPlan.requiresChanges()) {
271-
throw new ApplyValidationError(validationPlan);
275+
throw new ApplyValidationError(validationPlan, applyLogs);
272276
}
273277
}
274278

src/pty/background-pty.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ export class BackgroundPty implements IPty {
148148
})
149149
}
150150

151+
getLogs(): string[] {
152+
return [];
153+
}
154+
151155
private getDefaultShell(): string {
152156
return process.env.SHELL!;
153157
}

src/pty/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,11 @@ export class SpawnError extends Error {
4444
cmd: string;
4545
exitCode: number;
4646

47-
constructor(cmd: string, exitCode: number, data: string) {
48-
super(`Spawn Error: on command "${cmd}" with exit code: ${exitCode}\nOutput:\n${data}`);
47+
constructor(cmd: string, exitCode: number, data: string, logs?: string[]) {
48+
const logSection = logs?.length
49+
? `\nLast logs:\n${logs.join('\n')}`
50+
: '';
51+
super(`Spawn Error: on command "${cmd}" with exit code: ${exitCode}\nOutput:\n${data}${logSection}`);
4952

5053
this.data = data;
5154
this.cmd = cmd;
@@ -60,6 +63,8 @@ export interface IPty {
6063
spawnSafe(cmd: string | string[], options?: SpawnOptions): Promise<SpawnResult>
6164

6265
kill(): Promise<{ exitCode: number, signal?: number | undefined }>
66+
67+
getLogs(): string[]
6368
}
6469

6570
export function getPty(): IPty {

src/pty/seqeuntial-pty.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,26 @@ const validateSudoRequestResponse = ajv.compile(CommandRequestResponseDataSchema
2828
* without a tty (or even a stdin) attached so interactive commands will not work.
2929
*/
3030
export class SequentialPty implements IPty {
31+
private logBuffer: string[] = [];
32+
private static readonly MAX_LOG_LINES = 30;
33+
34+
getLogs(): string[] {
35+
return [...this.logBuffer];
36+
}
37+
38+
private appendLog(data: string): void {
39+
const lines = data.split('\n');
40+
this.logBuffer.push(...lines);
41+
if (this.logBuffer.length > SequentialPty.MAX_LOG_LINES) {
42+
this.logBuffer = this.logBuffer.slice(-SequentialPty.MAX_LOG_LINES);
43+
}
44+
}
45+
3146
async spawn(cmd: string | string[], options?: SpawnOptions): Promise<SpawnResult> {
3247
const spawnResult = await this.spawnSafe(cmd, options);
3348

3449
if (spawnResult.status !== 'success') {
35-
throw new SpawnError(Array.isArray(cmd) ? cmd.join('\n') : cmd, spawnResult.exitCode, spawnResult.data);
50+
throw new SpawnError(Array.isArray(cmd) ? cmd.join('\n') : cmd, spawnResult.exitCode, spawnResult.data, this.logBuffer);
3651
}
3752

3853
return spawnResult;
@@ -50,7 +65,9 @@ export class SequentialPty implements IPty {
5065
return this.externalSpawn(cmd, options);
5166
}
5267

53-
console.log(`Running command: ${Array.isArray(cmd) ? cmd.join('\\\n') : cmd}` + (options?.cwd ? `(${options?.cwd})` : ''))
68+
const cmdLine = `Running command: ${Array.isArray(cmd) ? cmd.join('\\\n') : cmd}` + (options?.cwd ? `(${options?.cwd})` : '');
69+
console.log(cmdLine);
70+
this.appendLog(cmdLine);
5471

5572
return new Promise((resolve) => {
5673
const output: string[] = [];
@@ -87,6 +104,7 @@ export class SequentialPty implements IPty {
87104
}
88105

89106
output.push(data.toString());
107+
this.appendLog(data.toString());
90108
})
91109

92110
const resizeListener = () => {

0 commit comments

Comments
 (0)