Merge branch 'list-albums' into 'main'

Add a command to list albums

See merge request kernald/immich-tools!24
This commit is contained in:
Marc Plano-Lesay 2024-11-24 00:34:12 +00:00
commit 351c1357c8
5 changed files with 61 additions and 1 deletions

View file

@ -36,6 +36,14 @@ pub(crate) struct Opts {
#[derive(Serialize, Subcommand)]
pub(crate) enum Commands {
/// Albums related commands
#[serde(rename = "albums")]
Albums {
#[command(subcommand)]
#[serde(flatten)]
albums_command: AlbumsCommands,
},
/// Assets related commands
#[serde(rename = "assets")]
Assets {
@ -68,6 +76,13 @@ pub(crate) enum Commands {
},
}
#[derive(Serialize, Subcommand)]
pub(crate) enum AlbumsCommands {
/// List all albums
#[serde(rename = "list")]
List {},
}
#[derive(Serialize, Subcommand)]
pub(crate) enum AssetsCommands {
/// Delete all assets

View file

@ -0,0 +1,35 @@
use crate::{
actions::{action::Action, fetch_all_albums::FetchAllAlbums},
context::Context,
};
use chrono::{DateTime, Utc};
use color_eyre::eyre::Result;
use tabled::{settings::Style, Table, Tabled};
#[derive(Tabled)]
struct Album {
#[tabled(rename = "Name")]
name: String,
#[tabled(rename = "Assets count")]
assets_count: u32,
#[tabled(rename = "Updated at")]
updated_at: DateTime<Utc>,
}
pub async fn list_albums(ctx: Context) -> Result<()> {
let mut albums: Vec<_> = FetchAllAlbums::new(())
.execute(&ctx)
.await?
.into_iter()
.map(|album| Album {
name: album.name,
updated_at: album.updated_at,
assets_count: album.assets_count,
})
.collect();
albums.sort_by_key(|album| album.name.to_lowercase());
println!("{}", Table::new(albums).with(Style::rounded()),);
Ok(())
}

View file

@ -1,4 +1,5 @@
pub mod delete_assets;
pub mod list_albums;
pub mod list_assets;
pub mod missing_date_of_birth;
pub mod scan_libraries;

View file

@ -9,11 +9,12 @@ mod tests {
}
use crate::args::Commands;
use args::{AssetsCommands, LibrariesCommands, PeopleCommands, ServerCommands};
use args::{AlbumsCommands, AssetsCommands, LibrariesCommands, PeopleCommands, ServerCommands};
use clap::Parser;
use color_eyre::eyre::{Result, WrapErr};
use color_eyre::Section;
use commands::delete_assets::delete_assets;
use commands::list_albums::list_albums;
use commands::list_assets::list_assets;
use commands::missing_date_of_birth::missing_date_of_birth;
use commands::scan_libraries::scan_libraries;
@ -76,6 +77,9 @@ async fn main() -> Result<()> {
validate_client_connection(&ctx.client).await?;
match &args.command {
Commands::Albums { albums_command } => match albums_command {
AlbumsCommands::List {} => list_albums(ctx).await,
},
Commands::Assets { assets_command } => match assets_command {
AssetsCommands::Delete { offline } => delete_assets(ctx, *offline).await,
AssetsCommands::List { offline } => list_assets(ctx, *offline).await,

View file

@ -1,3 +1,4 @@
use chrono::{DateTime, NaiveDate, Utc};
use uuid::Uuid;
use crate::types::AlbumResponseDto;
@ -6,6 +7,8 @@ use crate::types::AlbumResponseDto;
pub struct Album {
pub id: Uuid,
pub name: String,
pub updated_at: DateTime<Utc>,
pub assets_count: u32,
}
impl From<AlbumResponseDto> for Album {
@ -13,6 +16,8 @@ impl From<AlbumResponseDto> for Album {
Self {
id: Uuid::parse_str(&value.id).expect("Unable to parse an album's UUID"),
name: value.album_name,
updated_at: value.updated_at,
assets_count: value.asset_count as u32,
}
}
}