forked from DiceDB/alloy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Code Refactored and modularized (DiceDB#9)
Co-authored-by: root <root@N3XT> Co-authored-by: pshubham <[email protected]>
- Loading branch information
1 parent
f4055a7
commit bd8499c
Showing
9 changed files
with
810 additions
and
45 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,32 @@ | ||
// This is likely to hold api fucntion calls | ||
// src/lib/api.ts | ||
import { CLI_COMMAND_URL } from "@/shared/constants/apiEndpoints"; | ||
|
||
export const executeCLICommandOnServer = async (cmd: string, cmdOptions: object): Promise<string> => { | ||
try { | ||
const response = await fetch(`${CLI_COMMAND_URL}/${cmd}`, { | ||
method: 'POST', | ||
body: JSON.stringify(cmdOptions), | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
|
||
// TODO: This needs to be looked at | ||
const data = await response.json(); | ||
if (Object.prototype.hasOwnProperty.call(data, 'data')) { | ||
return data.data; | ||
} | ||
else if (Object.prototype.hasOwnProperty.call(data, 'error')) { | ||
return data.error; | ||
} | ||
|
||
if (!response.ok) { | ||
throw new Error('Network response was not ok'); | ||
} | ||
|
||
return data; | ||
} catch (error: unknown) { | ||
console.error('Error executing command:', error); | ||
return `Error: ${error}`; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
// This folder will contain or hold constants. For example in this particular file we can store the api endpoints as constants | ||
// This folder will contain or hold constants. For example in this particular file we can store the api endpoints as constants | ||
export const CLI_COMMAND_URL="http://localhost:8080/cli" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// src/shared/utils/cliUtils.ts | ||
|
||
import { executeCLICommandOnServer } from "@/lib/api"; | ||
import { CommandHandler } from "@/types"; | ||
|
||
export const handleCommand = async ({ | ||
command, | ||
setOutput, | ||
}: CommandHandler) => { | ||
const newOutput = `dice > ${command}`; | ||
let result: string; | ||
|
||
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 executeCLICommandOnServer(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 executeCLICommandOnServer(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 executeCLICommandOnServer(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]); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export const formatTime = (seconds: number): string => { | ||
const minutes = Math.floor(seconds / 60); | ||
const remainingSeconds = seconds % 60; | ||
return `${minutes}:${remainingSeconds < 10 ? "0" : ""}${remainingSeconds}`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,6 @@ | ||
// This file will contain the type definitions | ||
import * as React from 'react'; | ||
|
||
export interface CommandHandler { | ||
command: string; | ||
setOutput: React.Dispatch<React.SetStateAction<string[]>>; | ||
} |