use clap::{Arg, Command};
use lazy_static::lazy_static;
use std::env;
use std::path::PathBuf;
pub mod dfcache;
pub mod dfctl;
pub mod dfdaemon;
pub mod dfget;
pub mod dfinit;
pub mod dfstore;
pub const SERVICE_NAME: &str = "dragonfly";
pub const NAME: &str = "client";
pub const CARGO_PKG_VERSION: &str = env!("CARGO_PKG_VERSION");
pub const CARGO_PKG_RUSTC_VERSION: &str = env!("CARGO_PKG_RUST_VERSION");
pub const BUILD_PLATFORM: &str = env!("BUILD_PLATFORM");
pub const BUILD_TIMESTAMP: &str = env!("BUILD_TIMESTAMP");
pub const GIT_COMMIT_SHORT_HASH: &str = {
match option_env!("GIT_COMMIT_SHORT_HASH") {
Some(hash) => hash,
None => "unknown",
}
};
pub const GIT_COMMIT_DATE: &str = {
match option_env!("GIT_COMMIT_DATE") {
Some(hash) => hash,
None => "unknown",
}
};
lazy_static! {
pub static ref INSTANCE_NAME: String = {
if let (Some(pod_namespace), Some(pod_name)) = (
env::var("POD_NAMESPACE").ok(),
env::var("POD_NAME").ok()
) {
format!("{}-{}", pod_namespace, pod_name)
} else {
hostname::get()
.unwrap()
.to_string_lossy()
.to_string()
}
};
}
pub fn default_root_dir() -> PathBuf {
#[cfg(target_os = "linux")]
return PathBuf::from("/var/run/dragonfly/");
#[cfg(target_os = "macos")]
return home::home_dir().unwrap().join(".dragonfly");
}
pub fn default_config_dir() -> PathBuf {
#[cfg(target_os = "linux")]
return PathBuf::from("/etc/dragonfly/");
#[cfg(target_os = "macos")]
return home::home_dir().unwrap().join(".dragonfly").join("config");
}
pub fn default_log_dir() -> PathBuf {
#[cfg(target_os = "linux")]
return PathBuf::from("/var/log/dragonfly/");
#[cfg(target_os = "macos")]
return home::home_dir().unwrap().join(".dragonfly").join("logs");
}
pub fn default_storage_dir() -> PathBuf {
#[cfg(target_os = "linux")]
return PathBuf::from("/var/lib/dragonfly/");
#[cfg(target_os = "macos")]
return home::home_dir().unwrap().join(".dragonfly").join("storage");
}
pub fn default_lock_dir() -> PathBuf {
#[cfg(target_os = "linux")]
return PathBuf::from("/var/lock/dragonfly/");
#[cfg(target_os = "macos")]
return home::home_dir().unwrap().join(".dragonfly");
}
pub fn default_plugin_dir() -> PathBuf {
#[cfg(target_os = "linux")]
return PathBuf::from("/usr/local/lib/dragonfly/plugins/");
#[cfg(target_os = "macos")]
return home::home_dir().unwrap().join(".dragonfly").join("plugins");
}
pub fn default_cache_dir() -> PathBuf {
#[cfg(target_os = "linux")]
return PathBuf::from("/var/cache/dragonfly/");
#[cfg(target_os = "macos")]
return home::home_dir().unwrap().join(".dragonfly").join("cache");
}
#[derive(Debug, Clone)]
pub struct VersionValueParser;
impl clap::builder::TypedValueParser for VersionValueParser {
type Value = bool;
fn parse_ref(
&self,
cmd: &Command,
_arg: Option<&Arg>,
value: &std::ffi::OsStr,
) -> Result<Self::Value, clap::Error> {
if value == std::ffi::OsStr::new("true") {
println!(
"{} {} ({}, {})",
cmd.get_name(),
cmd.get_version().unwrap_or("unknown"),
GIT_COMMIT_SHORT_HASH,
GIT_COMMIT_DATE,
);
std::process::exit(0);
}
Ok(false)
}
}
#[cfg(test)]
mod tests {
use super::*;
use clap::{builder::TypedValueParser, Command};
use std::ffi::OsStr;
#[test]
fn version_value_parser_references_non_real_values() {
let parser = VersionValueParser;
let cmd = Command::new("test_app");
let value = OsStr::new("false");
let result = parser.parse_ref(&cmd, None, value);
assert!(result.is_ok());
assert!(!result.unwrap());
}
}