diff --git a/src/template.rs b/src/template.rs index a4f40a0..69a407e 100644 --- a/src/template.rs +++ b/src/template.rs @@ -14,6 +14,7 @@ use typed_builder::TypedBuilder; use crate::module_recipe::Recipe; pub const DEFAULT_CONTAINERFILE: &str = include_str!("../templates/Containerfile.tera"); +pub const EXPORT_SCRIPT: &str = include_str!("../templates/export.sh"); #[derive(Debug, Clone, Args, TypedBuilder)] pub struct TemplateCommand { @@ -119,7 +120,9 @@ impl TemplateCommand { } None => Err("Arg containerfile wasn't a string".into()), }, - None => Err("Needs the argument 'containerfile'".into()), + None => { + Err("Needs the argument 'containerfile' for print_containerfile()".into()) + } } }, ); @@ -134,7 +137,7 @@ impl TemplateCommand { Ok(s) => Ok(s.into()), Err(e) => Err(format!("Unable to serialize: {e}").into()), }, - None => Err("Needs the argument 'module'".into()), + None => Err("Needs the argument 'module' for print_module_context()".into()), } }, ); @@ -162,7 +165,7 @@ impl TemplateCommand { Err(_) => Err(format!("Unable to deserialize file {file}").into()), } } - None => Err("Needs the argument 'file'".into()), + None => Err("Needs the argument 'file' for get_module_from_file()".into()), } }, ); @@ -177,6 +180,32 @@ impl TemplateCommand { }, ); + debug!("Registering function `print_script`"); + tera.register_function( + "print_script", + |args: &HashMap| -> tera::Result { + trace!("tera fn print_script({args:#?})"); + + let escape_script = |script_contents: &str| { + format!( + "\"{}\"", + script_contents + .replace('\n', "\\n") + .replace('\"', "\\\"") + .replace('$', "\\$") + ) + }; + + match args.get("script") { + Some(x) => match x.as_str().unwrap_or_default() { + "export" => Ok(escape_script(EXPORT_SCRIPT).into()), + _ => Err(format!("Script {x} doesn't exist").into()), + }, + None => Err("Needs the argument 'script' for 'print_script()'".into()), + } + }, + ); + Ok((tera, context)) } } diff --git a/templates/Containerfile.tera b/templates/Containerfile.tera index c19f029..0bc6cad 100644 --- a/templates/Containerfile.tera +++ b/templates/Containerfile.tera @@ -29,9 +29,7 @@ COPY --from=ghcr.io/ublue-os/bling:latest /modules /tmp/modules/ # Custom modules overwrite defaults COPY modules /tmp/modules/ -RUN echo "#!/usr/bin/env bash" >> /tmp/exports.sh -RUN echo 'get_yaml_array() { readarray "$1" < <(echo "$3" | yq -I=0 "$2"); }; export -f get_yaml_array' >> /tmp/exports.sh -RUN chmod +x /tmp/exports.sh +RUN printf {{ print_script(script = "export") }} >> /tmp/exports.sh && chmod +x /tmp/exports.sh ARG CONFIG_DIRECTORY="/tmp/config" ARG IMAGE_NAME="{{ name }}" @@ -44,8 +42,7 @@ ARG BASE_IMAGE="{{ base_image }}" {{ print_containerfile(containerfile = c ) }} {%- endfor %} {%- else %} -RUN chmod +x /tmp/modules/{{ module.type }}/{{ module.type }}.sh -RUN source /tmp/exports.sh && OS_VERSION="$(grep -Po '(?<=VERSION_ID=)\d+' /usr/lib/os-release)" /tmp/modules/{{ module.type }}/{{ module.type }}.sh '{{ print_module_context(module = module) }}' +RUN chmod +x /tmp/modules/{{ module.type }}/{{ module.type }}.sh && source /tmp/exports.sh && /tmp/modules/{{ module.type }}/{{ module.type }}.sh '{{ print_module_context(module = module) }}' {%- endif %} {%- elif module["from-file"] %} {%- set extra_module = get_module_from_file(file = module["from-file"]) %} diff --git a/templates/export.sh b/templates/export.sh new file mode 100644 index 0000000..5224501 --- /dev/null +++ b/templates/export.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash + +get_yaml_array() { + readarray "$1" < <(echo "$3" | yq -I=0 "$2") +} + +export -f get_yaml_array +export OS_VERSION=$(grep -Po '(?<=VERSION_ID=)\d+' /usr/lib/os-release)