Struct elevated_command::Command
source · pub struct Command { /* private fields */ }Expand description
Wrap of std::process::command and escalate privileges while executing
Implementations§
source§impl Command
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
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
- The instance’s working directory
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());
}