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

Alias that returns modified command #5473

Open
wants to merge 65 commits into
base: main
Choose a base branch
from
Open

Alias that returns modified command #5473

wants to merge 65 commits into from

Conversation

anki-code
Copy link
Member

@anki-code anki-code commented Jun 1, 2024

Motivation

After deep dive into How make sudo expand aliases? I understood that we want to modify command before execution but we can't achieve this because all we can is to use callable alias to wrap the command into it. But callable alias is a complex "process wrapper" and using it with subprocess operators has requirements on managing threading, capturing and std manually. Implementing this is high bar for inexperienced users who just want to play with command itself.

Thus we need the way between simple string aliases and callable aliases. I implemented this way as "Alias that returns modified command".

Before

No way to create alias that returns modified command before execution.

After

@aliases.register('xsudo')
@aliases.return_command
def _xsudo(args):
    return ['sudo', '--', *aliases.eval_alias(args), aliases.CUT_ARGS]

aliases['cow'] = 'echo mooo'  

xsudo cow tooo
# Password:
# mooo tooo

xsudo cow tooo | grep m
# mooo tooo

Tag CUT_ARGS is additional tag to have an ability to rid from the tail of arguments. It's needed sometimes in other aliases too (#5452).

Closes #2618 #2893

Notes

  • It's uncomfortable work with aliases object when we have dict aliases for Windows in tests. We need to change it some day: Refactoring: aliases #5452

For community

⬇️ Please click the 👍 reaction instead of leaving a +1 or 👍 comment

@anki-code anki-code marked this pull request as draft June 1, 2024 20:20
@anki-code anki-code changed the title Command Alias Return Command Alias Jun 1, 2024
@anki-code anki-code changed the title Return Command Alias Alias that can modify the command on the fly Jun 1, 2024
@anki-code anki-code changed the title Alias that can modify the command on the fly Alias to modify the command on the fly Jun 1, 2024
@anki-code anki-code marked this pull request as ready for review June 1, 2024 23:35
@anki-code anki-code changed the title Alias to modify the command on the fly Alias that returns modified command Jun 1, 2024
@bennyyip

This comment was marked as off-topic.

@anki-code
Copy link
Member Author

Hey @bennyyip! I moved your comment to the sudo thread with proposal to make a xontrib. Thank you for sharing this!
As you can read first message of this PR the motivation to add "return cmd alias" is more bigger than just solve sudo case. We need a way to modify command before execution and without proxies and wrappers.

@anki-code
Copy link
Member Author

I've changed api to more clear and putted CUT_ARGS to aliases to avoid remembering by jnoortheen ;)
@gforsyth It's ready to review ;)

@anki-code
Copy link
Member Author

anki-code commented Jun 19, 2024

@gforsyth here is the new issue where in fact people want to use commands without wrapping it into the callable alias to avoid capturing/tty issues - #5512 (comment) but now they need to wrap it into the callable alias and facing with questions about managing the TTY.

@anki-code
Copy link
Member Author

@gforsyth here is another one issue where user tries to generate command using python subprocess and have fail with management std - #4792

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

Successfully merging this pull request may close these issues.

Question: how make sudo expand aliases?
3 participants