From 2aa3645e64ad2b16a7c6de9241a8341b70158330 Mon Sep 17 00:00:00 2001 From: hikaru Date: Wed, 7 Feb 2024 09:14:08 -0600 Subject: [PATCH 1/4] feat: snippets --- .gitignore | 1 + src/{commands/mod.rs => commands.rs} | 0 src/commands/template.rs | 29 +++++++++++++++++++++++++++- src/module_recipe.rs | 6 ++++++ src/ops.rs | 4 ++-- templates/Containerfile | 3 +-- 6 files changed, 38 insertions(+), 5 deletions(-) rename src/{commands/mod.rs => commands.rs} (100%) diff --git a/.gitignore b/.gitignore index c714a0e0..d69f2f69 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ # Local testing for bluebuild recipe files /config/ +Containerfile \ No newline at end of file diff --git a/src/commands/mod.rs b/src/commands.rs similarity index 100% rename from src/commands/mod.rs rename to src/commands.rs diff --git a/src/commands/template.rs b/src/commands/template.rs index 7e02fe2b..05b3629a 100644 --- a/src/commands/template.rs +++ b/src/commands/template.rs @@ -154,9 +154,36 @@ fn print_containerfile(containerfile: &str) -> String { file } +fn print_module_snippets(module: &Module) -> String { + trace!("print_module_snippets({module:?})"); + + let snippets = module + .config + .iter() + .filter_map(|s| { + if s.0.as_str() == "snippets" { + Some(s.1.as_sequence()?) + } else { + None + } + }) + .collect::>(); + + snippets + .iter() + .map(|s| { + s.iter() + .filter_map(|s| s.as_str()) + .collect::>() + .join("\n") + }) + .collect::>() + .join("\n") +} + fn print_module_context(module: &Module) -> String { serde_json::to_string(module).unwrap_or_else(|e| { - error!("Failed to parse module: {e}"); + error!("Failed to parse module!!!!!: {e}"); process::exit(1); }) } diff --git a/src/module_recipe.rs b/src/module_recipe.rs index 217912cc..904f7bd7 100644 --- a/src/module_recipe.rs +++ b/src/module_recipe.rs @@ -209,6 +209,11 @@ pub struct ModuleExt { } impl ModuleExt { + /// # Parse a module file returning a [`ModuleExt`] + /// + /// # Errors + /// Can return an `anyhow` Error if the file cannot be read or deserialized + /// into a [`ModuleExt`] pub fn parse_module_from_file(file_name: &str) -> Result { let file_path = PathBuf::from("config").join(file_name); let file_path = if file_path.is_absolute() { @@ -246,6 +251,7 @@ pub struct Module { } impl Module { + #[must_use] pub fn get_modules(modules: &[Self]) -> Vec { modules .iter() diff --git a/src/ops.rs b/src/ops.rs index 3796d359..a0b62dde 100644 --- a/src/ops.rs +++ b/src/ops.rs @@ -34,8 +34,8 @@ pub fn serde_yaml_err(contents: &str) -> impl Fn(serde_yaml::Error) -> SerdeErro contents.to_string(), ( err.into(), - location.map_or(0, |l| l.line()).into(), - location.map_or(0, |l| l.column()).into(), + location.map_or(0, serde_yaml::Location::line).into(), + location.map_or(0, serde_yaml::Location::column).into(), ), ) } diff --git a/templates/Containerfile b/templates/Containerfile index 86959475..4133dba1 100644 --- a/templates/Containerfile +++ b/templates/Containerfile @@ -23,9 +23,7 @@ COPY cosign.pub /usr/share/ublue-os/cosign.pub # Feel free to remove these lines if you want to speed up image builds and don't want any bling COPY --from=ghcr.io/ublue-os/bling:latest /rpms /tmp/bling/rpms COPY --from=ghcr.io/ublue-os/bling:latest /files /tmp/bling/files - COPY --from=docker.io/mikefarah/yq /usr/bin/yq /usr/bin/yq - COPY --from=gcr.io/projectsigstore/cosign /ko-app/cosign /usr/bin/cosign COPY --from=ghcr.io/blue-build/cli: @@ -65,6 +63,7 @@ COPY {{ src }} {{ dest }} {%- endif %} {%- else %} RUN chmod +x /tmp/modules/{{ type }}/{{ type }}.sh && source /tmp/exports.sh && /tmp/modules/{{ type }}/{{ type }}.sh '{{ self::print_module_context(module) }}' +{{ self::print_module_snippets(module) }} {%- endif %} {%- endif %} {%- endfor %} From 90a1ee58fdb3caf0c2d925551819562c451dcc5c Mon Sep 17 00:00:00 2001 From: hikaru Date: Wed, 7 Feb 2024 10:58:57 -0600 Subject: [PATCH 2/4] fix: slim down snippets to just containerfiles --- .gitignore | 2 +- src/commands/template.rs | 43 ++++++++++++---------------------------- templates/Containerfile | 6 +++++- 3 files changed, 19 insertions(+), 32 deletions(-) diff --git a/.gitignore b/.gitignore index d69f2f69..fb22b42a 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,5 @@ .vscode/ # Local testing for bluebuild recipe files -/config/ +/config/* Containerfile \ No newline at end of file diff --git a/src/commands/template.rs b/src/commands/template.rs index 05b3629a..55fc237b 100644 --- a/src/commands/template.rs +++ b/src/commands/template.rs @@ -121,12 +121,12 @@ fn has_cosign_file() -> bool { } #[must_use] -fn get_containerfile_list(module: &Module) -> Option> { - if module.module_type.as_ref()? == "containerfile" { +fn get_module_type_list(module: &Module, typ: &str, list_key: &str) -> Option> { + if module.module_type.as_ref()? == typ { Some( module .config - .get("containerfiles")? + .get(list_key)? .as_sequence()? .iter() .filter_map(|t| Some(t.as_str()?.to_owned())) @@ -137,6 +137,16 @@ fn get_containerfile_list(module: &Module) -> Option> { } } +#[must_use] +fn get_containerfile_list(module: &Module) -> Option> { + get_module_type_list(module, "containerfile", "containerfiles") +} + +#[must_use] +fn get_containerfile_snippets(module: &Module) -> Option> { + get_module_type_list(module, "containerfile", "snippets") +} + #[must_use] fn print_containerfile(containerfile: &str) -> String { debug!("print_containerfile({containerfile})"); @@ -154,33 +164,6 @@ fn print_containerfile(containerfile: &str) -> String { file } -fn print_module_snippets(module: &Module) -> String { - trace!("print_module_snippets({module:?})"); - - let snippets = module - .config - .iter() - .filter_map(|s| { - if s.0.as_str() == "snippets" { - Some(s.1.as_sequence()?) - } else { - None - } - }) - .collect::>(); - - snippets - .iter() - .map(|s| { - s.iter() - .filter_map(|s| s.as_str()) - .collect::>() - .join("\n") - }) - .collect::>() - .join("\n") -} - fn print_module_context(module: &Module) -> String { serde_json::to_string(module).unwrap_or_else(|e| { error!("Failed to parse module!!!!!: {e}"); diff --git a/templates/Containerfile b/templates/Containerfile index 4133dba1..01ba33ce 100644 --- a/templates/Containerfile +++ b/templates/Containerfile @@ -55,6 +55,11 @@ ARG BASE_IMAGE="{{ recipe.base_image }}" {{ self::print_containerfile(c) }} {%- endfor %} {%- endif %} + {%- if let Some(snippets) = self::get_containerfile_snippets(module) %} + {%- for s in snippets %} +{{ s }} + {%- endfor %} + {%- endif %} {%- else if type == "files" %} {%- if let Some(files) = self::get_files_list(module) %} {%- for (src, dest) in files %} @@ -63,7 +68,6 @@ COPY {{ src }} {{ dest }} {%- endif %} {%- else %} RUN chmod +x /tmp/modules/{{ type }}/{{ type }}.sh && source /tmp/exports.sh && /tmp/modules/{{ type }}/{{ type }}.sh '{{ self::print_module_context(module) }}' -{{ self::print_module_snippets(module) }} {%- endif %} {%- endif %} {%- endfor %} From c5fc6a59ff76e94d8b43bff6abd0c822cd9fe40c Mon Sep 17 00:00:00 2001 From: Gerald Pinder Date: Wed, 7 Feb 2024 12:13:41 -0500 Subject: [PATCH 3/4] Apply suggestions from code review --- .gitignore | 2 +- src/commands/template.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index fb22b42a..5764f0a4 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,4 @@ # Local testing for bluebuild recipe files /config/* -Containerfile \ No newline at end of file +Containerfile diff --git a/src/commands/template.rs b/src/commands/template.rs index 55fc237b..22bb7a29 100644 --- a/src/commands/template.rs +++ b/src/commands/template.rs @@ -121,8 +121,8 @@ fn has_cosign_file() -> bool { } #[must_use] -fn get_module_type_list(module: &Module, typ: &str, list_key: &str) -> Option> { - if module.module_type.as_ref()? == typ { +fn get_module_type_list(module: &Module, type: &str, list_key: &str) -> Option> { + if module.module_type.as_ref()? == type { Some( module .config From 8bdc27380ab5e99e9e18b4cd884474d162baa6f4 Mon Sep 17 00:00:00 2001 From: Gerald Pinder Date: Wed, 7 Feb 2024 13:01:56 -0500 Subject: [PATCH 4/4] Update src/commands/template.rs --- src/commands/template.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/commands/template.rs b/src/commands/template.rs index 22bb7a29..55fc237b 100644 --- a/src/commands/template.rs +++ b/src/commands/template.rs @@ -121,8 +121,8 @@ fn has_cosign_file() -> bool { } #[must_use] -fn get_module_type_list(module: &Module, type: &str, list_key: &str) -> Option> { - if module.module_type.as_ref()? == type { +fn get_module_type_list(module: &Module, typ: &str, list_key: &str) -> Option> { + if module.module_type.as_ref()? == typ { Some( module .config