first commit

This commit is contained in:
2025-09-17 12:13:02 -04:00
commit e3a5efe56f
25 changed files with 2276 additions and 0 deletions

59
src/config/linux.rs Normal file
View File

@@ -0,0 +1,59 @@
use std::{fs, process};
pub fn configure_linux_service() {
write_service_file("/etc/systemd/system/watchman.service");
if let Ok(o) = process::Command::new("systemctl")
.arg("daemon-reload")
.output()
{
println!("{}", String::from_utf8_lossy(&o.stdout));
if let Ok(o) = process::Command::new("systemctl")
.arg("enable")
.arg("watchman")
.output()
{
println!("{}", String::from_utf8_lossy(&o.stdout));
if let Ok(o) = process::Command::new("systemctl")
.arg("restart")
.arg("watchman")
.output()
{
println!("{}", String::from_utf8_lossy(&o.stdout));
}
}
}
}
pub fn write_service_file<T: ToString>(path: T) {
if whoami::username().eq_ignore_ascii_case("root") {
if fs::write(path.to_string(), service_file()).is_ok() {
println!(
"{}\n ~~~~~~~ SERVICE CONFIG CREATED ~~~~~~~",
service_file()
);
} else {
println!("{}", "COULD NOT WRITE SERVICE CONFIG TO FILE");
}
} else {
println!(
"{}",
"PLEASE RUN AS ROOT - COULD NOT WRITE SERVICE CONFIG TO FILE"
);
}
}
fn service_file() -> &'static str {
r#"
[Unit]
Description=Watchman
[Service]
Type=simple
User=root
Group=root
ExecStart=/root/.cargo/bin/watchman
[Install]
WantedBy=multi-user.target
"#
}

85
src/config/macos.rs Normal file
View File

@@ -0,0 +1,85 @@
use std::{fs, process};
pub fn configure_macos_service() {
write_launch_file("/Library/LaunchDaemons/com.helloimalemur.watchman.plist");
if let Ok(o) = process::Command::new("launchctl")
.arg("load")
.arg("/Library/LaunchDaemons/com.helloimalemur.watchman.plist")
.output()
{
println!("{}", String::from_utf8_lossy(&o.stdout));
if let Ok(o) = process::Command::new("launchctl")
.arg("start")
.arg("com.helloimalemur.watchman")
.output()
{
println!("{}", String::from_utf8_lossy(&o.stdout));
// if let Ok(o) = process::Command::new("systemctl")
// .arg("restart")
// .arg("watchman")
// .output()
// {
// println!("{}", String::from_utf8_lossy(&o.stdout));
// }
}
}
}
pub fn write_launch_file<T: ToString>(path: T) {
if whoami::username().eq_ignore_ascii_case("root") {
if fs::write(path.to_string(), launch_file()).is_ok() {
println!(
"{}\n ~~~~~~~ SERVICE CONFIG CREATED ~~~~~~~",
launch_file()
);
// sudo chown root:wheel /Library/LaunchDaemons/com.yourname.yourapp.plist
// sudo chmod 644 /Library/LaunchDaemons/com.yourname.yourapp.plist
let _ = process::Command::new("chown")
.arg("root:wheel")
.arg(path.to_string())
.spawn();
let _ = process::Command::new("chmod")
.arg("644")
.arg(path.to_string())
.spawn();
} else {
println!("{}", "COULD NOT WRITE SERVICE CONFIG TO FILE");
}
} else {
println!(
"{}",
"PLEASE RUN AS ROOT - COULD NOT WRITE SERVICE CONFIG TO FILE"
);
}
}
fn launch_file() -> &'static str {
r#"
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.helloimalemur.watchman</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/watchman</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>StandardOutPath</key>
<string>/var/log/watchman.log</string>
<key>StandardErrorPath</key>
<string>/var/log/watchman_errors.log</string>
</dict>
</plist>
"#
}

21
src/config/mod.rs Normal file
View File

@@ -0,0 +1,21 @@
use std::env::consts::OS;
use std::process;
use crate::config::linux::{configure_linux_service, write_service_file};
use crate::config::macos::configure_macos_service;
mod linux;
mod macos;
pub fn setup_service() {
match OS {
"linux" => {
configure_linux_service();
}
"macos" => {
configure_macos_service();
}
_ => {
println!("WARNING: not implemented for {}", OS);
}
}
}