Skip to content

Commit

Permalink
refactor: Swtich to using bon for builder pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
gmpinder committed Sep 21, 2024
1 parent aed7e27 commit 0c52cf6
Show file tree
Hide file tree
Showing 36 changed files with 326 additions and 314 deletions.
53 changes: 28 additions & 25 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ categories = ["command-line-utilities"]
version = "0.8.17"

[workspace.dependencies]
bon = "2"
chrono = "0.4"
clap = "4"
colored = "2"
Expand All @@ -23,7 +24,6 @@ serde = { version = "1", features = ["derive"] }
serde_json = "1"
serde_yaml = "0.9"
tempdir = "0.3"
typed-builder = "0.18"
users = "0.11"
uuid = { version = "1", features = ["v4"] }

Expand Down Expand Up @@ -81,7 +81,7 @@ serde.workspace = true
serde_json.workspace = true
serde_yaml.workspace = true
tempdir.workspace = true
typed-builder.workspace = true
bon.workspace = true
users.workspace = true

[features]
Expand Down
2 changes: 1 addition & 1 deletion process/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ oci-distribution.workspace = true
serde.workspace = true
serde_json.workspace = true
tempdir.workspace = true
typed-builder.workspace = true
bon.workspace = true
users.workspace = true
uuid.workspace = true

Expand Down
8 changes: 2 additions & 6 deletions process/drivers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::{
};

use blue_build_utils::constants::IMAGE_VERSION_LABEL;
use bon::Builder;
use clap::Args;
use log::{debug, info, trace};
use miette::{miette, Result};
Expand All @@ -21,7 +22,6 @@ use once_cell::sync::Lazy;
use opts::{GenerateImageNameOpts, GenerateTagsOpts};
#[cfg(feature = "sigstore")]
use sigstore_driver::SigstoreDriver;
use typed_builder::TypedBuilder;
use uuid::Uuid;

use self::{
Expand Down Expand Up @@ -82,29 +82,25 @@ static OS_VERSION: Lazy<Mutex<HashMap<String, u64>>> = Lazy::new(|| Mutex::new(H
///
/// If the args are left uninitialized, the program will determine
/// the best one available.
#[derive(Default, Clone, Copy, Debug, TypedBuilder, Args)]
#[derive(Default, Clone, Copy, Debug, Builder, Args)]
pub struct DriverArgs {
/// Select which driver to use to build
/// your image.
#[builder(default)]
#[arg(short = 'B', long)]
build_driver: Option<BuildDriverType>,

/// Select which driver to use to inspect
/// images.
#[builder(default)]
#[arg(short = 'I', long)]
inspect_driver: Option<InspectDriverType>,

/// Select which driver to use to sign
/// images.
#[builder(default)]
#[arg(short = 'S', long)]
signing_driver: Option<SigningDriverType>,

/// Select which driver to use to run
/// containers.
#[builder(default)]
#[arg(short = 'R', long)]
run_driver: Option<RunDriverType>,
}
Expand Down
17 changes: 10 additions & 7 deletions process/drivers/docker_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ use serde::Deserialize;
use tempdir::TempDir;

use crate::{
drivers::image_metadata::ImageMetadata,
drivers::{
image_metadata::ImageMetadata,
opts::{RunOptsEnv, RunOptsVolume},
},
logging::{CommandLogging, Logger},
signal_handler::{add_cid, remove_cid, ContainerId, ContainerRuntime},
};
Expand Down Expand Up @@ -321,7 +324,7 @@ impl InspectDriver for DockerDriver {
let output = Self::run_output(
&RunOpts::builder()
.image(SKOPEO_IMAGE)
.args(string_vec!["inspect", url.clone()])
.args(bon::vec!["inspect", &url])
.remove(true)
.build(),
)
Expand Down Expand Up @@ -379,13 +382,13 @@ fn docker_run(opts: &RunOpts, cid_file: &Path) -> Command {
if opts.privileged => "--privileged",
if opts.remove => "--rm",
if opts.pull => "--pull=always",
for volume in opts.volumes => [
for RunOptsVolume { path_or_vol_name, container_path } in opts.volumes.iter() => [
"--volume",
format!("{}:{}", volume.path_or_vol_name, volume.container_path),
format!("{path_or_vol_name}:{container_path}"),
],
for env in opts.env_vars => [
for RunOptsEnv { key, value } in opts.env_vars.iter() => [
"--env",
format!("{}={}", env.key, env.value),
format!("{key}={value}"),
],
|command| {
match (opts.uid, opts.gid) {
Expand All @@ -395,7 +398,7 @@ fn docker_run(opts: &RunOpts, cid_file: &Path) -> Command {
}
},
&*opts.image,
for opts.args,
for arg in opts.args.iter() => &**arg,
);
trace!("{command:?}");

Expand Down
10 changes: 5 additions & 5 deletions process/drivers/github_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ mod test {
constants::{
GITHUB_EVENT_NAME, GITHUB_EVENT_PATH, GITHUB_REF_NAME, GITHUB_SHA, PR_EVENT_NUMBER,
},
cowstr_vec, string_vec,
string_vec,
test_utils::set_env_var,
};
use oci_distribution::Reference;
Expand Down Expand Up @@ -230,7 +230,7 @@ mod test {
)]
#[case::default_branch_alt_tags(
setup_default_branch,
Some(cowstr_vec![TEST_TAG_1, TEST_TAG_2]),
Some(bon::vec![TEST_TAG_1, TEST_TAG_2]),
string_vec![
TEST_TAG_1,
format!("{TEST_TAG_1}-40"),
Expand All @@ -249,7 +249,7 @@ mod test {
)]
#[case::pr_branch_alt_tags(
setup_pr_branch,
Some(cowstr_vec![TEST_TAG_1, TEST_TAG_2]),
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"),
Expand All @@ -264,7 +264,7 @@ mod test {
)]
#[case::branch_alt_tags(
setup_branch,
Some(cowstr_vec![TEST_TAG_1, TEST_TAG_2]),
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"),
Expand All @@ -284,7 +284,7 @@ mod test {
let mut tags = GithubDriver::generate_tags(
&GenerateTagsOpts::builder()
.oci_ref(&oci_ref)
.alt_tags(alt_tags)
.maybe_alt_tags(alt_tags)
.build(),
)
.unwrap();
Expand Down
10 changes: 5 additions & 5 deletions process/drivers/gitlab_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ mod test {
CI_PIPELINE_SOURCE, CI_PROJECT_NAME, CI_PROJECT_NAMESPACE, CI_REGISTRY, CI_SERVER_HOST,
CI_SERVER_PROTOCOL,
},
cowstr_vec, string_vec,
string_vec,
test_utils::set_env_var,
};
use oci_distribution::Reference;
Expand Down Expand Up @@ -238,7 +238,7 @@ mod test {
)]
#[case::default_branch_alt_tags(
setup_default_branch,
Some(cowstr_vec![TEST_TAG_1, TEST_TAG_2]),
Some(bon::vec![TEST_TAG_1, TEST_TAG_2]),
string_vec![
TEST_TAG_1,
format!("{TEST_TAG_1}-40"),
Expand All @@ -257,7 +257,7 @@ mod test {
)]
#[case::pr_branch_alt_tags(
setup_mr_branch,
Some(cowstr_vec![TEST_TAG_1, TEST_TAG_2]),
Some(bon::vec![TEST_TAG_1, TEST_TAG_2]),
string_vec![
format!("mr-12-{TEST_TAG_1}-40"),
format!("{COMMIT_SHA}-{TEST_TAG_1}-40"),
Expand All @@ -272,7 +272,7 @@ mod test {
)]
#[case::branch_alt_tags(
setup_branch,
Some(cowstr_vec![TEST_TAG_1, TEST_TAG_2]),
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"),
Expand All @@ -292,7 +292,7 @@ mod test {
let mut tags = GitlabDriver::generate_tags(
&GenerateTagsOpts::builder()
.oci_ref(&oci_ref)
.alt_tags(alt_tags)
.maybe_alt_tags(alt_tags)
.build(),
)
.unwrap();
Expand Down
Loading

0 comments on commit 0c52cf6

Please sign in to comment.