upend/cli/src/common.rs

43 lines
1.2 KiB
Rust

use std::{env::current_exe, path::PathBuf};
use anyhow::{anyhow, Result};
use lazy_static::lazy_static;
use shadow_rs::{is_debug, shadow};
shadow!(build);
pub fn get_resource_path<S: AsRef<str>>(dir: S) -> Result<std::path::PathBuf> {
let base_path = if is_debug() {
let cwd = build::CARGO_MANIFEST_DIR.parse::<PathBuf>()?;
cwd.join("./tmp/resources")
} else {
current_exe()?
.parent()
.ok_or(anyhow!("couldn't locate resource path, binary in root"))?
.join("../share/upend")
};
let result = base_path.join(dir.as_ref());
if result.exists() {
Ok(result)
} else {
Err(anyhow!("Path {result:?} doesn't exist."))
}
}
lazy_static! {
static ref APP_USER_AGENT: String = format!("upend / {}", build::PKG_VERSION);
pub static ref REQWEST_CLIENT: reqwest::blocking::Client = reqwest::blocking::Client::builder()
.user_agent(APP_USER_AGENT.as_str())
.build()
.unwrap();
pub static ref REQWEST_ASYNC_CLIENT: reqwest::Client = reqwest::Client::builder()
.user_agent(APP_USER_AGENT.as_str())
.build()
.unwrap();
}
pub fn get_version() -> &'static str {
option_env!("UPEND_VERSION").unwrap_or("unknown")
}