From 9ec3b1c07d8518ae4a6563f847fa0749dcf8a109 Mon Sep 17 00:00:00 2001 From: Bauke Date: Sun, 9 Oct 2022 12:20:49 +0200 Subject: [PATCH] Add showing snapshots by ID. --- source/cli/mod.rs | 6 +++++- source/cli/run.rs | 26 +++++++++++++++++--------- source/snapshots/mod.rs | 13 +++++++++++++ 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/source/cli/mod.rs b/source/cli/mod.rs index 6ce328e..52f02b5 100644 --- a/source/cli/mod.rs +++ b/source/cli/mod.rs @@ -90,8 +90,12 @@ pub enum SnapshotSubcommands { /// Show a snapshot. Show { /// The date of the snapshot to show, defaults to today. - #[clap(short, long)] + #[clap(short, long, group = "selection")] date: Option, + + /// The ID of the snapshot to show. + #[clap(long, group = "selection")] + id: Option, }, } diff --git a/source/cli/run.rs b/source/cli/run.rs index 3975348..ea44287 100644 --- a/source/cli/run.rs +++ b/source/cli/run.rs @@ -58,16 +58,24 @@ pub async fn run() -> Result<()> { } } - SnapshotSubcommands::Show { date } => { + SnapshotSubcommands::Show { date, id } => { let date = date.unwrap_or_else(today); - let snapshot = if let Some(snapshot) = - SnapshotModel::get_by_date(&db, date).await? - { - info!("Snapshot {snapshot:?}"); - snapshot - } else { - info!("No snapshot exists for {date}"); - return Ok(()); + + let snapshot = match id { + Some(id) => SnapshotModel::get_by_id(&db, id).await, + None => SnapshotModel::get_by_date(&db, date).await, + }?; + + let snapshot = match (snapshot, id) { + (None, Some(id)) => { + info!("No snapshot exists for id {id}"); + return Ok(()); + } + (None, None) => { + info!("No snapshot exists for date {date}"); + return Ok(()); + } + (Some(snapshot), _) => snapshot, }; for group in GroupDataModel::get_all_by_snapshot(&db, &snapshot).await? diff --git a/source/snapshots/mod.rs b/source/snapshots/mod.rs index 57d5c61..6a8a643 100644 --- a/source/snapshots/mod.rs +++ b/source/snapshots/mod.rs @@ -27,6 +27,19 @@ impl SnapshotModel { Ok(existing) } + /// Get a snapshot by a given ID. + pub async fn get_by_id( + db: &DatabaseConnection, + id: i64, + ) -> Result> { + let snapshot = SnapshotEntity::find() + .filter(SnapshotColumn::Id.eq(id)) + .one(db) + .await?; + + Ok(snapshot) + } + /// Get all snapshots. pub async fn get_all(db: &DatabaseConnection) -> Result> { let snapshots = SnapshotEntity::find().all(db).await?;