Struct runas::Command

source ·
pub struct Command { /* private fields */ }
Expand description

A process builder for elevated execution

Implementations§

source§

impl Command

The Command type acts as a process builder for spawning programs that run in an elevated context.

Example:

use runas::Command;
let status = Command::new("cmd").status();
source

pub fn new<S: AsRef<OsStr>>(program: S) -> Command

Creates a new command type for a given program.

The default configuration is to spawn without arguments, to be visible and to not be launched from a GUI context.

Examples found in repository?
examples/cmd.rs (line 18)
14
15
16
17
18
19
20
21
22
fn main() {
    println!("Starting a root shell:");
    println!(
        "Status: {}",
        runas::Command::new(shell())
            .status()
            .expect("failed to execute")
    );
}
More examples
Hide additional examples
examples/demo.rs (line 24)
20
21
22
23
24
25
26
27
28
29
30
fn main() {
    println!("Running id");
    println!(
        "Status: {}",
        runas::Command::new("id")
            .gui(false)
            .force_prompt(false)
            .status()
            .expect("failed to execute")
    );
}
source

pub fn arg<S: AsRef<OsStr>>(&mut self, arg: S) -> &mut Command

Add an argument to pass to the program.

source

pub fn args<S: AsRef<OsStr>>(&mut self, args: &[S]) -> &mut Command

Add multiple arguments to pass to the program.

source

pub fn show(&mut self, val: bool) -> &mut Command

Controls the visibility of the program on supported platforms. The default is to launch the program visible.

source

pub fn gui(&mut self, val: bool) -> &mut Command

Controls the GUI context. The default behavior is to assume that the program is launched from a command line (not using a GUI). This primarily controls how the elevation prompt is rendered. On some platforms like Windows the elevation prompt is always a GUI element.

If the preferred mode is not available it falls back to the other automatically.

Examples found in repository?
examples/demo.rs (line 25)
20
21
22
23
24
25
26
27
28
29
30
fn main() {
    println!("Running id");
    println!(
        "Status: {}",
        runas::Command::new("id")
            .gui(false)
            .force_prompt(false)
            .status()
            .expect("failed to execute")
    );
}
source

pub fn force_prompt(&mut self, val: bool) -> &mut Command

Can disable the prompt forcing for supported platforms. Mostly this allows sudo on unix platforms to not prompt for a password.

Examples found in repository?
examples/demo.rs (line 26)
20
21
22
23
24
25
26
27
28
29
30
fn main() {
    println!("Running id");
    println!(
        "Status: {}",
        runas::Command::new("id")
            .gui(false)
            .force_prompt(false)
            .status()
            .expect("failed to execute")
    );
}
source

pub fn status(&mut self) -> Result<ExitStatus>

Executes a command as a child process, waiting for it to finish and collecting its exit status.

Examples found in repository?
examples/cmd.rs (line 19)
14
15
16
17
18
19
20
21
22
fn main() {
    println!("Starting a root shell:");
    println!(
        "Status: {}",
        runas::Command::new(shell())
            .status()
            .expect("failed to execute")
    );
}
More examples
Hide additional examples
examples/demo.rs (line 27)
20
21
22
23
24
25
26
27
28
29
30
fn main() {
    println!("Running id");
    println!(
        "Status: {}",
        runas::Command::new("id")
            .gui(false)
            .force_prompt(false)
            .status()
            .expect("failed to execute")
    );
}

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.