riposte icon indicating copy to clipboard operation
riposte copied to clipboard

Default command option

Open nbulischeck opened this issue 5 years ago • 4 comments

Would it be possible to add a default command option? I'm running into a situation where I'd like to have a default command case be shelling out to a remote server.

This could be represented as a decorator like:

@shell.default
def shell_out(command: str):
    remote.execute(command)

nbulischeck avatar Aug 11 '19 05:08 nbulischeck

Hello @nbulischeck! What would such default command do? Can you elaborate on your use-case?

So in terms of the user experiance you'd like to just type "foo bar baz" and automagically point it to default_command instead of providing "default_command foo bar baz"?

fwkz avatar Aug 11 '19 18:08 fwkz

@fwkz The default command would run if a user supplied some input that did not match any currently registered commands. My current use case is remote server management where a user would be able to interact with the remote server as if they were using a normal command shell. I believe you got it correct in your example. I mocked up a quick example below.

from os import system
from riposte import Riposte

class Application:
    def __init__(self):
        self.module = None

def exec_required(fn):
    def wrapper(*args, **kwargs):
        if app.module:
            return fn(*args, **kwargs)
    return wrapper

class CustomRiposte(Riposte):
    @property
    def prompt(self):
        if app.module:
            return f"foo:{app.module} > "
        return self._prompt

app = Application()
shell = CustomRiposte(prompt="foo > ")
HISTORY = []

@shell.command("exit")
def exit():
    if app.module:
        app.module = None
    else: quit()

@shell.command("exec")
def execute():
    app.module = "PS"

@shell.command("upload")
def test():
    print("Upload")

@shell.command("history")
def history():
    for entry in HISTORY:
        shell.print(entry)

@shell.command("default")
@exec_required
def default(command: str):
    system(command)
    HISTORY.append(command)

shell.run()

The user first enters execution mode by using exec and then they can run OS commands with default <os_command>. Instead of having the user type default whoami, I would like if they could just type whoami. The default should also not have a "name" so that it doesn't appear in the command list for tab completion.

Edit: As an afterthought, would it be possible to switch between multiple riposte shells?

nbulischeck avatar Aug 11 '19 18:08 nbulischeck

Edit: As an afterthought, would it be possible to switch between multiple riposte shells?

You can easily nest multiple shells, creating "riposte inception". Just remember to give each instance it's own history_file. Dynamically switching from one to another maintaining its session might be tricky.

But something like ProxyRiposte, which would be passing parsed input to the entry-point of some sorts might be something worth to implement. 🤔

fwkz avatar Aug 11 '19 20:08 fwkz

How does the idea of a default command sound?

@shell.default
@exec_required
def default(command: str):
    system(command)
    HISTORY.append(command)

nbulischeck avatar Aug 11 '19 20:08 nbulischeck