/**
* Bootsmaps user-defined scripts for monitoring with dry-run mode.
* @param {Array<Object>} scripts An array of script objects. Each object should have:
* - name: string, the name of the script.
* - input: Object, an object containing input parameters for the script.
* - command: string, the command to execute (e.g., "node script.js").
* - dryRun: boolean, whether to run in dry-run mode (default: false).
*/
function bootstrapScripts(scripts) {
if (!Array.isArray(scripts)) {
console.error("Error: scripts must be an array.");
return;
}
scripts.forEach(script => {
if (typeof script !== 'object' || script === null) {
console.warn("Skipping invalid script: ", script);
return;
}
if (!script.name || typeof script.name !== 'string') {
console.warn("Skipping script, missing name: ", script);
return;
}
if (!script.command || typeof script.command !== 'string') {
console.warn("Skipping script, missing command: ", script);
return;
}
if (script.dryRun !== undefined && typeof script.dryRun !== 'boolean') {
console.warn("Skipping script, invalid dryRun value: ", script);
return;
}
const execute = async () => {
try {
const command = script.command;
const input = script.input;
const dryRun = script.dryRun || false; // Default to false if not specified
console.log(`[Monitoring] Running script: ${script.name} (${command})`);
console.log(`[Monitoring] Input: ${JSON.stringify(input)}`);
if (dryRun) {
console.log("[Monitoring] DRY RUN: Would execute the following command:");
console.log(`[Monitoring] ${command}`);
console.log("[Monitoring] with input:", JSON.stringify(input));
return;
}
const { exec } = require('child_process'); // Node.js specific
exec(command, input, (error, stdout, stderr) => {
if (error) {
console.error(`[Monitoring] Error executing ${script.name}:`, error);
if (stderr) {
console.error(`[Monitoring] stderr: ${stderr}`);
}
} else {
console.log(`[Monitoring] Script ${script.name} executed successfully.`);
if (stdout) {
console.log(`[Monitoring] stdout: ${stdout}`);
}
}
});
} catch (err) {
console.error(`[Monitoring] Error during script execution:`, err);
}
};
execute();
});
}
Add your comment