Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Techassi committed Oct 5, 2023
1 parent aff75f4 commit 7d20ded
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 8 deletions.
2 changes: 1 addition & 1 deletion rust/stackablectl/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ async fn main() -> Result<(), Error> {
}

let output = app.run().await?;
println!("{output}");
print!("{output}");
Ok(())
}
115 changes: 108 additions & 7 deletions rust/stackablectl/src/output/mod.rs
Original file line number Diff line number Diff line change
@@ -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<T, E = Error> = std::result::Result<T, E>;

#[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<String>,
pub pre_hints: Vec<String>,

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<Self> {
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<String> {
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}");
}
}
Empty file.
15 changes: 15 additions & 0 deletions rust/stackablectl/src/output/templates/result_success.tpl
Original file line number Diff line number Diff line change
@@ -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 -%}

0 comments on commit 7d20ded

Please sign in to comment.