From 0d6ff366b09fbe37764863cc672741ba34dea56d Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Thu, 22 Jul 2021 17:47:51 +0300 Subject: refactor: use async for "obnam list" Sponsored-by: author --- src/client.rs | 17 +++++++++++++++++ src/cmd/list.rs | 12 +++++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/client.rs b/src/client.rs index e9ceea3..4ee3d3c 100644 --- a/src/client.rs +++ b/src/client.rs @@ -93,6 +93,10 @@ impl AsyncBackupClient { }) } + pub async fn list_generations(&self) -> Result { + self.chunk_client.list_generations().await + } + pub async fn fetch_chunk(&self, chunk_id: &ChunkId) -> Result { self.chunk_client.fetch_chunk(chunk_id).await } @@ -127,6 +131,19 @@ impl AsyncChunkClient { format!("{}/chunks", self.base_url()) } + pub async fn list_generations(&self) -> Result { + let (_, body) = self.get("", &[("generation", "true")]).await?; + + let map: HashMap = + serde_yaml::from_slice(&body).map_err(ClientError::YamlParse)?; + debug!("list_generations: map={:?}", map); + let finished = map + .iter() + .map(|(id, meta)| FinishedGeneration::new(id, meta.ended().map_or("", |s| s))) + .collect(); + Ok(GenerationList::new(finished)) + } + pub async fn fetch_chunk(&self, chunk_id: &ChunkId) -> Result { let (headers, body) = self.get(&format!("/{}", chunk_id), &[]).await?; let meta = self.get_chunk_meta_header(chunk_id, &headers)?; diff --git a/src/cmd/list.rs b/src/cmd/list.rs index 66036b9..691f2bf 100644 --- a/src/cmd/list.rs +++ b/src/cmd/list.rs @@ -1,16 +1,22 @@ -use crate::client::BackupClient; +use crate::client::AsyncBackupClient; use crate::config::ClientConfig; use crate::error::ObnamError; use structopt::StructOpt; +use tokio::runtime::Runtime; #[derive(Debug, StructOpt)] pub struct List {} impl List { pub fn run(&self, config: &ClientConfig) -> Result<(), ObnamError> { - let client = BackupClient::new(config)?; + let rt = Runtime::new()?; + rt.block_on(self.run_async(config)) + } + + async fn run_async(&self, config: &ClientConfig) -> Result<(), ObnamError> { + let client = AsyncBackupClient::new(config)?; - let generations = client.list_generations()?; + let generations = client.list_generations().await?; for finished in generations.iter() { println!("{} {}", finished.id(), finished.ended()); } -- cgit v1.2.1