summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2021-03-08 11:20:49 +0100
committerGitHub <noreply@github.com>2021-03-08 11:20:49 +0100
commitc05a738a6c3752b45a2236363fab09e3bac09c24 (patch)
tree7bf7ab025105c3e3e573bf10f5c37edee561364f /src
parenta811935b64b4ff684828bebf80730b6fb443a991 (diff)
parent6b2645ed4a6a2717e174f5cd39c13fedee837947 (diff)
Merge pull request #277 from matthiasbeyer/add-safe-containers-get
Add Containers::get_checked() for getting an existing container
Diffstat (limited to 'src')
-rw-r--r--src/lib.rs19
1 files changed, 19 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 0e2fdd2..b18ba45 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -650,6 +650,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,