From 5484f83b6dad26c9b07afadf86d1a57b1584193e Mon Sep 17 00:00:00 2001 From: Prashant Shubham Date: Sat, 5 Oct 2024 23:22:47 +0530 Subject: [PATCH] Adding support for generic command execution (#46) --- src/lib/api.ts | 4 +- src/shared/utils/shellUtils.ts | 68 ++++------------------------------ 2 files changed, 9 insertions(+), 63 deletions(-) diff --git a/src/lib/api.ts b/src/lib/api.ts index 2fea0c3..7b16f6a 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -3,12 +3,12 @@ import { SHELL_COMMAND_URL } from '@/shared/constants/apiEndpoints'; export const executeShellCommandOnServer = async ( cmd: string, - cmdOptions: object, + cmdArgs: object, ): Promise => { try { const response = await fetch(`${SHELL_COMMAND_URL}/${cmd}`, { method: 'POST', - body: JSON.stringify(cmdOptions), + body: JSON.stringify(cmdArgs), headers: { 'Content-Type': 'application/json', }, diff --git a/src/shared/utils/shellUtils.ts b/src/shared/utils/shellUtils.ts index cfe5fa5..e691674 100644 --- a/src/shared/utils/shellUtils.ts +++ b/src/shared/utils/shellUtils.ts @@ -9,66 +9,12 @@ export const handleCommand = async ({ command, setOutput }: CommandHandler) => { const [cmd, ...args] = command.split(' '); - switch (cmd.toUpperCase()) { - case 'GET': - if (args.length < 1) { - result = 'Invalid command. Usage: GET key'; - setOutput((prevOutput) => [...prevOutput, newOutput, result]); - return; - } - - try { - const [key] = args; - const cmdOptions = { key: key }; - result = await executeShellCommandOnServer(cmd, cmdOptions); - setOutput((prevOutput) => [...prevOutput, newOutput, result]); - } catch (error: unknown) { - console.error('Error executing command:', error); - result = 'Error executing command'; - return `Error: ${String(error)}`; - } - break; - - case 'SET': - if (args.length === 2) { - const [key, value] = args; - try { - const cmdOptions = { key: key, value: value }; - result = await executeShellCommandOnServer(cmd, cmdOptions); - setOutput((prevOutput) => [...prevOutput, newOutput, result]); - } catch (error: unknown) { - console.error('Error executing command:', error); - result = 'Error executing command'; - setOutput((prevOutput) => [...prevOutput, newOutput, result]); - return `Error: ${String((error as Error).message || error)}`; - } - } else { - result = 'Invalid command. Usage: SET key value'; - setOutput((prevOutput) => [...prevOutput, newOutput, result]); - } - break; - - case 'DEL': - if (args.length <= 1) { - const [keys] = args; - try { - const cmdOptions = { keys: [keys] }; - result = await executeShellCommandOnServer(cmd, cmdOptions); - setOutput((prevOutput) => [...prevOutput, newOutput, result]); - } catch (error: unknown) { - console.error('Error executing command:', error); - result = 'Error executing command'; - setOutput((prevOutput) => [...prevOutput, newOutput, result]); - return `Error: ${String((error as Error).message || error)}`; - } - } else { - result = 'Invalid command. Usage: DEL key1 key2 ....'; - setOutput((prevOutput) => [...prevOutput, newOutput, result]); - } - break; - - default: - result = `Unknown command: ${cmd}`; - setOutput((prevOutput) => [...prevOutput, newOutput, result]); + try { + result = await executeShellCommandOnServer(cmd, args); + setOutput((prevOutput) => [...prevOutput, newOutput, result]); + } catch (error: unknown) { + console.error('Error executing command:', error); + result = 'Error executing command'; + return `Error: ${String(error)}`; } };