refactor: inefficiency in generated Containerfile

This commit is contained in:
Gerald Pinder 2024-01-01 18:38:39 +00:00
parent 7dd3a8f0f9
commit 938ddae891
3 changed files with 42 additions and 8 deletions

View file

@ -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<String, tera::Value>| -> tera::Result<tera::Value> {
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))
}
}

View file

@ -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"]) %}

8
templates/export.sh Normal file
View file

@ -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)