import { DebugBuilder } from "./modules/debugger.mjs"; const log = new DebugBuilder("client", "serviceHandler"); import { exec } from 'child_process'; /** * Executes a system command with error handling. * @param {string} command The command to execute. * @returns {Promise<{ stdout: string, stderr: string }>} A promise resolving to an object containing stdout and stderr. */ const executeCommand = (command) => { return new Promise((resolve, reject) => { exec(command, (error, stdout, stderr) => { if (error) { log.ERROR(`Command failed with error: ${error.message}`); resolve({ stdout, stderr }); } else { resolve({ stdout, stderr }); } }); }); }; /** * Starts the given service from the command line. * @param {string} serviceName The service name to be started. * @returns {Promise} */ export const startService = async (serviceName) => { try { await executeCommand(`sudo systemctl start ${serviceName}.service`); } catch (error) { log.ERROR(`Failed to start service: ${error.message}`); } }; /** * Restarts the given service from the command line. * @param {string} serviceName The service name to be restarted. * @returns {Promise} */ export const restartService = async (serviceName) => { try { await executeCommand(`sudo systemctl restart ${serviceName}.service`); } catch (error) { log.ERROR(`Failed to restart service: ${error.message}`); } }; /** * Stops the given service from the command line. * @param {string} serviceName The service name to be stopped. * @returns {Promise} */ export const stopService = async (serviceName) => { try { await executeCommand(`sudo systemctl stop ${serviceName}.service`); } catch (error) { log.ERROR(`Failed to stop service: ${error.message}`); } };