use std::{env, process::Command};
use once_cell::sync::OnceCell;
use serde::Serialize;
pub static ENV_INFO: OnceCell<EnvInfo> = OnceCell::new();
#[allow(dead_code)]
#[derive(Debug, Serialize)]
pub struct EnvInfo {
package: String,
host: String,
rustc: String,
args: Vec<String>,
repo: String,
branch: String,
commit: String,
}
impl EnvInfo {
pub fn register() {
fn command(args: &[&str]) -> String {
let mut output = String::from_utf8(
Command::new(args[0]).args(&args[1..]).output().map(|out| out.stdout).unwrap_or_default(),
)
.unwrap_or_default();
output.pop();
output
}
let rustc_info = command(&["rustc", "--version", "--verbose"]);
let rustc_info = rustc_info.split('\n').map(|line| line.split(": "));
let mut rustc = String::new();
let mut host = String::new();
for mut pair in rustc_info {
let key = pair.next();
let value = pair.next();
match (key, value) {
(Some(key), None) => {
if key.starts_with("rustc ") {
rustc = key.trim_start_matches("rustc ").to_owned();
}
}
(Some(key), Some(value)) => {
if key == "host" {
host = value.to_string();
}
}
_ => {}
}
}
let args = env::args().filter(|arg| !arg.starts_with("APrivateKey")).collect::<Vec<_>>();
let env_info = EnvInfo {
package: env::var("CARGO_PKG_VERSION").unwrap_or_default(),
host,
rustc,
args,
repo: env::var("CARGO_PKG_REPOSITORY").unwrap_or_default(),
branch: command(&["git", "branch", "--show-current"]),
commit: command(&["git", "rev-parse", "HEAD"]),
};
ENV_INFO.set(env_info).unwrap();
}
}