# Copyright (c) 2025 Kim Jarvis TPF Software Services S.A. kim.jarvis@tpfsystems.com # This software is licensed under the MIT License. See the LICENSE file for details.#fromreemote.commandimportCommand
[docs]classShell:""" A class to encapsulate the functionality of shell commands in Unix-like operating systems. It allows users to specify a shell command that is executed on all hosts. additional command-line options, and the ability to execute the command with elevated privileges (`sudo`). Attributes: cmd (str): The shell command. guard (bool): If `False` the commands will not be executed. sudo (bool): If `True`, the commands will be executed with `sudo` privileges. su (bool): If `True`, the commands will be executed with `su` privileges. **Examples:** .. code:: python # Execute a shell command on all hosts r = yield Shell("echo Hello World") # The result is available in stdout print(r.cp.stdout) Usage: This class is designed to be used in a generator-based workflow where commands are yielded for execution. Notes: - Commands are constructed based on the `sudo`, and `su` flags. """def__init__(self,cmd:str,guard:bool=True,sudo:bool=False,su:bool=False):self.cmd=cmdself.guard=guardself.sudo=sudoself.su=sudef__repr__(self):return(f"Shell(cmd={self.cmd!r}, "f"guard={self.guard!r}, "f"sudo={self.sudo!r}, su={self.su!r})")defexecute(self):# print(f"{self}")r=yieldCommand(self.cmd,guard=self.guard,sudo=self.sudo,su=self.su)r.changed=True