Skip to content

Commit

Permalink
CLI updates
Browse files Browse the repository at this point in the history
CLI updates
  • Loading branch information
enty8080 authored Aug 2, 2024
2 parents 12be25e + e8cfd34 commit e40935a
Show file tree
Hide file tree
Showing 18 changed files with 176 additions and 512 deletions.
131 changes: 32 additions & 99 deletions ghost/core/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,22 @@
SOFTWARE.
"""

import cmd
import sys

from badges import Badges, Tables
from badges.cmd import Cmd

from ghost.core.device import Device


class Console(cmd.Cmd):
class Console(Cmd):
""" Subclass of ghost.core module.
This subclass of ghost.core modules is intended for providing
main Ghost Framework console interface.
"""

def __init__(self) -> None:
super().__init__()
cmd.Cmd.__init__(self)

self.badges = Badges()
self.tables = Tables()

self.devices = {}
self.banner = """%clear%end
super().__init__(
prompt='(%lineghost%end)> '
intro="""%clear%end
.--. .-. .-.
: .--': : .' `.
: : _ : `-. .--. .--.`. .'
Expand All @@ -55,24 +47,9 @@ def __init__(self) -> None:
--=[ %bold%whiteGhost Framework 8.0.0%end
--=[ Developed by EntySec (%linehttps://entysec.com/%end)
"""
)

self.prompt = '(ghost)> '

def do_help(self, _) -> None:
""" Show available commands.
:return None: None
"""

self.tables.print_table("Core Commands", ('Command', 'Description'), *[
('clear', 'Clear terminal window.'),
('connect', 'Connect device.'),
('devices', 'Show connected devices.'),
('disconnect', 'Disconnect device.'),
('exit', 'Exit Ghost Framework.'),
('help', 'Show available commands.'),
('interact', 'Interact with device.')
])
self.devices = {}

def do_exit(self, _) -> None:
""" Exit Ghost Framework.
Expand All @@ -87,26 +64,18 @@ def do_exit(self, _) -> None:

raise EOFError

def do_clear(self, _) -> None:
""" Clear terminal window.
:return None: None
"""

self.badges.print_empty('%clear', end='')

def do_connect(self, address: str) -> None:
def do_connect(self, args: list) -> None:
""" Connect device.
:param str address: device host:port or just host
:param list args: arguments
:return None: None
"""

if not address:
self.badges.print_usage("connect <host>:[port]")
if len(args) < 2:
self.print_usage("connect <host>:[port]")
return

address = address.split(':')
address = args[1].split(':')

if len(address) < 2:
host, port = address[0], 5555
Expand All @@ -123,11 +92,11 @@ def do_connect(self, address: str) -> None:
'device': device
}
})
self.badges.print_empty("")
self.print_empty("")

self.badges.print_information(
self.print_information(
f"Type %greendevices%end to list all connected devices.")
self.badges.print_information(
self.print_information(
f"Type %greeninteract {str(len(self.devices) - 1)}%end "
"to interact this device."
)
Expand All @@ -139,7 +108,7 @@ def do_devices(self, _) -> None:
"""

if not self.devices:
self.badges.print_warning("No devices connected.")
self.print_warning("No devices connected.")
return

devices = []
Expand All @@ -149,88 +118,52 @@ def do_devices(self, _) -> None:
(device, self.devices[device]['host'],
self.devices[device]['port']))

self.tables.print_table("Connected Devices", ('ID', 'Host', 'Port'), *devices)
self.print_table("Connected Devices", ('ID', 'Host', 'Port'), *devices)

def do_disconnect(self, device_id: int) -> None:
def do_disconnect(self, args: list) -> None:
""" Disconnect device.
:param int device_id: device ID
:param list args: arguments
:return None: None
"""

if not device_id:
self.badges.print_usage("disconnect <id>")
if len(args) < 2:
self.print_usage("disconnect <id>")
return

device_id = int(device_id)
device_id = int(args[1])

if device_id not in self.devices:
self.badges.print_error("Invalid device ID!")
self.print_error("Invalid device ID!")
return

self.devices[device_id]['device'].disconnect()
self.devices.pop(device_id)

def do_interact(self, device_id: int) -> None:
def do_interact(self, args: list) -> None:
""" Interact with device.
:param int device_id: device ID
:param list args: arguments
:return None: None
"""

if not device_id:
self.badges.print_usage("interact <id>")
if len(args) < 2:
self.print_usage("interact <id>")
return

device_id = int(device_id)
device_id = int(args[1])

if device_id not in self.devices:
self.badges.print_error("Invalid device ID!")
self.print_error("Invalid device ID!")
return

self.badges.print_process(f"Interacting with device {str(device_id)}...")
self.print_process(f"Interacting with device {str(device_id)}...")
self.devices[device_id]['device'].interact()

def do_EOF(self, _):
""" Catch EOF.
:return None: None
:raises EOFError: EOF error
"""

raise EOFError

def default(self, line: str) -> None:
""" Default unrecognized command handler.
:param str line: line sent
:return None: None
"""

self.badges.print_error(f"Unrecognized command: {line.split()[0]}!")

def emptyline(self) -> None:
""" Do something on empty line.
:return None: None
"""

pass

def shell(self) -> None:
""" Run console shell.
:return None: None
"""

self.badges.print_empty(self.banner)

while True:
try:
cmd.Cmd.cmdloop(self)

except (EOFError, KeyboardInterrupt):
self.badges.print_empty(end='')
break

except Exception as e:
self.badges.print_error("An error occurred: " + str(e) + "!")
self.loop()
Loading

0 comments on commit e40935a

Please sign in to comment.