use std::path::Path;
pub fn is_workflow_file(path: &Path) -> bool {
if let Some(file_name) = path.file_name() {
let file_name_str = file_name.to_string_lossy().to_lowercase();
if file_name_str == ".gitlab-ci.yml" || file_name_str.ends_with("gitlab-ci.yml") {
return true;
}
}
if let Some(ext) = path.extension() {
if ext == "yml" || ext == "yaml" {
if let Some(parent) = path.parent() {
return parent.ends_with(".github/workflows") || parent.ends_with("workflows");
} else {
let filename = path
.file_name()
.map(|f| f.to_string_lossy().to_lowercase())
.unwrap_or_default();
return filename.contains("workflow")
|| filename.contains("action")
|| filename.contains("ci")
|| filename.contains("cd");
}
}
}
false
}
pub mod fd {
use nix::fcntl::{open, OFlag};
use nix::sys::stat::Mode;
use nix::unistd::{close, dup, dup2};
use std::io::{self, Result};
use std::os::unix::io::RawFd;
use std::path::Path;
const STDERR_FILENO: RawFd = 2;
pub struct RedirectedStderr {
original_fd: Option<RawFd>,
null_fd: Option<RawFd>,
}
impl RedirectedStderr {
pub fn to_null() -> Result<Self> {
let stderr_backup = match dup(STDERR_FILENO) {
Ok(fd) => fd,
Err(e) => return Err(io::Error::other(e)),
};
let null_fd = match open(Path::new("/dev/null"), OFlag::O_WRONLY, Mode::empty()) {
Ok(fd) => fd,
Err(e) => {
let _ = close(stderr_backup); return Err(io::Error::other(e));
}
};
if let Err(e) = dup2(null_fd, STDERR_FILENO) {
let _ = close(stderr_backup); let _ = close(null_fd);
return Err(io::Error::other(e));
}
Ok(RedirectedStderr {
original_fd: Some(stderr_backup),
null_fd: Some(null_fd),
})
}
}
impl Drop for RedirectedStderr {
fn drop(&mut self) {
if let Some(orig_fd) = self.original_fd.take() {
let _ = dup2(orig_fd, STDERR_FILENO);
let _ = close(orig_fd);
}
if let Some(null_fd) = self.null_fd.take() {
let _ = close(null_fd);
}
}
}
pub fn with_stderr_to_null<F, T>(f: F) -> Result<T>
where
F: FnOnce() -> T,
{
let _redirected = RedirectedStderr::to_null()?;
Ok(f())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_fd_redirection() {
let result = fd::with_stderr_to_null(|| {
eprintln!("This should be redirected to /dev/null");
42
});
assert!(result.is_ok());
assert_eq!(result.unwrap(), 42);
}
}