Skip to content

Commit

Permalink
Merge branch 'main' into fix-rebase-upgrade-not-using-cache
Browse files Browse the repository at this point in the history
  • Loading branch information
gmpinder authored Feb 10, 2024
2 parents 81ea6af + 6b4c86f commit 6f2c628
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 42 deletions.
22 changes: 13 additions & 9 deletions src/commands/bug_report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,18 @@ impl BugReportCommand {
}

fn get_recipe(&self) -> Option<Recipe> {
let recipe_path = if let Some(recipe_path) = self.recipe_path.clone() {
recipe_path
} else if let Ok(recipe) = get_config_file("recipe", "Enter path to recipe file") {
recipe
} else {
trace!("Failed to get recipe");
String::new()
};
let recipe_path = self.recipe_path.clone().map_or_else(
|| {
get_config_file("recipe", "Enter path to recipe file").map_or_else(
|_| {
trace!("Failed to get recipe");
String::new()
},
|recipe| recipe,
)
},
|recipe_path| recipe_path,
);

Recipe::parse(&recipe_path).ok()
}
Expand Down Expand Up @@ -263,7 +267,7 @@ fn get_shell_info() -> ShellInfo {

ShellInfo {
version,
name: current_shell.to_string(),
name: current_shell,
}
}

Expand Down
19 changes: 9 additions & 10 deletions src/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ impl BuildCommand {
} else {
tags.first()
.map(|t| format!("{image_name}:{t}"))
.unwrap_or(image_name.to_string())
.unwrap_or_else(|| image_name.to_string())
};
debug!("Full tag is {first_image_name}");

Expand Down Expand Up @@ -243,7 +243,7 @@ impl BuildCommand {
debug!("Pushing is enabled");

let credentials =
credentials.ok_or(anyhow!("Should have checked for creds earlier"))?;
credentials.ok_or_else(|| anyhow!("Should have checked for creds earlier"))?;

push_images_podman_api(&tags, &image_name, &first_image_name, &client, &credentials)
.await?;
Expand Down Expand Up @@ -301,7 +301,7 @@ impl BuildCommand {

let credentials = self
.get_login_creds()
.ok_or(anyhow!("Unable to get credentials"))?;
.ok_or_else(|| anyhow!("Unable to get credentials"))?;

let (registry, username, password) = (
credentials.registry,
Expand Down Expand Up @@ -429,7 +429,7 @@ impl BuildCommand {
} else {
tags.first()
.map(|t| format!("{image_name}:{t}"))
.unwrap_or(image_name.to_string())
.unwrap_or_else(|| image_name.to_string())
};

info!("Building image {full_image}");
Expand Down Expand Up @@ -587,7 +587,7 @@ fn sign_images(image_name: &str, tag: Option<&str>) -> Result<()> {
let image_digest = get_image_digest(image_name, tag)?;
let image_name_tag = tag
.map(|t| format!("{image_name}:{t}"))
.unwrap_or(image_name.to_owned());
.unwrap_or_else(|| image_name.to_owned());

match (
env::var("CI_DEFAULT_BRANCH"),
Expand Down Expand Up @@ -695,11 +695,10 @@ fn sign_images(image_name: &str, tag: Option<&str>) -> Result<()> {
fn get_image_digest(image_name: &str, tag: Option<&str>) -> Result<String> {
trace!("get_image_digest({image_name}, {tag:?})");

let image_url = if let Some(tag) = tag {
format!("docker://{image_name}:{tag}")
} else {
format!("docker://{image_name}")
};
let image_url = tag.map_or_else(
|| format!("docker://{image_name}"),
|tag| format!("docker://{image_name}:{tag}"),
);

trace!("skopeo inspect --format='{{.Digest}}' {image_url}");
let image_digest = String::from_utf8(
Expand Down
8 changes: 7 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
//! The root library for blue-build.
#![warn(clippy::correctness, clippy::suspicious, clippy::perf, clippy::style)]
#![warn(
clippy::correctness,
clippy::suspicious,
clippy::perf,
clippy::style,
clippy::nursery
)]
#![doc = include_str!("../README.md")]
#![forbid(unsafe_code)]
#![allow(clippy::module_name_repetitions)]
Expand Down
21 changes: 10 additions & 11 deletions src/module_recipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,21 +214,21 @@ impl 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<ModuleExt> {
pub fn parse_module_from_file(file_name: &str) -> Result<Self> {
let file_path = PathBuf::from("config").join(file_name);
let file_path = if file_path.is_absolute() {
file_path
} else {
std::env::current_dir()?.join(file_path)
};

let file = fs::read_to_string(file_path.clone())?;
let file = fs::read_to_string(file_path)?;

serde_yaml::from_str::<ModuleExt>(&file).map_or_else(
|_| -> Result<ModuleExt> {
serde_yaml::from_str::<Self>(&file).map_or_else(
|_| -> Result<Self> {
let module =
serde_yaml::from_str::<Module>(&file).map_err(ops::serde_yaml_err(&file))?;
Ok(ModuleExt::builder().modules(vec![module]).build())
Ok(Self::builder().modules(vec![module]).build())
},
Ok,
)
Expand Down Expand Up @@ -256,17 +256,16 @@ impl Module {
modules
.iter()
.flat_map(|module| {
if let Some(file_name) = &module.from_file {
match ModuleExt::parse_module_from_file(file_name) {
module.from_file.as_ref().map_or_else(
|| vec![module.clone()],
|file_name| match ModuleExt::parse_module_from_file(file_name) {
Err(e) => {
error!("Failed to get module from {file_name}: {e}");
vec![]
}
Ok(module_ext) => Self::get_modules(&module_ext.modules),
}
} else {
vec![module.clone()]
}
},
)
})
.collect()
}
Expand Down
35 changes: 24 additions & 11 deletions templates/Containerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
# This stage is responsible for holding onto
# your config without copying it directly into
# the final image
FROM scratch as stage-config
COPY ./config /config

# Copy modules
# The default modules are inside ublue-os/bling
# Custom modules overwrite defaults
FROM scratch as stage-modules
COPY --from=ghcr.io/ublue-os/bling:latest /modules /modules
COPY ./modules /modules

# This stage is responsible for holding onto
# exports like the exports.sh
FROM docker.io/alpine as stage-exports
RUN printf {{ self::print_script(export_script) }} >> /exports.sh && chmod +x /exports.sh

FROM {{ recipe.base_image }}:{{ recipe.image_version }}

LABEL org.opencontainers.image.title="{{ recipe.name }}"
Expand Down Expand Up @@ -29,16 +47,6 @@ COPY --from=ghcr.io/blue-build/cli:
latest-installer
{%- endif %} /out/bluebuild /usr/bin/bluebuild

COPY config /tmp/config/

# Copy modules
# The default modules are inside ublue-os/bling
COPY --from=ghcr.io/ublue-os/bling:latest /modules /tmp/modules/
# Custom modules overwrite defaults
COPY modules /tmp/modules/

RUN printf {{ self::print_script(export_script) }} >> /tmp/exports.sh && chmod +x /tmp/exports.sh

ARG CONFIG_DIRECTORY="/tmp/config"
ARG IMAGE_NAME="{{ recipe.name }}"
ARG BASE_IMAGE="{{ recipe.base_image }}"
Expand All @@ -63,7 +71,12 @@ COPY {{ src }} {{ dest }}
{%- endfor %}
{%- endif %}
{%- else %}
RUN chmod +x /tmp/modules/{{ type }}/{{ type }}.sh && source /tmp/exports.sh && /tmp/modules/{{ type }}/{{ type }}.sh '{{ self::print_module_context(module) }}'
RUN \
--mount=type=bind,from=stage-config,src=/config,dst=/tmp/config,rw \
--mount=type=bind,from=stage-modules,src=/modules,dst=/tmp/modules,rw \
--mount=type=bind,from=stage-exports,src=/exports.sh,dst=/tmp/exports.sh \
chmod +x /tmp/modules/{{ type }}/{{ type }}.sh \
&& source /tmp/exports.sh && /tmp/modules/{{ type }}/{{ type }}.sh '{{ self::print_module_context(module) }}'
{%- endif %}
{%- endif %}
{%- endfor %}
Expand Down

0 comments on commit 6f2c628

Please sign in to comment.