MCPcopy Create free account
hub / github.com/CodSpeedHQ/codspeed / wrap_with_sudo

Function wrap_with_sudo

src/executor/helpers/run_with_sudo.rs:74–96  ·  view source on GitHub ↗

Wrap with sudo if not running as root

(mut cmd_builder: CommandBuilder)

Source from the content-addressed store, hash-verified

72
73/// Wrap with sudo if not running as root
74pub fn wrap_with_sudo(mut cmd_builder: CommandBuilder) -> Result<CommandBuilder> {
75 if is_root_user() {
76 Ok(cmd_builder)
77 } else if is_sudo_available() {
78 debug!("Wrapping with sudo: {}", cmd_builder.as_command_line());
79 validate_sudo_access()?;
80 cmd_builder.wrap(
81 "sudo",
82 [
83 // Password prompt should not appear here since it has already been validated
84 "--non-interactive",
85 // Forward all environment variables to the command
86 "--preserve-env",
87 ],
88 );
89 Ok(cmd_builder)
90 } else {
91 bail!(
92 "Sudo is not available to run the command: {}",
93 cmd_builder.as_command_line()
94 );
95 }
96}
97
98/// Run a command with sudo after validating sudo access
99pub fn run_with_sudo<S, I, T>(program: S, argv: I) -> Result<()>

Callers 4

runMethod · 0.85
wrap_commandMethod · 0.85
wrap_commandMethod · 0.85
run_with_sudoFunction · 0.85

Calls 4

is_root_userFunction · 0.85
is_sudo_availableFunction · 0.85
validate_sudo_accessFunction · 0.85
wrapMethod · 0.80

Tested by

no test coverage detected