diff options
author | Matthias Beyer <matthias.beyer@atos.net> | 2021-06-02 13:02:29 +0200 |
---|---|---|
committer | Matthias Beyer <matthias.beyer@atos.net> | 2021-06-02 13:17:58 +0200 |
commit | fb2aa81b6360acb7033488d0b042d3eddf25ad4b (patch) | |
tree | 7d73e83fe75245e8372ac90575fff705d6524952 | |
parent | bf25f0e2e519aa304cf89b8eb79a74bd94c804a6 (diff) |
Refactor: move get_date_filter() helper fn to commands:util module
Signed-off-by: Matthias Beyer <matthias.beyer@atos.net>
-rw-r--r-- | src/commands/endpoint.rs | 32 | ||||
-rw-r--r-- | src/commands/util.rs | 16 |
2 files changed, 24 insertions, 24 deletions
diff --git a/src/commands/endpoint.rs b/src/commands/endpoint.rs index 4de51d2..6fd7d69 100644 --- a/src/commands/endpoint.rs +++ b/src/commands/endpoint.rs @@ -182,8 +182,8 @@ async fn containers_list(endpoint_names: Vec<EndpointName>, ) -> Result<()> { let list_stopped = matches.is_present("list_stopped"); let filter_image = matches.value_of("filter_image"); - let older_than_filter = get_date_filter("older_than", matches)?; - let newer_than_filter = get_date_filter("newer_than", matches)?; + let older_than_filter = crate::commands::util::get_date_filter("older_than", matches)?; + let newer_than_filter = crate::commands::util::get_date_filter("newer_than", matches)?; let csv = matches.is_present("csv"); let hdr = crate::commands::util::mk_header([ "Endpoint", @@ -232,8 +232,8 @@ async fn containers_prune(endpoint_names: Vec<EndpointName>, matches: &ArgMatches, config: &Configuration, ) -> Result<()> { - let older_than_filter = get_date_filter("older_than", matches)?; - let newer_than_filter = get_date_filter("newer_than", matches)?; + let older_than_filter = crate::commands::util::get_date_filter("older_than", matches)?; + let newer_than_filter = crate::commands::util::get_date_filter("newer_than", matches)?; let stats = connect_to_endpoints(config, &endpoint_names) .await? @@ -279,8 +279,8 @@ async fn containers_top(endpoint_names: Vec<EndpointName>, config: &Configuration, ) -> Result<()> { let limit = matches.value_of("limit").map(usize::from_str).transpose()?; - let older_than_filter = get_date_filter("older_than", matches)?; - let newer_than_filter = get_date_filter("newer_than", matches)?; + let older_than_filter = crate::commands::util::get_date_filter("older_than", matches)?; + let newer_than_filter = crate::commands::util::get_date_filter("newer_than", matches)?; let csv = matches.is_present("csv"); let data = connect_to_endpoints(config, &endpoint_names) @@ -374,8 +374,8 @@ async fn containers_stop(endpoint_names: Vec<EndpointName>, matches: &ArgMatches, config: &Configuration, ) -> Result<()> { - let older_than_filter = get_date_filter("older_than", matches)?; - let newer_than_filter = get_date_filter("newer_than", matches)?; + let older_than_filter = crate::commands::util::get_date_filter("older_than", matches)?; + let newer_than_filter = crate::commands::util::get_date_filter("newer_than", matches)?; let stop_timeout = matches.value_of("timeout") .map(u64::from_str) @@ -422,22 +422,6 @@ async fn containers_stop(endpoint_names: Vec<EndpointName>, } -fn get_date_filter(name: &str, matches: &ArgMatches) -> Result<Option<chrono::DateTime::<chrono::Local>>> { - matches.value_of(name) - .map(humantime::parse_duration) - .transpose()? - .map(chrono::Duration::from_std) - .transpose()? - .map(|dur| { - chrono::offset::Local::now() - .checked_sub_signed(dur) - .ok_or_else(|| anyhow!("Time calculation would overflow")) - .with_context(|| anyhow!("Cannot subtract {} from 'now'", dur)) - .map_err(Error::from) - }) - .transpose() -} - /// Helper function to connect to all endpoints from the configuration, that appear (by name) in /// the `endpoint_names` list pub(super) async fn connect_to_endpoints(config: &Configuration, endpoint_names: &[EndpointName]) -> Result<Vec<Arc<Endpoint>>> { diff --git a/src/commands/util.rs b/src/commands/util.rs index e892875..58763ac 100644 --- a/src/commands/util.rs +++ b/src/commands/util.rs @@ -225,3 +225,19 @@ pub fn display_data<D: Display>( } } +pub fn get_date_filter(name: &str, matches: &ArgMatches) -> Result<Option<chrono::DateTime::<chrono::Local>>> { + matches.value_of(name) + .map(humantime::parse_duration) + .transpose()? + .map(chrono::Duration::from_std) + .transpose()? + .map(|dur| { + chrono::offset::Local::now() + .checked_sub_signed(dur) + .ok_or_else(|| anyhow!("Time calculation would overflow")) + .with_context(|| anyhow!("Cannot subtract {} from 'now'", dur)) + .map_err(Error::from) + }) + .transpose() +} + |