summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Beyer <matthias.beyer@atos.net>2021-03-05 19:05:45 +0100
committerMatthias Beyer <mail@beyermatthias.de>2021-03-07 21:50:29 +0100
commit93386d25f097c7097df9148ff2e97cc27138817d (patch)
tree9adbce312232ae387f37b1642368177f6bab65b2
parenta84c7246a1255505232707e8ee6daac46e8e201f (diff)
Add filters for listing containers on endpoints
Signed-off-by: Matthias Beyer <matthias.beyer@atos.net>
-rw-r--r--Cargo.toml1
-rw-r--r--src/cli.rs43
-rw-r--r--src/commands/endpoint.rs14
3 files changed, 58 insertions, 0 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 8ca0c75..638160a 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -31,6 +31,7 @@ futures = "0.3"
getset = "0.1"
git2 = "0.13"
handlebars = { version = "3", features = ["no_logging"] }
+humantime = "2.1"
indicatif = "0.15"
indoc = "1"
itertools = "0.10"
diff --git a/src/cli.rs b/src/cli.rs
index cc8734e..f110861 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -889,6 +889,45 @@ pub fn cli<'a>() -> App<'a> {
.takes_value(false)
.about("Format output as CSV")
)
+
+ .arg(Arg::new("list_stopped")
+ .required(false)
+ .multiple(false)
+ .long("list-stopped")
+ .takes_value(false)
+ .about("List stopped containers too")
+ )
+
+ .arg(Arg::new("filter_image")
+ .required(false)
+ .multiple(false)
+ .long("image")
+ .takes_value(true)
+ .value_name("IMAGE")
+ .about("List only containers of IMAGE")
+ )
+
+ .arg(Arg::new("older_than")
+ .required(false)
+ .multiple(false)
+ .long("older-than")
+ .takes_value(true)
+ .value_name("DATE")
+ .about("List only containers that are older than DATE")
+ .validator(parse_date_from_string)
+ .conflicts_with("newer_than")
+ )
+
+ .arg(Arg::new("newer_than")
+ .required(false)
+ .multiple(false)
+ .long("newer-than")
+ .takes_value(true)
+ .value_name("DATE")
+ .about("List only containers that are newer than DATE")
+ .validator(parse_date_from_string)
+ .conflicts_with("older_than")
+ )
)
)
)
@@ -969,6 +1008,10 @@ fn dir_exists_validator(s: &str) -> Result<(), String> {
}
}
+fn parse_date_from_string(s: &str) -> std::result::Result<(), String> {
+ humantime::parse_rfc3339_weak(s).map_err(|e| e.to_string()).map(|_| ())
+}
+
#[cfg(test)]
mod tests {
use super::env_pass_validator;
diff --git a/src/commands/endpoint.rs b/src/commands/endpoint.rs
index 4882364..abb09bb 100644
--- a/src/commands/endpoint.rs
+++ b/src/commands/endpoint.rs
@@ -167,6 +167,16 @@ async fn containers_list(endpoint_names: Vec<String>,
matches: &ArgMatches,
config: &Configuration,
) -> Result<()> {
+ let list_stopped = matches.is_present("list_stopped");
+ let filter_image = matches.value_of("filter_image");
+ let older_than_filter = matches.value_of("older_than")
+ .map(humantime::parse_rfc3339_weak)
+ .transpose()?
+ .map(chrono::DateTime::<chrono::Local>::from);
+ let newer_than_filter = matches.value_of("newer_than")
+ .map(humantime::parse_rfc3339_weak)
+ .transpose()?
+ .map(chrono::DateTime::<chrono::Local>::from);
let csv = matches.is_present("csv");
let hdr = crate::commands::util::mk_header([
"Endpoint",
@@ -190,6 +200,10 @@ async fn containers_list(endpoint_names: Vec<String>,
let endpoint_name = tpl.0;
tpl.1
.into_iter()
+ .filter(|stat| list_stopped || stat.state != "exited")
+ .filter(|stat| filter_image.map(|fim| fim == stat.image).unwrap_or(true))
+ .filter(|stat| older_than_filter.as_ref().map(|time| time > &stat.created).unwrap_or(true))
+ .filter(|stat| newer_than_filter.as_ref().map(|time| time < &stat.created).unwrap_or(true))
.map(|stat| {
vec![
endpoint_name.clone(),