From 7d20dededb316ab9a8b1285c6f003dc93994636d Mon Sep 17 00:00:00 2001 From: Techassi Date: Thu, 5 Oct 2023 14:47:15 +0200 Subject: [PATCH] Initial commit --- rust/stackablectl/src/main.rs | 2 +- rust/stackablectl/src/output/mod.rs | 115 ++++++++++++++++-- .../src/output/templates/result_empty.tpl | 0 .../src/output/templates/result_success.tpl | 15 +++ 4 files changed, 124 insertions(+), 8 deletions(-) create mode 100644 rust/stackablectl/src/output/templates/result_empty.tpl create mode 100644 rust/stackablectl/src/output/templates/result_success.tpl diff --git a/rust/stackablectl/src/main.rs b/rust/stackablectl/src/main.rs index 7e2d461c..54db4ad9 100644 --- a/rust/stackablectl/src/main.rs +++ b/rust/stackablectl/src/main.rs @@ -42,6 +42,6 @@ async fn main() -> Result<(), Error> { } let output = app.run().await?; - println!("{output}"); + print!("{output}"); Ok(()) } diff --git a/rust/stackablectl/src/output/mod.rs b/rust/stackablectl/src/output/mod.rs index 574c2099..2b67305f 100644 --- a/rust/stackablectl/src/output/mod.rs +++ b/rust/stackablectl/src/output/mod.rs @@ -1,14 +1,115 @@ -use lazy_static::lazy_static; +use snafu::{ResultExt, Snafu}; +use stackable_cockpit::constants::{DEFAULT_OPERATOR_NAMESPACE, DEFAULT_PRODUCT_NAMESPACE}; use tera::Tera; -lazy_static! { - pub static ref RENDERER: Tera = { - let mut renderer = Tera::default(); +pub type Result = std::result::Result; + +#[derive(Debug, Snafu)] +pub enum Error { + #[snafu(display("failed to create output renderer"))] + CreationError { source: tera::Error }, + + #[snafu(display("failed to render console output"))] + RenderError { source: tera::Error }, +} + +pub trait ContextExt { + fn into_context(self) -> tera::Context; +} + +pub struct Context {} + +#[derive(Debug, Default)] +pub struct SuccessContext { + pub used_operator_namespace: String, + pub used_product_namespace: String, - renderer.add_raw_templates(vec![(include_str!("templates/"))]); + pub post_hints: Vec, + pub pre_hints: Vec, + + pub output: String, +} + +impl ContextExt for SuccessContext { + fn into_context(self) -> tera::Context { + let mut ctx = tera::Context::new(); + + ctx.insert("default_operator_namespace", DEFAULT_OPERATOR_NAMESPACE); + ctx.insert("default_product_namespace", DEFAULT_PRODUCT_NAMESPACE); + + ctx.insert("used_operator_namespace", &self.used_operator_namespace); + ctx.insert("used_product_namespace", &self.used_product_namespace); + + ctx.insert("post_hints", &self.post_hints); + ctx.insert("pre_hints", &self.pre_hints); + + ctx.insert("output", &self.output); + + ctx + } +} + +impl SuccessContext { + pub fn new(output: String) -> Self { + Self { + output, + ..Default::default() + } + } + + pub fn add_pre_hint(&mut self, pre_hint: String) -> &mut Self { + self.pre_hints.push(pre_hint); + self + } + + pub fn add_post_hint(&mut self, post_hint: String) -> &mut Self { + self.post_hints.push(post_hint); + self + } +} + +pub struct OutputRenderer { + renderer: Tera, +} + +impl OutputRenderer { + pub fn new() -> Result { + let mut renderer = Tera::default(); renderer - }; + .add_raw_templates(vec![ + ( + "result_success", + include_str!("templates/result_success.tpl"), + ), + ("result_empty", include_str!("templates/result_empty.tpl")), + ]) + .context(CreationSnafu)?; + + Ok(Self { renderer }) + } + + pub fn success(&self, context: SuccessContext) -> Result { + self.renderer + .render("result_success", &context.into_context()) + .context(RenderSnafu) + } } -pub struct OutputRenderer {} +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn simple() { + let renderer = OutputRenderer::new().unwrap(); + let mut context = SuccessContext::new("Eyyy yooo".into()); + + context + .add_pre_hint("This is pre hint number one".into()) + .add_pre_hint("Pre hint number two".into()); + + let out = renderer.success(context).unwrap(); + println!("{out}"); + } +} diff --git a/rust/stackablectl/src/output/templates/result_empty.tpl b/rust/stackablectl/src/output/templates/result_empty.tpl new file mode 100644 index 00000000..e69de29b diff --git a/rust/stackablectl/src/output/templates/result_success.tpl b/rust/stackablectl/src/output/templates/result_success.tpl new file mode 100644 index 00000000..8f3a5040 --- /dev/null +++ b/rust/stackablectl/src/output/templates/result_success.tpl @@ -0,0 +1,15 @@ +{% if pre_hints | length != 0 -%} +{% for pre_hint in pre_hints -%} +{{ pre_hint }} +{% endfor %} +{% endif -%} + +{%- if output | length != 0 %} +{{ output }} +{%- endif %} + +{% if post_hints | length != 0 -%} +{% for post_hint in post_hints -%} +{{ post_hint }} +{% endfor -%} +{% endif -%}