Skip to content

Commit

Permalink
refactor: Separate module template from recipe module
Browse files Browse the repository at this point in the history
  • Loading branch information
gmpinder committed Jan 28, 2024
1 parent 6e0d1ac commit 32ef3bd
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 83 deletions.
5 changes: 2 additions & 3 deletions Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,11 @@ blue-build-cli-alpine:
DO cargo+SAVE_IMAGE --IMAGE=$IMAGE --TAG=$TAG --LATEST=$LATEST --NIGHTLY=$NIGHTLY --ALPINE=true

installer:
# FROM alpine
FROM mgoltzsche/podman:minimal
FROM alpine
ARG NIGHTLY=false

BUILD +install --BUILD_TARGET="x86_64-unknown-linux-gnu" --NIGHTLY=$NIGHTLY
COPY (+install/bb --BUILD_TARGET="x86_64-unknown-linux-gnu") /out/bb
COPY (+install/bb --BUILD_TARGET="x86_64-unknown-linux-gnu" --NIGHTLY=$NIGHTLY) /out/bb
COPY install.sh /install.sh

CMD ["cat", "/install.sh"]
Expand Down
96 changes: 95 additions & 1 deletion src/commands/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use serde::{Deserialize, Serialize};
use serde_yaml::Value;
use typed_builder::TypedBuilder;

use crate::module_recipe::Recipe;
use crate::module_recipe::{Module, ModuleExt, Recipe};

use super::BlueBuildCommand;

Expand All @@ -25,10 +25,18 @@ pub struct ContainerFileTemplate<'a> {
recipe: &'a Recipe,
recipe_path: &'a Path,

module_template: ModuleTemplate<'a>,

#[builder(default)]
export_script: ExportsTemplate,
}

#[derive(Debug, Clone, Template, TypedBuilder)]
#[template(path = "Containerfile.module", escape = "none")]
pub struct ModuleTemplate<'a> {
modules_list: &'a ModuleExt,
}

#[derive(Debug, Clone, Default, Template)]
#[template(path = "export.sh", escape = "none")]
pub struct ExportsTemplate;
Expand Down Expand Up @@ -65,6 +73,11 @@ impl TemplateCommand {
let template = ContainerFileTemplate::builder()
.recipe(&recipe_de)
.recipe_path(&self.recipe)
.module_template(
ModuleTemplate::builder()
.modules_list(&recipe_de.modules_ext)
.build(),
)
.build();

let output_str = template.render()?;
Expand Down Expand Up @@ -109,3 +122,84 @@ fn running_gitlab_actions() -> bool {

env::var("GITHUB_ACTIONS").is_ok_and(|e| e == "true")
}

fn get_containerfile_list(module: &Module) -> Option<Vec<String>> {
if module.module_type.as_ref()? == "containerfile" {
Some(
module
.config
.get("containerfiles")?
.as_sequence()?
.iter()
.filter_map(|t| Some(t.as_str()?.to_owned()))
.collect(),
)
} else {
None
}
}

fn print_containerfile(containerfile: &str) -> String {
trace!("print_containerfile({containerfile})");
debug!("Loading containerfile contents for {containerfile}");

let path = format!("config/containerfiles/{containerfile}/Containerfile");

let file = fs::read_to_string(&path).unwrap_or_else(|e| {
error!("Failed to read file {path}: {e}");
process::exit(1);
});

trace!("Containerfile contents {path}:\n{file}");

file
}

fn get_module_from_file(file_name: &str) -> String {
trace!("get_module_from_file({file_name})");

let io_err_fn = |e| {
error!("Failed to read module {file_name}: {e}");
process::exit(1);
};

let file_path = PathBuf::from("config").join(file_name);

let file = fs::read_to_string(file_path).unwrap_or_else(io_err_fn);

let serde_err_fn = |e| {
error!("Failed to deserialize module {file_name}: {e}");
process::exit(1);
};

let template_err_fn = |e| {
error!("Failed to render module {file_name}: {e}");
process::exit(1);
};

serde_yaml::from_str::<ModuleExt>(file.as_str()).map_or_else(
|_| {
let module = serde_yaml::from_str::<Module>(file.as_str()).unwrap_or_else(serde_err_fn);

ModuleTemplate::builder()
.modules_list(&ModuleExt::builder().modules(vec![module]).build())
.build()
.render()
.unwrap_or_else(template_err_fn)
},
|module_ext| {
ModuleTemplate::builder()
.modules_list(&module_ext)
.build()
.render()
.unwrap_or_else(template_err_fn)
},
)
}

fn print_module_context(module: &Module) -> String {
serde_json::to_string(module).unwrap_or_else(|e| {
error!("Failed to parse module: {e}");
process::exit(1);
})
}
78 changes: 1 addition & 77 deletions src/module_recipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ impl Recipe {
}
}

#[derive(Serialize, Clone, Deserialize, Debug, Template, TypedBuilder)]
#[template(path = "Containerfile.module", escape = "none")]
#[derive(Serialize, Clone, Deserialize, Debug, TypedBuilder)]
pub struct ModuleExt {
#[builder(default, setter(into))]
pub modules: Vec<Module>,
Expand All @@ -137,78 +136,3 @@ pub struct Module {
// ======================================================== //
// ========================= Helpers ====================== //
// ======================================================== //

fn get_containerfile_list(module: &Module) -> Option<Vec<String>> {
if module.module_type.as_ref()? == "containerfile" {
Some(
module
.config
.get("containerfiles")?
.as_sequence()?
.iter()
.filter_map(|t| Some(t.as_str()?.to_owned()))
.collect(),
)
} else {
None
}
}

fn print_containerfile(containerfile: &str) -> String {
trace!("print_containerfile({containerfile})");
debug!("Loading containerfile contents for {containerfile}");

let path = format!("config/containerfiles/{containerfile}/Containerfile");

let file = fs::read_to_string(&path).unwrap_or_else(|e| {
error!("Failed to read file {path}: {e}");
process::exit(1);
});

trace!("Containerfile contents {path}:\n{file}");

file
}

fn get_module_from_file(file_name: &str) -> String {
trace!("get_module_from_file({file_name})");

let io_err_fn = |e| {
error!("Failed to read module {file_name}: {e}");
process::exit(1);
};

let file_path = PathBuf::from("config").join(file_name);

let file = fs::read_to_string(file_path).unwrap_or_else(io_err_fn);

let serde_err_fn = |e| {
error!("Failed to deserialize module {file_name}: {e}");
process::exit(1);
};

let template_err_fn = |e| {
error!("Failed to render module {file_name}: {e}");
process::exit(1);
};

serde_yaml::from_str::<ModuleExt>(file.as_str()).map_or_else(
|_| {
let module = serde_yaml::from_str::<Module>(file.as_str()).unwrap_or_else(serde_err_fn);

ModuleExt::builder()
.modules(vec![module])
.build()
.render()
.unwrap_or_else(template_err_fn)
},
|module_ext| module_ext.render().unwrap_or_else(template_err_fn),
)
}

fn print_module_context(module: &Module) -> String {
serde_json::to_string(module).unwrap_or_else(|e| {
error!("Failed to parse module: {e}");
process::exit(1);
})
}
2 changes: 1 addition & 1 deletion templates/Containerfile
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ ARG CONFIG_DIRECTORY="/tmp/config"
ARG IMAGE_NAME="{{ recipe.name }}"
ARG BASE_IMAGE="{{ recipe.base_image }}"

{{ recipe.modules_ext }}
{{ module_template }}

RUN rm -rf /tmp/* /var/* && ostree container commit
2 changes: 1 addition & 1 deletion templates/Containerfile.module
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{%- for module in modules %}
{%- for module in modules_list.modules %}
{%- if let Some(type) = module.module_type %}
{%- if type == "containerfile" %}
{%- if let Some(containerfiles) = self::get_containerfile_list(module) %}
Expand Down

0 comments on commit 32ef3bd

Please sign in to comment.