summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2021-03-06 08:10:00 +0100
committerMatthias Beyer <mail@beyermatthias.de>2021-03-08 08:22:42 +0100
commit6b2645ed4a6a2717e174f5cd39c13fedee837947 (patch)
treef62d636647dde89c073445cbd55f16f0c02a4eaa
parent95beb6f2aaf36d38ca1dc3abb7544fb878c45ca0 (diff)
Add Containers::get_checked() for getting an existing container
Containers::get() does always return a handle to a container, which might be invalid. This function checks whether the container exists before returning either a Some(Container) or None. Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r--src/lib.rs19
1 files changed, 19 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs
index a9d9264..83c53b9 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -645,6 +645,25 @@ impl<'docker> Containers<'docker> {
Container::new(self.docker, name)
}
+ /// Same as `Container::get()`, but checks whether the container exists and returns None if it
+ /// doesn't
+ pub async fn get_checked<S>(
+ &self,
+ name: S,
+ ) -> Result<Option<Container<'docker>>>
+ where
+ S: AsRef<str>,
+ {
+ let listopts = ContainerListOptions::builder().all().build();
+ self.list(&listopts)
+ .await?
+ .into_iter()
+ .find(|rep| rep.id == name.as_ref())
+ .map(|rep| Container::new(self.docker, rep.id))
+ .map(Ok)
+ .transpose()
+ }
+
/// Returns a builder interface for creating a new container instance
pub async fn create(
&self,