generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add warning to the Web5 Rust CLI if they're not running as root (#385)
- Loading branch information
1 parent
d4bf728
commit c391ec8
Showing
5 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
mod dids; | ||
mod test; | ||
mod utils; | ||
mod vcs; | ||
|
||
use clap::{Parser, Subcommand}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#[cfg(test)] | ||
mod tests { | ||
use crate::utils::is_root; | ||
use std::env; | ||
|
||
#[test] | ||
fn test_is_root() { | ||
if cfg!(target_os = "linux") || cfg!(target_os = "macos") { | ||
assert_eq!(is_root(), env::var("USER").unwrap() == "root"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use std::env; | ||
|
||
// ANSI color codes | ||
const YELLOW_COLOR: &str = "\x1b[93m"; | ||
const RESET_COLOR: &str = "\x1b[0m"; | ||
|
||
// Function to check if the current process has root privileges | ||
pub fn is_root() -> bool { | ||
if let Ok(user) = env::var("USER") { | ||
user == "root" | ||
} else { | ||
false | ||
} | ||
} | ||
|
||
// Function to display a warning if not running as root | ||
pub fn warn_if_not_root() { | ||
const WARNING_MESSAGE: &str = | ||
"Warning: This command may require root privileges to function properly."; | ||
|
||
if !is_root() { | ||
eprintln!("\n{}{}{}\n", YELLOW_COLOR, WARNING_MESSAGE, RESET_COLOR); | ||
} | ||
} |