Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default command option #19

Open
nbulischeck opened this issue Aug 11, 2019 · 4 comments
Open

Default command option #19

nbulischeck opened this issue Aug 11, 2019 · 4 comments

Comments

@nbulischeck
Copy link

nbulischeck commented Aug 11, 2019

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)
@fwkz
Copy link
Owner

fwkz commented Aug 11, 2019

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"?

@nbulischeck
Copy link
Author

nbulischeck commented Aug 11, 2019

@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?

@fwkz
Copy link
Owner

fwkz commented Aug 11, 2019

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. 🤔

@nbulischeck
Copy link
Author

How does the idea of a default command sound?

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants