Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance runCommand with error handling and event callbacks #2481

Merged
merged 2 commits into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 27 additions & 16 deletions src/api/CompileTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface ILELibrarySettings {

export namespace CompileTools {
export const NEWLINE = `\r\n`;
export const DID_NOT_RUN = -123;

function expandVariables(variables: Variable) {
for (const key in variables) {
Expand Down Expand Up @@ -48,10 +49,15 @@ export namespace CompileTools {
}
}

interface RunCommandEvents {
writeEvent?: (content: string) => void;
commandConfirm?: (command: string) => Promise<string>;
}

/**
* Execute a command
*/
export async function runCommand(connection: IBMi, options: RemoteCommand, writeEvent?: (content: string) => void): Promise<CommandResult> {
export async function runCommand(connection: IBMi, options: RemoteCommand, events: RunCommandEvents = {}): Promise<CommandResult> {
const config = connection.getConfig();
if (config && connection) {
const cwd = options.cwd;
Expand All @@ -75,26 +81,30 @@ export namespace CompileTools {
variables
);

if (events.commandConfirm) {
commandString = await events.commandConfirm(commandString);
}

if (commandString) {
const commands = commandString.split(`\n`).filter(command => command.trim().length > 0);

if (writeEvent) {
if (events.writeEvent) {
if (options.environment === `ile` && !options.noLibList) {
writeEvent(`Current library: ` + ileSetup.currentLibrary + NEWLINE);
writeEvent(`Library list: ` + ileSetup.libraryList.join(` `) + NEWLINE);
events.writeEvent(`Current library: ` + ileSetup.currentLibrary + NEWLINE);
events.writeEvent(`Library list: ` + ileSetup.libraryList.join(` `) + NEWLINE);
}
if (options.cwd) {
writeEvent(`Working directory: ` + options.cwd + NEWLINE);
events.writeEvent(`Working directory: ` + options.cwd + NEWLINE);
}
writeEvent(`Commands:\n${commands.map(command => `\t${command}\n`).join(``)}` + NEWLINE);
events.writeEvent(`Commands:\n${commands.map(command => `\t${command}\n`).join(``)}` + NEWLINE);
}

const callbacks: StandardIO = writeEvent ? {
const callbacks: StandardIO = events.writeEvent ? {
onStdout: (data) => {
writeEvent(data.toString().replaceAll(`\n`, NEWLINE));
events.writeEvent!(data.toString().replaceAll(`\n`, NEWLINE));
},
onStderr: (data) => {
writeEvent(data.toString().replaceAll(`\n`, NEWLINE));
events.writeEvent!(data.toString().replaceAll(`\n`, NEWLINE));
}
} : {};

Expand Down Expand Up @@ -148,18 +158,19 @@ export namespace CompileTools {

commandResult.command = commandString;
return commandResult;

} else {
return {
code: DID_NOT_RUN,
command: options.command,
stdout: ``,
stderr: `Command execution failed. (No command)`,
};
}
}
else {
throw new Error("Please connect to an IBM i");
}

return {
code: 1,
command: options.command,
stdout: ``,
stderr: `Command execution failed. (Internal)`,
};
}

function buildLibraryList(config: ILELibrarySettings): string[] {
Expand Down
20 changes: 20 additions & 0 deletions src/api/tests/suites/connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Tools } from '../../Tools';
import { CONNECTION_TIMEOUT, disposeConnection, newConnection } from '../connection';
import IBMi from '../../IBMi';
import { getJavaHome } from '../../configuration/DebugConfiguration';
import { CompileTools } from '../../CompileTools';

describe(`connection tests`, {concurrent: true}, () => {
let connection: IBMi
Expand Down Expand Up @@ -218,6 +219,25 @@ describe(`connection tests`, {concurrent: true}, () => {
expect(qtempIndex < qsysincIndex).toBeTruthy();
});

it('runCommand (ILE, variable expansion)', async () => { const config = connection.getConfig();

const result = await CompileTools.runCommand(connection,
{
command: `CRTDTAARA DTAARA(&SCOOBY/TEST) TYPE(*CHAR) LEN(10)`,
environment: `ile`,
env: {'&SCOOBY': `QTEMP`},
},
{
commandConfirm: async (command) => {
expect(command).toBe(`CRTDTAARA DTAARA(QTEMP/TEST) TYPE(*CHAR) LEN(10)`);
return command;
}
}
);

expect(result?.code).toBe(0);
});

it('withTempDirectory and countFiles', async () => { const content = connection.getContent()!;
let temp;

Expand Down
Loading
Loading