pub struct Command { /* private fields */ }
Expand description
Wrap of std::process::command and escalate privileges while executing
Implementations§
Source§impl Command
The implementation of state check and elevated executing varies on each platform
impl Command
The implementation of state check and elevated executing varies on each platform
Sourcepub fn is_elevated() -> bool
pub fn is_elevated() -> bool
Check the state the current program running
Return true
if the program is running as root, otherwise false
§Examples
use elevated_command::Command;
fn main() {
let is_elevated = Command::is_elevated();
}
Sourcepub fn output(&self) -> Result<Output>
pub fn output(&self) -> Result<Output>
Prompting the user with a graphical OS dialog for the root password, excuting the command with escalated privileges, and return the output
§Examples
use elevated_command::Command;
use std::process::Command as StdCommand;
fn main() {
let mut cmd = StdCommand::new("path to the application");
let elevated_cmd = Command::new(cmd);
let output = elevated_cmd.output().unwrap();
}
Source§impl Command
Command initialization shares the same logic across all the platforms
impl Command
Command initialization shares the same logic across all the platforms
Sourcepub fn new(cmd: StdCommand) -> Self
pub fn new(cmd: StdCommand) -> Self
Constructs a new Command
from a std::process::Command
instance, it would read the following configuration from
the instance while executing:
- The instance’s path to the program
- The instance’s arguments
- The instance’s environment variables
So far, the new Command
would only take the environment variables explicitly
set by std::process::Command::env and std::process::Command::env,
without the ones inherited from the parent process
And the environment variables would only be taken on Linux and MacOS, they would be ignored on Windows
Current working directory would be the following while executing the command:
- %SystemRoot%\System32 on Windows
- /root on Linux
- $TMPDIR/sudo_prompt_applet/applet.app/Contents/MacOS on MacOS
To pass environment variables on Windows, to inherit environment variables from the parent process and to change the working directory will be supported in later versions
§Examples
use elevated_command::Command;
use std::process::Command as StdCommand;
fn main() {
let mut cmd = StdCommand::new("path to the application");
cmd.arg("some arg");
cmd.env("some key", "some value");
let elevated_cmd = Command::new(cmd);
}
Sourcepub fn into_inner(self) -> StdCommand
pub fn into_inner(self) -> StdCommand
Consumes the Take
, returning the wrapped std::process::Command
§Examples
use elevated_command::Command;
use std::process::Command as StdCommand;
fn main() {
let mut cmd = StdCommand::new("path to the application");
let elevated_cmd = Command::new(cmd);
let cmd = elevated_cmd.into_inner();
}
Sourcepub fn get_ref(&self) -> &StdCommand
pub fn get_ref(&self) -> &StdCommand
Gets a mutable reference to the underlying std::process::Command
§Examples
use elevated_command::Command;
use std::process::Command as StdCommand;
fn main() {
let mut cmd = StdCommand::new("path to the application");
let elevated_cmd = Command::new(cmd);
let cmd = elevated_cmd.get_ref();
}
Sourcepub fn get_mut(&mut self) -> &mut StdCommand
pub fn get_mut(&mut self) -> &mut StdCommand
Gets a reference to the underlying std::process::Command
§Examples
use elevated_command::Command;
use std::process::Command as StdCommand;
fn main() {
let mut cmd = StdCommand::new("path to the application");
let elevated_cmd = Command::new(cmd);
let cmd = elevated_cmd.get_mut();
}
Sourcepub fn icon(&mut self, icon: Vec<u8>) -> &mut Self
pub fn icon(&mut self, icon: Vec<u8>) -> &mut Self
Set the icon
for the pop-up graphical OS dialog
This method is only applicable on MacOS
§Examples
use elevated_command::Command;
use std::process::Command as StdCommand;
fn main() {
let mut cmd = StdCommand::new("path to the application");
let elevated_cmd = Command::new(cmd);
elevated_cmd.icon(include_bytes!("path to the icon").to_vec());
}
Sourcepub fn name(&mut self, name: String) -> &mut Self
pub fn name(&mut self, name: String) -> &mut Self
Set the name for the pop-up graphical OS dialog
This method is only applicable on MacOS
§Examples
use elevated_command::Command;
use std::process::Command as StdCommand;
fn main() {
let mut cmd = StdCommand::new("path to the application");
let elevated_cmd = Command::new(cmd);
elevated_cmd.name("some name".to_string());
}