testcontainers-build 0.1.0

Build Testcontainers images from folders, remote contexts, and pluggable Git providers
Documentation
use crate::{BuildContext, BuildContextProvider, ContextLocation, Error, Result};
use async_trait::async_trait;

/// Use an HTTP(S) tar archive or plain Dockerfile.
/// The Docker builder downloads the context.
pub struct RemoteUriSource {
    pub(crate) uri: String,
}

impl RemoteUriSource {
    /// Validate the remote context URI.
    /// Reject embedded credentials and schemes other than HTTP(S).
    pub fn new(uri: impl AsRef<str>) -> Result<Self> {
        let uri = url::Url::parse(uri.as_ref()).map_err(|e| Error::InvalidInput(e.to_string()))?;
        if !matches!(uri.scheme(), "http" | "https")
            || uri.host_str().is_none()
            || !uri.username().is_empty()
            || uri.password().is_some()
        {
            return Err(Error::InvalidInput(
                "use an HTTP(S) URI without embedded credentials".into(),
            ));
        }
        Ok(Self { uri: uri.into() })
    }
}

#[async_trait]
impl BuildContextProvider for RemoteUriSource {
    async fn resolve(&self) -> Result<BuildContext> {
        Ok(BuildContext::new(ContextLocation::Remote(self.uri.clone())))
    }
}