use once_cell::sync::Lazy;
pub use std::env::*;
use std::path::PathBuf;
pub static PITCHFORK_BIN: Lazy<PathBuf> = Lazy::new(|| {
current_exe()
.and_then(|p| p.canonicalize())
.unwrap_or_else(|e| {
eprintln!("Warning: Could not determine pitchfork binary path: {e}");
args()
.next()
.map(PathBuf::from)
.unwrap_or_else(|| PathBuf::from("pitchfork"))
})
});
pub static CWD: Lazy<PathBuf> = Lazy::new(|| current_dir().unwrap_or_else(|_| PathBuf::from(".")));
pub static HOME_DIR: Lazy<PathBuf> = Lazy::new(|| {
#[cfg(unix)]
if nix::unistd::Uid::effective().is_root()
&& let Ok(sudo_user) = std::env::var("SUDO_USER")
&& let Some(home) = home_dir_for_user(&sudo_user)
{
return home;
}
dirs::home_dir().unwrap_or_else(|| {
eprintln!("Warning: Could not determine home directory");
PathBuf::from("/tmp")
})
});
pub static PITCHFORK_CONFIG_DIR: Lazy<PathBuf> = Lazy::new(|| {
var_path("PITCHFORK_CONFIG_DIR").unwrap_or(HOME_DIR.join(".config").join("pitchfork"))
});
pub static PITCHFORK_GLOBAL_CONFIG_USER: Lazy<PathBuf> =
Lazy::new(|| PITCHFORK_CONFIG_DIR.join("config.toml"));
pub static PITCHFORK_GLOBAL_CONFIG_SYSTEM: Lazy<PathBuf> =
Lazy::new(|| PathBuf::from("/etc/pitchfork/config.toml"));
pub static PITCHFORK_STATE_DIR: Lazy<PathBuf> = Lazy::new(|| {
if let Some(p) = var_path("PITCHFORK_STATE_DIR") {
return p;
}
#[cfg(unix)]
if nix::unistd::Uid::effective().is_root()
&& let Some(home) = configured_supervisor_user_home_dir()
{
return home.join(".local").join("state").join("pitchfork");
}
#[cfg(unix)]
if nix::unistd::Uid::effective().is_root() {
return HOME_DIR.join(".local").join("state").join("pitchfork");
}
dirs::state_dir()
.unwrap_or_else(|| HOME_DIR.join(".local").join("state"))
.join("pitchfork")
});
pub static PITCHFORK_STATE_FILE: Lazy<PathBuf> =
Lazy::new(|| PITCHFORK_STATE_DIR.join("state.toml"));
pub static PITCHFORK_HOSTS_FILE: Lazy<PathBuf> = Lazy::new(|| {
if let Some(p) = var_path("PITCHFORK_HOSTS_FILE") {
return p;
}
if cfg!(windows) {
let system_root = var("SystemRoot").unwrap_or_else(|_| r"C:\Windows".to_string());
PathBuf::from(system_root)
.join("System32")
.join("drivers")
.join("etc")
.join("hosts")
} else {
PathBuf::from("/etc/hosts")
}
});
pub static PITCHFORK_LOG: Lazy<log::LevelFilter> =
Lazy::new(|| var_log_level("PITCHFORK_LOG").unwrap_or(log::LevelFilter::Info));
pub static PITCHFORK_LOG_FILE_LEVEL: Lazy<log::LevelFilter> =
Lazy::new(|| var_log_level("PITCHFORK_LOG_FILE_LEVEL").unwrap_or(*PITCHFORK_LOG));
pub static PITCHFORK_LOGS_DIR: Lazy<PathBuf> =
Lazy::new(|| var_path("PITCHFORK_LOGS_DIR").unwrap_or(PITCHFORK_STATE_DIR.join("logs")));
pub static PITCHFORK_LOG_FILE: Lazy<PathBuf> =
Lazy::new(|| PITCHFORK_LOGS_DIR.join("pitchfork").join("pitchfork.log"));
pub static IPC_SOCK_DIR: Lazy<PathBuf> = Lazy::new(|| PITCHFORK_STATE_DIR.join("sock"));
pub static IPC_SOCK_MAIN: Lazy<PathBuf> = Lazy::new(|| IPC_SOCK_DIR.join("main.sock"));
pub static ORIGINAL_PATH: Lazy<Option<String>> = Lazy::new(|| var("PATH").ok());
pub static IPC_JSON: Lazy<bool> = Lazy::new(|| !var_false("IPC_JSON"));
pub fn expand_tilde(path: impl AsRef<std::path::Path>) -> PathBuf {
expand_tilde_for_user(path, None)
}
pub fn expand_tilde_for_user(path: impl AsRef<std::path::Path>, user: Option<&str>) -> PathBuf {
let path = path.as_ref();
match path.strip_prefix("~") {
Ok(rest) => home_dir_for_effective_user(user).join(rest),
Err(_) => path.to_path_buf(),
}
}
fn var_path(name: &str) -> Option<PathBuf> {
var(name).map(expand_tilde).ok()
}
fn var_log_level(name: &str) -> Option<log::LevelFilter> {
var(name).ok().and_then(|level| level.parse().ok())
}
fn var_false(name: &str) -> bool {
var(name)
.map(|val| val.to_lowercase())
.map(|val| val == "false" || val == "0")
.unwrap_or(false)
}
#[cfg(unix)]
fn home_dir_for_user(username: &str) -> Option<PathBuf> {
nix::unistd::User::from_name(username)
.ok()
.flatten()
.map(|u| u.dir)
}
#[cfg(unix)]
fn home_dir_by_user_spec(user: &str) -> Option<PathBuf> {
if user.chars().all(|c| c.is_ascii_digit()) {
let uid = user.parse::<u32>().ok()?;
nix::unistd::User::from_uid(nix::unistd::Uid::from_raw(uid))
.ok()
.flatten()
.map(|u| u.dir)
} else {
home_dir_for_user(user)
}
}
#[cfg(unix)]
pub(crate) fn home_dir_for_effective_user(user: Option<&str>) -> PathBuf {
let user = user.map(str::trim).filter(|u| !u.is_empty());
match user {
Some(u) => home_dir_by_user_spec(u).unwrap_or_else(|| HOME_DIR.clone()),
None => HOME_DIR.clone(),
}
}
#[cfg(not(unix))]
pub(crate) fn home_dir_for_effective_user(_user: Option<&str>) -> PathBuf {
HOME_DIR.clone()
}
#[cfg(unix)]
fn configured_supervisor_user_home_dir() -> Option<PathBuf> {
let s = crate::settings::settings();
let user = s.supervisor.user.trim();
if user.is_empty() {
return None;
}
home_dir_by_user_spec(user)
}
#[cfg(test)]
mod tests {
use super::*;
use std::path::Path;
#[test]
fn expand_tilde_replaces_home_prefix() {
assert_eq!(
expand_tilde("~/projects/api"),
HOME_DIR.join("projects/api")
);
assert_eq!(expand_tilde("~"), *HOME_DIR);
}
#[test]
fn expand_tilde_leaves_other_paths_unchanged() {
assert_eq!(
expand_tilde("/srv/projects/api"),
Path::new("/srv/projects/api")
);
assert_eq!(expand_tilde("projects/api"), Path::new("projects/api"));
assert_eq!(expand_tilde("~other/api"), Path::new("~other/api"));
}
#[test]
fn expand_tilde_for_user_none_uses_supervisor_home() {
assert_eq!(expand_tilde_for_user("~/data", None), HOME_DIR.join("data"));
}
#[test]
fn expand_tilde_for_user_empty_uses_supervisor_home() {
assert_eq!(
expand_tilde_for_user("~/data", Some("")),
HOME_DIR.join("data")
);
}
#[test]
fn expand_tilde_for_user_nonexistent_falls_back_to_supervisor_home() {
assert_eq!(
expand_tilde_for_user("~/data", Some("nonexistent_user_xyz")),
HOME_DIR.join("data")
);
}
#[test]
fn expand_tilde_for_user_leaves_non_tilde_unchanged() {
assert_eq!(
expand_tilde_for_user("/srv/api", Some("postgres")),
Path::new("/srv/api")
);
}
}