Skip to main content

testcontainers_build/sources/
remote.rs

1use crate::{BuildContext, BuildContextProvider, ContextLocation, Error, Result};
2use async_trait::async_trait;
3
4/// Use an HTTP(S) tar archive or plain Dockerfile.
5/// The Docker builder downloads the context.
6pub struct RemoteUriSource {
7    pub(crate) uri: String,
8}
9
10impl RemoteUriSource {
11    /// Validate the remote context URI.
12    /// Reject embedded credentials and schemes other than HTTP(S).
13    pub fn new(uri: impl AsRef<str>) -> Result<Self> {
14        let uri = url::Url::parse(uri.as_ref()).map_err(|e| Error::InvalidInput(e.to_string()))?;
15        if !matches!(uri.scheme(), "http" | "https")
16            || uri.host_str().is_none()
17            || !uri.username().is_empty()
18            || uri.password().is_some()
19        {
20            return Err(Error::InvalidInput(
21                "use an HTTP(S) URI without embedded credentials".into(),
22            ));
23        }
24        Ok(Self { uri: uri.into() })
25    }
26}
27
28#[async_trait]
29impl BuildContextProvider for RemoteUriSource {
30    async fn resolve(&self) -> Result<BuildContext> {
31        Ok(BuildContext::new(ContextLocation::Remote(self.uri.clone())))
32    }
33}