Created
August 22, 2017 15:28
-
-
Save mmstick/13c910d3940cd93e340c08d872eb3906 to your computer and use it in GitHub Desktop.
Rusty One-Liners
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
/// Takes a pathname and shortens it for readability. | |
pub fn shorten_path(path: &Path) -> PathBuf { | |
// Attempt to strip the current working directory from the path. | |
path.strip_prefix(&env::current_dir().unwrap()) | |
// If the home directory was split, return a new `PathBuf` with "." as the replacement. | |
.map(|value| PathBuf::from(".").join(value)) | |
// If the current working directory could not be found, attempt to strip the home directory from the path. | |
.unwrap_or(path.strip_prefix(&env::home_dir().unwrap()).ok() | |
// Return the input path if the home directory was not found, otherwise prepend "~" to the path. | |
.map_or_else(|| path.to_path_buf(), |value| PathBuf::from("~").join(value))) | |
} |
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
fn list_dependencies(&self) -> String { | |
Command::new("systemctl").arg("list-dependencies").arg(&self.name).output().ok() | |
// Collect the command's standard output as a `String` and return it as an `Option`. | |
.and_then(|output| String::from_utf8(output.stdout).ok()) | |
// Collect a list of dependencies as a `String`, else return the unit's name. | |
.map_or(self.name.clone(), |stdout| { | |
// Skip the first line of the output | |
stdout.lines().skip(1) | |
// Skip the first four characters of each line | |
.map(|x| x.chars().skip(4).collect::<String>()) | |
// Fold each line into a single `String`. | |
.fold(String::new(), |acc, x| acc + x.as_str() + "\n") | |
}) | |
} | |
fn parse_state(status: &str) -> bool { | |
// The second line contains information pertaining to the state. | |
status.lines().nth(2) | |
// Determines whether the unit is status is active or inactive. | |
.map_or_else(|| false, |active_line| { | |
// Collect the first letter from the status parameter which is either '[a]ctive' or '[i]nactive' | |
active_line.trim().split_at(8).1.chars().next() | |
// If the character is `a` then the status is `[a]ctive`. | |
.map_or(false, |value| value == 'a') | |
}) | |
} | |
fn is_active(&self) -> bool { | |
Command::new("systemctl").arg("status").arg(&self.name).output().ok() | |
// Collect the command's standard output as a `String` and return it as an `Option`. | |
.and_then(|output| String::from_utf8(output.stdout).ok()) | |
// Determine whether the state of the input is active or not. | |
.map_or(false, |stdout| parse_state(stdout.as_str())) | |
} |
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
/// On UNIX systems that feature a `proc` filesystem, if `/proc/self/fd/0` points to a | |
/// location other than `/dev/pts` or `pipe:`, then the standard input has been redirected. | |
/// | |
/// - **/proc/self/fd/0** is the current process's standard input | |
/// - **/proc/self/fd/1** is the current process's standard output | |
/// - **/proc/self/fd/2** is the current process's standard error | |
pub fn input_was_redirected() -> Option<PathBuf> { | |
fs::read_link("/proc/self/fd/0").ok() | |
.and_then(|link| { | |
if !link.to_string_lossy().starts_with("/dev/pts") && | |
!link.to_string_lossy().starts_with("pipe:") | |
{ Some(link) } else { None } | |
}) | |
} | |
fn get_modified_file_time(filename: &str) -> Option<SystemTime> { | |
fs::metadata(filename).ok().and_then( | |
|file| file.modified().ok(), | |
) | |
} | |
fn file_is_newer_than(first: &str, second: &str) -> bool { | |
// Obtain the modified file time of the first file or return FAILED | |
get_modified_file_time(first).map_or(false, |left| { | |
// Obtain the modified file time of the second file or return FAILED | |
get_modified_file_time(second).map_or(false, |right| { | |
// If the first file is newer than the right file, return SUCCESS | |
left > right | |
}) | |
}) | |
} | |
fn file_has_read_permission(filepath: &str) -> bool { | |
// Collect the mode of permissions for the file | |
fs::metadata(filepath).map(|metadata| metadata.permissions().mode()).ok() | |
// If the mode is equal to any of the above, return `SUCCESS` | |
.map_or(false, |mode| mode & 0b100100100 != 0) | |
} | |
fn file_has_write_permission(filepath: &str) -> bool { | |
// Collect the mode of permissions for the file | |
fs::metadata(filepath).map(|metadata| metadata.permissions().mode()).ok() | |
// If the mode is equal to any of the above, return `SUCCESS` | |
.map_or(false, |mode| mode & 0b10010010 != 0) | |
} | |
fn get_dev_and_inode(filename: &str) -> Option<(u64, u64)> { | |
fs::metadata(filename).map(|file| (file.dev(), file.ino())).ok() | |
} | |
fn files_have_same_device_and_inode_numbers(first: &str, second: &str) -> bool { | |
// Obtain the device and inode of the first file or return FAILED | |
get_dev_and_inode(first).map_or(false, |left| { | |
// Obtain the device and inode of the second file or return FAILED | |
get_dev_and_inode(second).map_or(false, |right| { | |
// Compare the device and inodes of the first and second files | |
left == right | |
}) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment