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

Add Context.sudo_scope context manager #999

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

Conversation

Cc618
Copy link

@Cc618 Cc618 commented Jun 15, 2024

As requested in #778, implemented a context manager that allows running multiple commands while changing user.

Usage

with c.sudo_scope():
    c.run("echo toto")
    c.run("whoami")  # root

# Supports `Context.sudo` options
with c.sudo_scope(user='hello'):
    c.run("whoami")  # hello

Implementation details

As prefix, Context.sudo_scope pushes its arguments to a stack Context.command_sudos.
When creating a command, the arguments from the nearest sudo_scope are used to create the command as Context.sudo does.

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

Successfully merging this pull request may close these issues.

1 participant