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

source

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();

}
source

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

source

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);
}
source

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();
}
source

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();
}
source

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();
}
source

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());
}
source

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());
}

Trait Implementations§

source§

impl From<Command> for Command

source§

fn from(cmd: StdCommand) -> Self

Converts from a std::process::Command

It is similiar with the construct method

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.