2022-03-26 09:42:19 +00:00
|
|
|
use super::{Context, Module, ModuleConfig};
|
2020-02-26 16:18:19 +00:00
|
|
|
|
|
|
|
use crate::configs::singularity::SingularityConfig;
|
2020-07-07 22:45:32 +00:00
|
|
|
use crate::formatter::StringFormatter;
|
2020-02-26 16:18:19 +00:00
|
|
|
|
|
|
|
/// Creates a module with the current Singularity image
|
|
|
|
///
|
|
|
|
/// Will display the Singularity image if `$SINGULARITY_NAME` is set.
|
|
|
|
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
2021-03-27 17:53:54 +00:00
|
|
|
let singularity_env = context.get_env("SINGULARITY_NAME")?;
|
2020-02-26 16:18:19 +00:00
|
|
|
|
|
|
|
let mut module = context.new_module("singularity");
|
2020-07-07 22:45:32 +00:00
|
|
|
let config: SingularityConfig = SingularityConfig::try_load(module.config);
|
2020-02-26 16:18:19 +00:00
|
|
|
|
2020-07-07 22:45:32 +00:00
|
|
|
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
|
|
|
|
formatter
|
|
|
|
.map_meta(|variable, _| match variable {
|
|
|
|
"symbol" => Some(config.symbol),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.map_style(|variable| match variable {
|
|
|
|
"style" => Some(Ok(config.style)),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.map(|variable| match variable {
|
2021-03-27 17:53:54 +00:00
|
|
|
"env" => Some(Ok(&singularity_env)),
|
2020-07-07 22:45:32 +00:00
|
|
|
_ => None,
|
|
|
|
})
|
2021-11-01 21:18:45 +00:00
|
|
|
.parse(None, Some(context))
|
2020-07-07 22:45:32 +00:00
|
|
|
});
|
2020-02-26 16:18:19 +00:00
|
|
|
|
2020-07-07 22:45:32 +00:00
|
|
|
module.set_segments(match parsed {
|
|
|
|
Ok(segments) => segments,
|
|
|
|
Err(error) => {
|
|
|
|
log::warn!("Error in module `singularity`: \n{}", error);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
});
|
2020-02-26 16:18:19 +00:00
|
|
|
|
|
|
|
Some(module)
|
|
|
|
}
|
2020-08-07 19:13:12 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::test::ModuleRenderer;
|
|
|
|
use ansi_term::Color;
|
|
|
|
|
|
|
|
#[test]
|
2021-02-11 20:08:17 +00:00
|
|
|
fn no_env_set() {
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("singularity").collect();
|
|
|
|
|
|
|
|
let expected = None;
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
#[test]
|
2021-02-11 20:08:17 +00:00
|
|
|
fn env_set() {
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("singularity")
|
|
|
|
.env("SINGULARITY_NAME", "centos.img")
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let expected = Some(format!(
|
|
|
|
"{} ",
|
|
|
|
Color::Blue.bold().dimmed().paint("[centos.img]")
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
}
|