diff --git a/src/commands/build.rs b/src/commands/build.rs index f67e610..32a197b 100644 --- a/src/commands/build.rs +++ b/src/commands/build.rs @@ -18,7 +18,7 @@ use colorized::{Color, Colors}; use log::{debug, info, trace, warn}; use typed_builder::TypedBuilder; -use crate::{commands::template::TemplateCommand, strategies::Strategy}; +use crate::{commands::template::TemplateCommand, credentials, drivers::Driver}; use super::BlueBuildCommand; @@ -142,7 +142,7 @@ impl BlueBuildCommand for BuildCommand { fn try_run(&mut self) -> Result<()> { trace!("BuildCommand::try_run()"); - Strategy::builder() + Driver::builder() .username(self.username.as_ref()) .password(self.password.as_ref()) .registry(self.registry.as_ref()) @@ -230,7 +230,7 @@ impl BuildCommand { trace!("BuildCommand::build_image()"); let recipe = Recipe::parse(&recipe_path)?; - let os_version = Strategy::get_os_version(&recipe)?; + let os_version = Driver::get_os_version(&recipe)?; let tags = recipe.generate_tags(&os_version); let image_name = self.generate_full_image_name(&recipe)?; @@ -249,7 +249,7 @@ impl BuildCommand { trace!("BuildCommand::login()"); info!("Attempting to login to the registry"); - let credentials = Strategy::get_credentials()?; + let credentials = credentials::get()?; let (registry, username, password) = ( &credentials.registry, @@ -258,7 +258,7 @@ impl BuildCommand { ); info!("Logging into the registry, {registry}"); - Strategy::get_build_strategy().login()?; + Driver::get_build_driver().login()?; trace!("cosign login -u {username} -p [MASKED] {registry}"); let login_output = Command::new("cosign") @@ -355,7 +355,7 @@ impl BuildCommand { fn run_build(&self, image_name: &str, tags: &[String]) -> Result<()> { trace!("BuildCommand::run_build({image_name}, {tags:#?})"); - let strat = Strategy::get_build_strategy(); + let strat = Driver::get_build_driver(); let full_image = if self.archive.is_some() { image_name.to_string() diff --git a/src/commands/template.rs b/src/commands/template.rs index efedca8..f52afdc 100644 --- a/src/commands/template.rs +++ b/src/commands/template.rs @@ -8,7 +8,7 @@ use clap::Args; use log::{debug, info, trace}; use typed_builder::TypedBuilder; -use crate::strategies::Strategy; +use crate::drivers::Driver; use super::BlueBuildCommand; @@ -53,8 +53,8 @@ impl TemplateCommand { trace!("recipe_de: {recipe_de:#?}"); let template = ContainerFileTemplate::builder() - .os_version(Strategy::get_os_version(&recipe_de)?) - .build_id(Strategy::get_build_id()) + .os_version(Driver::get_os_version(&recipe_de)?) + .build_id(Driver::get_build_id()) .recipe(&recipe_de) .recipe_path(recipe_path.as_path()) .build(); diff --git a/src/strategies/credentials.rs b/src/credentials.rs similarity index 98% rename from src/strategies/credentials.rs rename to src/credentials.rs index 123e4c9..2252a74 100644 --- a/src/strategies/credentials.rs +++ b/src/credentials.rs @@ -101,6 +101,9 @@ static ENV_CREDENTIALS: Lazy> = Lazy::new(|| { /// Be sure to call this before trying to use /// any strategy that requires credentials as /// the environment credentials are lazy allocated. +/// +/// # Errors +/// Will error if it can't lock the mutex. pub fn set_user_creds( username: Option<&String>, password: Option<&String>, diff --git a/src/strategies.rs b/src/drivers.rs similarity index 84% rename from src/strategies.rs rename to src/drivers.rs index fed32e8..5762fff 100644 --- a/src/strategies.rs +++ b/src/drivers.rs @@ -18,7 +18,6 @@ use blue_build_utils::constants::{ IMAGE_VERSION_LABEL, RUN_PODMAN_SOCK, VAR_RUN_PODMAN_PODMAN_SOCK, VAR_RUN_PODMAN_SOCK, XDG_RUNTIME_DIR, }; -pub use credentials::Credentials; use log::{debug, error, info, trace}; use once_cell::sync::Lazy; use typed_builder::TypedBuilder; @@ -31,22 +30,21 @@ use podman_api::Podman; use tokio::runtime::Runtime; #[cfg(feature = "builtin-podman")] -use crate::strategies::podman_api_strategy::PodmanApiStrategy; +use podman_api_driver::PodmanApiDriver; -use crate::image_inspection::ImageInspection; +use crate::{credentials, image_inspection::ImageInspection}; use self::{ - buildah_strategy::BuildahStrategy, docker_strategy::DockerStrategy, - podman_strategy::PodmanStrategy, skopeo_strategy::SkopeoStrategy, + buildah_driver::BuildahDriver, docker_driver::DockerDriver, podman_driver::PodmanDriver, + skopeo_driver::SkopeoDriver, }; -mod buildah_strategy; -mod credentials; -mod docker_strategy; +mod buildah_driver; +mod docker_driver; #[cfg(feature = "builtin-podman")] -mod podman_api_strategy; -mod podman_strategy; -mod skopeo_strategy; +mod podman_api_driver; +mod podman_driver; +mod skopeo_driver; /// Stores the build strategy. /// @@ -59,8 +57,8 @@ mod skopeo_strategy; /// /// This will cause the program to exit if a build strategy could /// not be determined. -static BUILD_STRATEGY: Lazy> = - Lazy::new(|| match Strategy::determine_build_strategy() { +static BUILD_STRATEGY: Lazy> = + Lazy::new(|| match Driver::determine_build_driver() { Err(e) => { error!("{e}"); process::exit(1); @@ -79,8 +77,8 @@ static BUILD_STRATEGY: Lazy> = /// /// This will cause the program to exit if a build strategy could /// not be determined. -static INSPECT_STRATEGY: Lazy> = - Lazy::new(|| match Strategy::determine_inspect_strategy() { +static INSPECT_STRATEGY: Lazy> = + Lazy::new(|| match Driver::determine_inspect_driver() { Err(e) => { error!("{e}"); process::exit(1); @@ -96,7 +94,7 @@ static OS_VERSION: Lazy>> = Lazy::new(|| Mutex::ne /// Allows agnostic building, tagging /// pushing, and login. -pub trait BuildStrategy: Sync + Send { +pub trait BuildDriver: Sync + Send { /// Runs the build logic for the strategy. /// /// # Errors @@ -123,7 +121,7 @@ pub trait BuildStrategy: Sync + Send { } /// Allows agnostic inspection of images. -pub trait InspectStrategy: Sync + Send { +pub trait InspectDriver: Sync + Send { /// Gets the labels on an image tag. /// /// # Errors @@ -132,7 +130,7 @@ pub trait InspectStrategy: Sync + Send { } #[derive(Debug, TypedBuilder)] -pub struct Strategy<'a> { +pub struct Driver<'a> { #[builder(default)] username: Option<&'a String>, @@ -143,7 +141,7 @@ pub struct Strategy<'a> { registry: Option<&'a String>, } -impl<'a> Strategy<'a> { +impl Driver<'_> { /// Initializes the Strategy with user provided credentials. /// /// If you want to take advantage of a user's credentials, @@ -164,23 +162,15 @@ impl<'a> Strategy<'a> { } /// Gets the current run's build strategy - pub fn get_build_strategy() -> Arc { + pub fn get_build_driver() -> Arc { BUILD_STRATEGY.clone() } /// Gets the current run's inspectioin strategy - pub fn get_inspection_strategy() -> Arc { + pub fn get_inspection_driver() -> Arc { INSPECT_STRATEGY.clone() } - /// Get the current environment credentials. - /// - /// # Errors - /// Will error if credentials don't exist. - pub fn get_credentials() -> Result<&'static Credentials> { - credentials::get() - } - /// Retrieve the `os_version` for an image. /// /// This gets cached for faster resolution if it's required @@ -228,7 +218,7 @@ impl<'a> Strategy<'a> { Ok(os_version) } - fn determine_inspect_strategy() -> Result> { + fn determine_inspect_driver() -> Result> { trace!("Strategy::determine_inspect_strategy()"); Ok( @@ -237,15 +227,15 @@ impl<'a> Strategy<'a> { blue_build_utils::check_command_exists("docker"), blue_build_utils::check_command_exists("podman"), ) { - (Ok(_skopeo), _, _) => Arc::new(SkopeoStrategy), - (_, Ok(_docker), _) => Arc::new(DockerStrategy), - (_, _, Ok(_podman)) => Arc::new(PodmanStrategy), + (Ok(_skopeo), _, _) => Arc::new(SkopeoDriver), + (_, Ok(_docker), _) => Arc::new(DockerDriver), + (_, _, Ok(_podman)) => Arc::new(PodmanDriver), _ => bail!("Could not determine inspection strategy. You need either skopeo, docker, or podman"), } ) } - fn determine_build_strategy() -> Result> { + fn determine_build_driver() -> Result> { trace!("Strategy::determine_build_strategy()"); Ok( @@ -263,7 +253,7 @@ impl<'a> Strategy<'a> { if PathBuf::from(format!("{xdg_runtime}/podman/podman.sock")).exists() => { Arc::new( - PodmanApiStrategy::builder() + PodmanApiDriver::builder() .client( Podman::unix(PathBuf::from(format!( "{xdg_runtime}/podman/podman.sock" @@ -277,7 +267,7 @@ impl<'a> Strategy<'a> { #[cfg(feature = "builtin-podman")] (_, run_podman_podman_sock, _, _, _, _, _) if run_podman_podman_sock.exists() => { Arc::new( - PodmanApiStrategy::builder() + PodmanApiDriver::builder() .client(Podman::unix(run_podman_podman_sock).into()) .rt(Runtime::new()?) .build(), @@ -288,7 +278,7 @@ impl<'a> Strategy<'a> { if var_run_podman_podman_sock.exists() => { Arc::new( - PodmanApiStrategy::builder() + PodmanApiDriver::builder() .client(Podman::unix(var_run_podman_podman_sock).into()) .rt(Runtime::new()?) .build(), @@ -297,16 +287,16 @@ impl<'a> Strategy<'a> { #[cfg(feature = "builtin-podman")] (_, _, _, var_run_podman_sock, _, _, _) if var_run_podman_sock.exists() => { Arc::new( - PodmanApiStrategy::builder() + PodmanApiDriver::builder() .client(Podman::unix(var_run_podman_sock).into()) .rt(Runtime::new()?) .build(), ) } // (_, _, _, _, Ok(_docker), _, _) if !oci_required => { - (_, _, _, _, Ok(_docker), _, _) => Arc::new(DockerStrategy), - (_, _, _, _, _, Ok(_podman), _) => Arc::new(PodmanStrategy), - (_, _, _, _, _, _, Ok(_buildah)) => Arc::new(BuildahStrategy), + (_, _, _, _, Ok(_docker), _, _) => Arc::new(DockerDriver), + (_, _, _, _, _, Ok(_podman), _) => Arc::new(PodmanDriver), + (_, _, _, _, _, _, Ok(_buildah)) => Arc::new(BuildahDriver), _ => bail!( "Could not determine strategy, need either docker, podman, or buildah to continue" ), diff --git a/src/strategies/buildah_strategy.rs b/src/drivers/buildah_driver.rs similarity index 94% rename from src/strategies/buildah_strategy.rs rename to src/drivers/buildah_driver.rs index c69e2dc..d398b1c 100644 --- a/src/strategies/buildah_strategy.rs +++ b/src/drivers/buildah_driver.rs @@ -3,12 +3,14 @@ use std::process::Command; use anyhow::{bail, Result}; use log::{info, trace}; -use super::{credentials, BuildStrategy}; +use crate::credentials; + +use super::BuildDriver; #[derive(Debug)] -pub struct BuildahStrategy; +pub struct BuildahDriver; -impl BuildStrategy for BuildahStrategy { +impl BuildDriver for BuildahDriver { fn build(&self, image: &str) -> Result<()> { trace!("buildah build -t {image}"); let status = Command::new("buildah") diff --git a/src/strategies/docker_strategy.rs b/src/drivers/docker_driver.rs similarity index 95% rename from src/strategies/docker_strategy.rs rename to src/drivers/docker_driver.rs index 9b9d03f..8061d07 100644 --- a/src/strategies/docker_strategy.rs +++ b/src/drivers/docker_driver.rs @@ -9,12 +9,12 @@ use log::{info, trace}; use crate::image_inspection::ImageInspection; -use super::{credentials, BuildStrategy, InspectStrategy}; +use super::{credentials, BuildDriver, InspectDriver}; #[derive(Debug)] -pub struct DockerStrategy; +pub struct DockerDriver; -impl BuildStrategy for DockerStrategy { +impl BuildDriver for DockerDriver { fn build(&self, image: &str) -> Result<()> { trace!("docker"); let mut command = Command::new("docker"); @@ -103,7 +103,7 @@ impl BuildStrategy for DockerStrategy { } } -impl InspectStrategy for DockerStrategy { +impl InspectDriver for DockerDriver { fn get_labels(&self, image_name: &str, tag: &str) -> Result { let url = format!("docker://{image_name}:{tag}"); diff --git a/src/strategies/podman_api_strategy.rs b/src/drivers/podman_api_driver.rs similarity index 97% rename from src/strategies/podman_api_strategy.rs rename to src/drivers/podman_api_driver.rs index d20a390..0702bca 100644 --- a/src/strategies/podman_api_strategy.rs +++ b/src/drivers/podman_api_driver.rs @@ -21,17 +21,17 @@ use tokio::{ }; use typed_builder::TypedBuilder; -use crate::strategies::BUILD_ID; +use crate::drivers::BUILD_ID; -use super::{credentials, BuildStrategy}; +use super::{credentials, BuildDriver}; #[derive(Debug, TypedBuilder)] -pub struct PodmanApiStrategy { +pub struct PodmanApiDriver { client: Arc, rt: Runtime, } -impl BuildStrategy for PodmanApiStrategy { +impl BuildDriver for PodmanApiDriver { fn build(&self, image: &str) -> Result<()> { trace!("PodmanApiStrategy::build({image})"); diff --git a/src/strategies/podman_strategy.rs b/src/drivers/podman_driver.rs similarity index 94% rename from src/strategies/podman_strategy.rs rename to src/drivers/podman_driver.rs index 8853fc7..8938af7 100644 --- a/src/strategies/podman_strategy.rs +++ b/src/drivers/podman_driver.rs @@ -6,12 +6,12 @@ use log::{debug, info, trace}; use crate::image_inspection::ImageInspection; -use super::{credentials, BuildStrategy, InspectStrategy}; +use super::{credentials, BuildDriver, InspectDriver}; #[derive(Debug)] -pub struct PodmanStrategy; +pub struct PodmanDriver; -impl BuildStrategy for PodmanStrategy { +impl BuildDriver for PodmanDriver { fn build(&self, image: &str) -> Result<()> { trace!("podman build . -t {image}"); let status = Command::new("podman") @@ -81,7 +81,7 @@ impl BuildStrategy for PodmanStrategy { } } -impl InspectStrategy for PodmanStrategy { +impl InspectDriver for PodmanDriver { fn get_labels(&self, image_name: &str, tag: &str) -> Result { let url = format!("docker://{image_name}:{tag}"); diff --git a/src/strategies/skopeo_strategy.rs b/src/drivers/skopeo_driver.rs similarity index 88% rename from src/strategies/skopeo_strategy.rs rename to src/drivers/skopeo_driver.rs index 2b1d714..40ce673 100644 --- a/src/strategies/skopeo_strategy.rs +++ b/src/drivers/skopeo_driver.rs @@ -5,12 +5,12 @@ use log::{debug, trace}; use crate::image_inspection::ImageInspection; -use super::InspectStrategy; +use super::InspectDriver; #[derive(Debug)] -pub struct SkopeoStrategy; +pub struct SkopeoDriver; -impl InspectStrategy for SkopeoStrategy { +impl InspectDriver for SkopeoDriver { fn get_labels(&self, image_name: &str, tag: &str) -> Result { let url = format!("docker://{image_name}:{tag}"); diff --git a/src/lib.rs b/src/lib.rs index b7cada7..d069692 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,5 +5,6 @@ shadow_rs::shadow!(shadow); pub mod commands; +pub mod credentials; +pub mod drivers; pub mod image_inspection; -pub mod strategies;