Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Make use of Reference to ensure typing #313

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 18 additions & 23 deletions process/drivers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,35 +200,30 @@ impl Driver {
let _ = oci_ref; // silence lint

if true {
return Ok(40);
return Ok(41);
}
}

info!("Retrieving OS version from {oci_ref}");

let inspect_opts = GetMetadataOpts::builder()
.image(format!(
"{}/{}",
oci_ref.resolve_registry(),
oci_ref.repository()
))
.tag(oci_ref.tag().unwrap_or("latest"))
.platform(platform)
.build();

let os_version = Self::get_metadata(&inspect_opts)
.and_then(|inspection| {
inspection.get_version().ok_or_else(|| {
miette!(
"Failed to parse version from metadata for {}",
oci_ref.to_string().bold()
)
})
let os_version = Self::get_metadata(
&GetMetadataOpts::builder()
.image(oci_ref)
.platform(platform)
.build(),
)
.and_then(|inspection| {
inspection.get_version().ok_or_else(|| {
miette!(
"Failed to parse version from metadata for {}",
oci_ref.to_string().bold()
)
})
.or_else(|err| {
warn!("Unable to get version via image inspection due to error:\n{err:?}");
get_version_run_image(oci_ref)
})?;
})
.or_else(|err| {
warn!("Unable to get version via image inspection due to error:\n{err:?}");
get_version_run_image(oci_ref)
})?;
trace!("os_version: {os_version}");
Ok(os_version)
}
Expand Down
24 changes: 17 additions & 7 deletions process/drivers/buildah_driver.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{io::Write, process::Stdio};

use blue_build_utils::{cmd, credentials::Credentials};
use colored::Colorize;
use log::{debug, error, info, trace};
use miette::{bail, miette, IntoDiagnostic, Result};
use semver::Version;
Expand Down Expand Up @@ -83,39 +84,48 @@ impl BuildDriver for BuildahDriver {
fn tag(opts: &TagOpts) -> Result<()> {
trace!("BuildahDriver::tag({opts:#?})");

let mut command = cmd!("buildah", "tag", &*opts.src_image, &*opts.dest_image,);
let dest_image_str = opts.dest_image.to_string();

let mut command = cmd!(
"buildah",
"tag",
opts.src_image.to_string(),
&dest_image_str,
);

trace!("{command:?}");
if command.status().into_diagnostic()?.success() {
info!("Successfully tagged {}!", opts.dest_image);
info!("Successfully tagged {}!", dest_image_str.bold().green());
} else {
bail!("Failed to tag image {}", opts.dest_image);
bail!("Failed to tag image {}", dest_image_str.bold().red());
}
Ok(())
}

fn push(opts: &PushOpts) -> Result<()> {
trace!("BuildahDriver::push({opts:#?})");

let image_str = opts.image.to_string();

let command = cmd!(
"buildah",
"push",
format!(
"--compression-format={}",
opts.compression_type.unwrap_or_default()
),
&*opts.image,
&image_str,
);

trace!("{command:?}");
let status = command
.build_status(&opts.image, "Pushing Image")
.build_status(&image_str, "Pushing Image")
.into_diagnostic()?;

if status.success() {
info!("Successfully pushed {}!", opts.image);
info!("Successfully pushed {}!", image_str.bold().green());
} else {
bail!("Failed to push image {}", opts.image);
bail!("Failed to push image {}", image_str.bold().red());
}
Ok(())
}
Expand Down
18 changes: 12 additions & 6 deletions process/drivers/cosign_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use blue_build_utils::{
constants::{COSIGN_PASSWORD, COSIGN_PUB_PATH, COSIGN_YES},
credentials::Credentials,
};
use colored::Colorize;
use log::{debug, trace};
use miette::{bail, miette, Context, IntoDiagnostic, Result};

Expand Down Expand Up @@ -121,27 +122,32 @@ impl SigningDriver for CosignDriver {
}

fn sign(opts: &SignOpts) -> Result<()> {
let image_digest: &str = opts.image.as_ref();
if opts.image.digest().is_none() {
bail!(
"Image ref {} is not a digest ref",
opts.image.to_string().bold().red(),
);
}

let mut command = cmd!(
"cosign",
"sign",
if let Some(ref key) = opts.key => format!("--key={key}"),
"--recursive",
image_digest,
opts.image.to_string(),
COSIGN_PASSWORD => "",
COSIGN_YES => "true",
);

trace!("{command:?}");
if !command.status().into_diagnostic()?.success() {
bail!("Failed to sign {image_digest}");
bail!("Failed to sign {}", opts.image.to_string().bold().red());
}

Ok(())
}

fn verify(opts: &VerifyOpts) -> Result<()> {
let image_name_tag: &str = opts.image.as_ref();
let mut command = cmd!(
"cosign",
"verify",
Expand All @@ -157,12 +163,12 @@ impl SigningDriver for CosignDriver {
),
};
},
image_name_tag
opts.image.to_string(),
);

trace!("{command:?}");
if !command.status().into_diagnostic()?.success() {
bail!("Failed to verify {image_name_tag}");
bail!("Failed to verify {}", opts.image.to_string().bold().red());
}

Ok(())
Expand Down
54 changes: 33 additions & 21 deletions process/drivers/docker_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use blue_build_utils::{
string_vec,
};
use cached::proc_macro::cached;
use colored::Colorize;
use log::{debug, info, trace, warn};
use miette::{bail, miette, IntoDiagnostic, Result};
use once_cell::sync::Lazy;
Expand Down Expand Up @@ -154,31 +155,35 @@ impl BuildDriver for DockerDriver {
fn tag(opts: &TagOpts) -> Result<()> {
trace!("DockerDriver::tag({opts:#?})");

let dest_image_str = opts.dest_image.to_string();

trace!("docker tag {} {}", opts.src_image, opts.dest_image);
let status = cmd!("docker", "tag", &*opts.src_image, &*opts.dest_image,)
let status = cmd!("docker", "tag", opts.src_image.to_string(), &dest_image_str)
.status()
.into_diagnostic()?;

if status.success() {
info!("Successfully tagged {}!", opts.dest_image);
info!("Successfully tagged {}!", dest_image_str.bold().green());
} else {
bail!("Failed to tag image {}", opts.dest_image);
bail!("Failed to tag image {}", dest_image_str.bold().red());
}
Ok(())
}

fn push(opts: &PushOpts) -> Result<()> {
trace!("DockerDriver::push({opts:#?})");

let image_str = opts.image.to_string();

trace!("docker push {}", opts.image);
let status = cmd!("docker", "push", &*opts.image)
let status = cmd!("docker", "push", &image_str)
.status()
.into_diagnostic()?;

if status.success() {
info!("Successfully pushed {}!", opts.image);
info!("Successfully pushed {}!", image_str.bold().green());
} else {
bail!("Failed to push image {}", opts.image);
bail!("Failed to push image {}", image_str.bold().red());
}
Ok(())
}
Expand Down Expand Up @@ -315,18 +320,25 @@ impl BuildDriver for DockerDriver {
],
);

let final_images = match (opts.image.as_deref(), opts.archive_path.as_deref()) {
let final_images = match (opts.image, opts.archive_path.as_deref()) {
(Some(image), None) => {
let images = if opts.tags.is_empty() {
cmd!(command, "-t", image);
let image = image.to_string();
cmd!(command, "-t", &image);
string_vec![image]
} else {
opts.tags.iter().for_each(|tag| {
cmd!(command, "-t", format!("{image}:{tag}"));
cmd!(
command,
"-t",
format!("{}/{}:{tag}", image.resolve_registry(), image.repository())
);
});
opts.tags
.iter()
.map(|tag| format!("{image}:{tag}"))
.map(|tag| {
format!("{}/{}:{tag}", image.resolve_registry(), image.repository())
})
.collect()
};
let first_image = images.first().unwrap();
Expand All @@ -348,8 +360,12 @@ impl BuildDriver for DockerDriver {
images
}
(None, Some(archive_path)) => {
cmd!(command, "--output", format!("type=oci,dest={archive_path}"));
string_vec![archive_path]
cmd!(
command,
"--output",
format!("type=oci,dest={}", archive_path.display())
);
string_vec![archive_path.display().to_string()]
}
(Some(_), Some(_)) => bail!("Cannot use both image and archive path"),
(None, None) => bail!("Need either the image or archive path set"),
Expand Down Expand Up @@ -385,16 +401,12 @@ impl InspectDriver for DockerDriver {
#[cached(
result = true,
key = "String",
convert = r#"{ format!("{}-{:?}-{}", &*opts.image, opts.tag.as_ref(), opts.platform)}"#,
convert = r#"{ format!("{}-{}", opts.image, opts.platform)}"#,
sync_writes = true
)]
fn get_metadata_cache(opts: &GetMetadataOpts) -> Result<ImageMetadata> {
trace!("DockerDriver::get_metadata({opts:#?})");

let url = opts.tag.as_ref().map_or_else(
|| format!("{}", opts.image),
|tag| format!("{}:{tag}", opts.image),
);
let image_str = opts.image.to_string();

let mut command = cmd!(
"docker",
Expand All @@ -409,16 +421,16 @@ fn get_metadata_cache(opts: &GetMetadataOpts) -> Result<ImageMetadata> {
"inspect",
"--format",
"{{json .}}",
&url
&image_str,
);
trace!("{command:?}");

let output = command.output().into_diagnostic()?;

if output.status.success() {
info!("Successfully inspected image {url}!");
info!("Successfully inspected image {}!", image_str.bold().green());
} else {
bail!("Failed to inspect image {url}")
bail!("Failed to inspect image {}", image_str.bold().red())
}

serde_json::from_slice::<metadata::Metadata>(&output.stdout)
Expand Down
38 changes: 19 additions & 19 deletions process/drivers/github_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,55 +230,55 @@ mod test {
setup_default_branch,
None,
string_vec![
format!("{}-40", &*TIMESTAMP),
format!("{}-41", &*TIMESTAMP),
"latest",
&*TIMESTAMP,
format!("{COMMIT_SHA}-40"),
"40",
format!("{COMMIT_SHA}-41"),
"41",
],
)]
#[case::default_branch_alt_tags(
setup_default_branch,
Some(bon::vec![TEST_TAG_1, TEST_TAG_2]),
string_vec![
TEST_TAG_1,
format!("{TEST_TAG_1}-40"),
format!("{}-{TEST_TAG_1}-40", &*TIMESTAMP),
format!("{COMMIT_SHA}-{TEST_TAG_1}-40"),
format!("{TEST_TAG_1}-41"),
format!("{}-{TEST_TAG_1}-41", &*TIMESTAMP),
format!("{COMMIT_SHA}-{TEST_TAG_1}-41"),
TEST_TAG_2,
format!("{TEST_TAG_2}-40"),
format!("{}-{TEST_TAG_2}-40", &*TIMESTAMP),
format!("{COMMIT_SHA}-{TEST_TAG_2}-40"),
format!("{TEST_TAG_2}-41"),
format!("{}-{TEST_TAG_2}-41", &*TIMESTAMP),
format!("{COMMIT_SHA}-{TEST_TAG_2}-41"),
],
)]
#[case::pr_branch(
setup_pr_branch,
None,
string_vec!["pr-12-40", format!("{COMMIT_SHA}-40")],
string_vec!["pr-12-41", format!("{COMMIT_SHA}-41")],
)]
#[case::pr_branch_alt_tags(
setup_pr_branch,
Some(bon::vec![TEST_TAG_1, TEST_TAG_2]),
string_vec![
format!("pr-12-{TEST_TAG_1}-40"),
format!("{COMMIT_SHA}-{TEST_TAG_1}-40"),
format!("pr-12-{TEST_TAG_2}-40"),
format!("{COMMIT_SHA}-{TEST_TAG_2}-40"),
format!("pr-12-{TEST_TAG_1}-41"),
format!("{COMMIT_SHA}-{TEST_TAG_1}-41"),
format!("pr-12-{TEST_TAG_2}-41"),
format!("{COMMIT_SHA}-{TEST_TAG_2}-41"),
],
)]
#[case::branch(
setup_branch,
None,
string_vec![format!("{COMMIT_SHA}-40"), "br-test-40"],
string_vec![format!("{COMMIT_SHA}-41"), "br-test-41"],
)]
#[case::branch_alt_tags(
setup_branch,
Some(bon::vec![TEST_TAG_1, TEST_TAG_2]),
string_vec![
format!("br-{BR_REF_NAME}-{TEST_TAG_1}-40"),
format!("{COMMIT_SHA}-{TEST_TAG_1}-40"),
format!("br-{BR_REF_NAME}-{TEST_TAG_2}-40"),
format!("{COMMIT_SHA}-{TEST_TAG_2}-40"),
format!("br-{BR_REF_NAME}-{TEST_TAG_1}-41"),
format!("{COMMIT_SHA}-{TEST_TAG_1}-41"),
format!("br-{BR_REF_NAME}-{TEST_TAG_2}-41"),
format!("{COMMIT_SHA}-{TEST_TAG_2}-41"),
],
)]
fn generate_tags(
Expand Down
Loading
Loading