Struct Command

Source
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

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

Source§

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>,

Source§

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.