#sudo #command #user #cross-platform #applications #prompt #elevated

elevated-command

Run a command using sudo, prompting the user with an OS dialog if necessary

4 stable releases

1.1.2 Jan 4, 2024
1.1.1 Jan 3, 2024
1.1.0 Jan 2, 2024
1.0.0 Jan 2, 2024

#2 in #elevated

Download history 123/week @ 2024-07-01 157/week @ 2024-07-08 186/week @ 2024-07-15 184/week @ 2024-07-22 286/week @ 2024-07-29 61/week @ 2024-08-05 285/week @ 2024-08-12 81/week @ 2024-08-19 137/week @ 2024-08-26 277/week @ 2024-09-02 164/week @ 2024-09-09 246/week @ 2024-09-16 130/week @ 2024-09-23 90/week @ 2024-09-30 92/week @ 2024-10-07 197/week @ 2024-10-14

529 downloads per month
Used in 2 crates (via geph5-client)

MIT license

35KB
278 lines

elevated-command

crates.io docs.rs

Run command using sudo, prompting the user with a graphical OS dialog if necessary. Useful for background rust applications or native Tauri apps that need sudo.

Cross-Platform

elevated-command provides a native OS dialog prompt on Windows, Linux and MacOS.

Windows

Linux

macOS

Example

Add the following to your Cargo.toml:

[dependencies]
elevated-command = "1.1"

In your main.rs:

use elevated_command::Command;
use std::process::Command as StdCommand;

fn main() {
    let is_elevated = Command::is_elevated();

    let mut cmd = StdCommand::new("path to the application");
    cmd.arg("some arg");

    let output = if is_elevated {
        cmd.output().unwrap()
    } else {
        let elevated_cmd = Command::new(cmd);
        elevated_cmd.output().unwrap()
    };
}

Note: The application should not be sudo.

To get started using elevated-command, please see the API reference (docs.rs).

Behavior

On Windows, elevated-command will elevate your command using User Account Control (UAC).

On Linux, elevated-command will use pkexec to show the password prompt and run your command.

On MacOS, elevated-command should behave just like the sudo command in the shell.

Reference

  1. jorangreef/sudo-prompt
  2. https://stackoverflow.com/questions/8046097/how-to-check-if-a-process-has-the-administrative-rights/8196291#8196291
  3. https://fix.moe/post/macos-app-authorization

License

Licensed under the MIT license.

Dependencies

~0.1–36MB
~527K SLoC