1
0

Split in multiple binaries

This commit is contained in:
2023-05-09 08:40:39 +02:00
parent 1ff90d9bcc
commit e27202d63b
5 changed files with 209 additions and 160 deletions

40
src/nixos.rs Normal file
View File

@@ -0,0 +1,40 @@
use std::path::PathBuf;
#[derive(Clone)]
pub struct NixStorePath {
pub hash: String,
pub name: String,
}
impl NixStorePath {
pub fn from_str_symlink(path: &str) -> Result<Self, String> {
Ok(Self::from_path_buf_symlink(PathBuf::from(path))?)
}
pub fn from_path_buf_symlink(path: PathBuf) -> Result<Self, String> {
Ok(Self::from_path_buf(path.read_link().map_err(|err| err.to_string())?)?)
}
pub fn from_path_buf(path: PathBuf) -> Result<Self, String> {
let store_path_name = path.iter().nth(3)
.ok_or_else(|| String::from("Can't read store path name"))?
.to_str()
.ok_or_else(|| String::from("Failed converting store path name to string"))?
.to_string();
Ok(Self::from_store_path_name(store_path_name)?)
}
pub fn from_store_path_name(store_path_name: String) -> Result<Self, String> {
let (hash, name) = store_path_name
.split_once("-")
.ok_or_else(|| String::from("Failed splitting store path name for hash and name"))?;
Ok(Self {
hash: hash.to_string(),
name: name.to_string(),
})
}
pub fn to_prometheus_metric(self, infix: String) -> Result<String, String> {
return Ok(format!("nixos_{}_hash{{hash=\"{}\"}} 1\nnixos_{}_name{{name=\"{}\"}} 1\n", infix, self.hash, infix, self.name));
}
}