use std::path::Path;
const KILLSWITCH_FILE_PATH: &str = "/tmp/lightswitch/killswitch";
pub struct KillSwitch {
killswitch_path: String,
ignore_killswitch: bool,
}
impl KillSwitch {
pub fn new(killswitch_path_override: Option<String>, ignore_killswitch: bool) -> Self {
let killswitch_path = match killswitch_path_override {
Some(path) if !path.is_empty() => path,
_ => KILLSWITCH_FILE_PATH.to_string(),
};
KillSwitch {
killswitch_path,
ignore_killswitch,
}
}
pub fn enabled(&self) -> bool {
!self.ignore_killswitch && Path::new(&self.killswitch_path).try_exists().unwrap()
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs::{File, create_dir_all, remove_file};
#[test]
fn test_killswitch_enabled() {
create_dir_all("/tmp/lightswitch").unwrap();
let killswitch = KillSwitch::new(None, false);
assert!(!killswitch.enabled());
File::create(KILLSWITCH_FILE_PATH).unwrap();
assert!(killswitch.enabled());
remove_file(KILLSWITCH_FILE_PATH).unwrap();
}
#[test]
fn test_killswitch_path_override() {
let temp_killswitch_file = tempfile::NamedTempFile::new().unwrap();
let temp_killswitch_path = temp_killswitch_file.path();
let killswitch = KillSwitch::new(
Some(temp_killswitch_path.to_str().expect("").to_string()),
false,
);
assert!(killswitch.enabled());
remove_file(temp_killswitch_path).unwrap();
}
#[test]
fn test_killswitch_ignored() {
let temp_killswitch_file = tempfile::NamedTempFile::new().unwrap();
let temp_killswitch_path = temp_killswitch_file.path();
let killswitch = KillSwitch::new(
Some(temp_killswitch_path.to_str().expect("").to_string()),
true,
);
assert!(!killswitch.enabled());
remove_file(temp_killswitch_path).unwrap();
}
}