summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2021-07-22 17:47:51 +0300
committerLars Wirzenius <liw@liw.fi>2021-07-23 19:23:12 +0300
commit0d6ff366b09fbe37764863cc672741ba34dea56d (patch)
tree5559f84178e92428905c3b8be7cf1d712df43ad1 /src
parent9a8f4be06cb476b8149ed4f73d2cb62add14873f (diff)
downloadobnam2-0d6ff366b09fbe37764863cc672741ba34dea56d.tar.gz
refactor: use async for "obnam list"
Sponsored-by: author
Diffstat (limited to 'src')
-rw-r--r--src/client.rs17
-rw-r--r--src/cmd/list.rs12
2 files changed, 26 insertions, 3 deletions
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<GenerationList, ClientError> {
+ self.chunk_client.list_generations().await
+ }
+
pub async fn fetch_chunk(&self, chunk_id: &ChunkId) -> Result<DataChunk, ClientError> {
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<GenerationList, ClientError> {
+ let (_, body) = self.get("", &[("generation", "true")]).await?;
+
+ let map: HashMap<String, ChunkMeta> =
+ 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<DataChunk, ClientError> {
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());
}