Skip to content

Commit

Permalink
Adding support for generic command execution (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucifercr07 authored Oct 5, 2024
1 parent 18fe135 commit 5484f83
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 63 deletions.
4 changes: 2 additions & 2 deletions src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { SHELL_COMMAND_URL } from '@/shared/constants/apiEndpoints';

export const executeShellCommandOnServer = async (
cmd: string,
cmdOptions: object,
cmdArgs: object,
): Promise<string> => {
try {
const response = await fetch(`${SHELL_COMMAND_URL}/${cmd}`, {
method: 'POST',
body: JSON.stringify(cmdOptions),
body: JSON.stringify(cmdArgs),
headers: {
'Content-Type': 'application/json',
},
Expand Down
68 changes: 7 additions & 61 deletions src/shared/utils/shellUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)}`;
}
};

0 comments on commit 5484f83

Please sign in to comment.