diff options
author | Matthias Beyer <matthias.beyer@atos.net> | 2021-03-05 18:35:15 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2021-03-07 21:50:29 +0100 |
commit | a84c7246a1255505232707e8ee6daac46e8e201f (patch) | |
tree | dfbc7165431c82a6fcbd059e32096741920143e7 | |
parent | fe9f663353e516ca70ce6ffba1b587c3ced6c334 (diff) |
Add subcommand "endpoint container list" for listing containers
Signed-off-by: Matthias Beyer <matthias.beyer@atos.net>
-rw-r--r-- | src/cli.rs | 15 | ||||
-rw-r--r-- | src/commands/endpoint.rs | 55 |
2 files changed, 70 insertions, 0 deletions
@@ -876,6 +876,21 @@ pub fn cli<'a>() -> App<'a> { .about("Format output as CSV") ) ) + .subcommand(App::new("containers") + .version(crate_version!()) + .about("Work with the containers of the endpoint(s)") + .subcommand(App::new("list") + .version(crate_version!()) + .about("List the containers and stats about them") + .arg(Arg::new("csv") + .required(false) + .multiple(false) + .long("csv") + .takes_value(false) + .about("Format output as CSV") + ) + ) + ) ) } diff --git a/src/commands/endpoint.rs b/src/commands/endpoint.rs index 78f2556..4882364 100644 --- a/src/commands/endpoint.rs +++ b/src/commands/endpoint.rs @@ -39,6 +39,7 @@ pub async fn endpoint(matches: &ArgMatches, config: &Configuration, progress_gen match matches.subcommand() { Some(("ping", matches)) => ping(endpoint_names, matches, config, progress_generator).await, Some(("stats", matches)) => stats(endpoint_names, matches, config, progress_generator).await, + Some(("containers", matches)) => containers(endpoint_names, matches, config).await, Some((other, _)) => Err(anyhow!("Unknown subcommand: {}", other)), None => Err(anyhow!("No subcommand")), } @@ -151,6 +152,60 @@ async fn stats(endpoint_names: Vec<String>, crate::commands::util::display_data(hdr, data, csv) } +async fn containers(endpoint_names: Vec<String>, + matches: &ArgMatches, + config: &Configuration, +) -> Result<()> { + match matches.subcommand() { + Some(("list", matches)) => containers_list(endpoint_names, matches, config).await, + Some((other, _)) => Err(anyhow!("Unknown subcommand: {}", other)), + None => Err(anyhow!("No subcommand")), + } +} + +async fn containers_list(endpoint_names: Vec<String>, + matches: &ArgMatches, + config: &Configuration, +) -> Result<()> { + let csv = matches.is_present("csv"); + let hdr = crate::commands::util::mk_header([ + "Endpoint", + "Container id", + "Image", + "Created", + "Status", + ].to_vec()); + + let data = connect_to_endpoints(config, &endpoint_names) + .await? + .into_iter() + .map(|ep| async move { + ep.container_stats().await.map(|stats| (ep.name().clone(), stats)) + }) + .collect::<futures::stream::FuturesUnordered<_>>() + .collect::<Result<Vec<(_, _)>>>() + .await? + .into_iter() + .map(|tpl| { + let endpoint_name = tpl.0; + tpl.1 + .into_iter() + .map(|stat| { + vec![ + endpoint_name.clone(), + stat.id, + stat.image, + stat.created.to_string(), + stat.status, + ] + }) + .collect::<Vec<Vec<String>>>() + }) + .flatten() + .collect::<Vec<Vec<String>>>(); + + crate::commands::util::display_data(hdr, data, csv) +} /// Helper function to connect to all endpoints from the configuration, that appear (by name) in /// the `endpoint_names` list |