Extend model to support update command

This commit is contained in:
2025-05-19 15:05:24 -06:00
parent 62e933448c
commit 73c5e4af9b
20 changed files with 337 additions and 194 deletions
+20 -11
View File
@@ -7,9 +7,9 @@ pub mod tmdb;
#[derive(Parser)]
#[command(version, about, long_about = None)]
pub struct Cli {
/// Use a custom config file [default: ~/.flix]
#[arg(short, long, value_name = "FILE")]
config: Option<PathBuf>,
/// Use a custom config file
#[arg(short, long, value_name = "FILE", default_value = "~/.flix")]
config: PathBuf,
#[command(subcommand)]
command: Command,
@@ -17,12 +17,9 @@ pub struct Cli {
impl Cli {
pub fn config_path(&self) -> PathBuf {
match self.config.as_ref() {
Some(path) => match path.strip_prefix("~/") {
Ok(path) => expect_home_dir().join(path),
Err(_) => path.to_owned(),
},
None => expect_home_dir().join(".flix"),
match self.config.strip_prefix("~/") {
Ok(path) => expect_home_dir().join(path),
Err(_) => self.config.to_owned(),
}
}
@@ -45,12 +42,18 @@ pub enum Command {
force: bool,
/// Change the destination
#[arg(short, long, value_name = "FILE")]
output: Option<PathBuf>,
#[arg(short, long, value_name = "FILE", default_value = "flix.toml")]
output: PathBuf,
#[command(subcommand)]
command: BackendCommand,
},
/// Update a flix manifest
Update {
/// Change the destination
#[arg(short, long, value_name = "FILE", default_value = "flix.toml")]
output: PathBuf,
},
}
#[derive(Subcommand)]
@@ -62,6 +65,12 @@ pub enum BackendCommand {
},
}
impl From<tmdb::Command> for BackendCommand {
fn from(value: tmdb::Command) -> Self {
Self::Tmdb { command: value }
}
}
fn expect_home_dir() -> PathBuf {
#[allow(clippy::expect_used)]
home::home_dir().expect("you do not have a home directory")
+54 -31
View File
@@ -5,15 +5,16 @@ use flix_tmdb::Client;
use anyhow::{Context, Result};
use clap::Parser;
use tokio::fs;
use tokio::io::AsyncWriteExt;
mod cli;
use cli::{BackendCommand, Cli, Command};
mod config;
use config::Config;
use tokio::io::AsyncWriteExt;
mod run;
use run::flix::FlixObject;
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<()> {
@@ -27,41 +28,63 @@ async fn main() -> Result<()> {
let client = Client::new(config.tmdb().bearer_token().to_owned());
match cli.command() {
Command::Print { command } => match command {
BackendCommand::Tmdb { command } => {
let object = run::tmdb::TmdbObject::fetch(&client, command).await?;
println!("{}", object.serialize().context("failed to serialize")?)
}
},
Command::Print { command } => exec_print(client, command).await?,
Command::Write {
force,
output,
command,
} => match command {
BackendCommand::Tmdb { command } => {
let object = run::tmdb::TmdbObject::fetch(&client, command).await?;
let output = output
.as_ref()
.map(|p| p.as_path())
.unwrap_or(Path::new("flix.toml"));
let mut file = if *force {
fs::File::create(output).await
} else {
fs::File::create_new(output).await
}
.with_context(|| format!("could not create file at path {}", output.display()))?;
file.write_all(
object
.serialize()
.context("failed to serialize tmdb object")?
.as_bytes(),
)
.await
.with_context(|| format!("could not write to file at path {}", output.display()))?;
}
},
} => exec_write(client, *force, output, command).await?,
Command::Update { output } => exec_update(client, output).await?,
}
Ok(())
}
async fn exec_print(client: Client, command: &BackendCommand) -> Result<()> {
match command {
BackendCommand::Tmdb { command } => {
let object = run::tmdb::TmdbObject::fetch(&client, command).await?;
println!("{}", object.serialize().context("failed to serialize")?)
}
}
Ok(())
}
async fn exec_write(
client: Client,
force: bool,
output: &Path,
command: &BackendCommand,
) -> Result<()> {
match command {
BackendCommand::Tmdb { command } => {
let object = run::tmdb::TmdbObject::fetch(&client, command).await?;
let mut file = if force {
fs::File::create(output).await
} else {
fs::File::create_new(output).await
}
.with_context(|| format!("could not create file at path {}", output.display()))?;
file.write_all(
object
.serialize()
.context("failed to serialize tmdb object")?
.as_bytes(),
)
.await
.with_context(|| format!("could not write to file at path {}", output.display()))?;
}
}
Ok(())
}
async fn exec_update(client: Client, output: &Path) -> Result<()> {
let content = fs::read_to_string(output)
.await
.with_context(|| format!("failed to read file at path: {}", output.display()))?;
let object: FlixObject = toml::from_str(&content).context("failed to deserialize flix file")?;
let command = object.backend_command()?;
exec_write(client, true, output, &command).await
}
+68
View File
@@ -0,0 +1,68 @@
use flix::model::{Collection, Episode, Movie, Season, Show, Verse};
use anyhow::{Result, bail};
use crate::cli::BackendCommand;
use crate::cli::tmdb;
#[derive(Debug, serde::Deserialize)]
#[serde(untagged)]
pub enum FlixObject {
Collection(Collection),
Movie(Movie),
Show(Show),
Season(Season),
Episode(Episode),
// DEAD CODE: The TMDB backend does not support Verses
#[allow(dead_code)]
Verse(Verse),
}
impl FlixObject {
pub fn backend_command(&self) -> Result<BackendCommand> {
Ok(match self {
FlixObject::Collection(collection) => {
let Some(ref tmdb) = collection.tmdb else {
bail!("missing tmdb data")
};
tmdb::Command::Collection { id: tmdb.id.into() }.into()
}
FlixObject::Movie(movie) => {
let Some(ref tmdb) = movie.tmdb else {
bail!("missing tmdb data")
};
tmdb::Command::Movie { id: tmdb.id.into() }.into()
}
FlixObject::Show(show) => {
let Some(ref tmdb) = show.tmdb else {
bail!("missing tmdb data")
};
tmdb::Command::Show { id: tmdb.id.into() }.into()
}
FlixObject::Season(season) => {
let Some(ref tmdb) = season.tmdb else {
bail!("missing tmdb data")
};
tmdb::Command::Season {
id: tmdb.show_id.into(),
season: season.season.number,
}
.into()
}
FlixObject::Episode(episode) => {
let Some(ref tmdb) = episode.tmdb else {
bail!("missing tmdb data")
};
tmdb::Command::Episode {
id: tmdb.show_id.into(),
season: episode.episode.season,
episode: episode.episode.number,
}
.into()
}
FlixObject::Verse(_) => {
bail!("verses are not handled")
}
})
}
}
+1
View File
@@ -1 +1,2 @@
pub mod flix;
pub mod tmdb;
+16 -10
View File
@@ -1,11 +1,11 @@
use flix::model::{
Collection, Episode, GenericCollection, GenericEpisode, GenericMovie, GenericSeason,
GenericShow, Movie, Season, Show, TmdbCollection, TmdbMovie, TmdbShow,
GenericShow, Movie, Season, Show, TmdbCollection, TmdbEpisode, TmdbMovie, TmdbSeason, TmdbShow,
};
use flix_tmdb::Client;
use flix_tmdb::model::{
Collection as TCollection, Episode as TEpisode, Movie as TMovie, Season as TSeason,
Show as TShow,
Show as TShow, ShowId,
};
use anyhow::{Context, Result};
@@ -16,8 +16,8 @@ pub enum TmdbObject {
Collection(TCollection),
Movie(TMovie),
Show(TShow),
Season(TSeason),
Episode(TEpisode),
Season(TSeason, ShowId),
Episode(TEpisode, u32, ShowId),
}
impl TmdbObject {
@@ -25,7 +25,7 @@ impl TmdbObject {
Ok(match self {
TmdbObject::Collection(tmdb) => toml::to_string(&Collection {
collection: GenericCollection {
name: tmdb.name,
title: tmdb.title,
overview: tmdb.overview,
},
tmdb: Some(TmdbCollection { id: tmdb.id }),
@@ -44,7 +44,7 @@ impl TmdbObject {
})?,
TmdbObject::Show(tmdb) => toml::to_string(&Show {
show: GenericShow {
name: tmdb.name,
title: tmdb.title,
overview: tmdb.overview,
genres: tmdb.genres.iter().cloned().map(|g| g.name).collect(),
air_date: tmdb.first_air_date,
@@ -54,21 +54,24 @@ impl TmdbObject {
genres: tmdb.genres.iter().map(|g| g.id).collect(),
}),
})?,
TmdbObject::Season(tmdb) => toml::to_string(&Season {
TmdbObject::Season(tmdb, show_id) => toml::to_string(&Season {
season: GenericSeason {
number: tmdb.season_number,
name: tmdb.name,
title: tmdb.title,
overview: tmdb.overview,
air_date: tmdb.air_date,
},
tmdb: Some(TmdbSeason { show_id }),
})?,
TmdbObject::Episode(tmdb) => toml::to_string(&Episode {
TmdbObject::Episode(tmdb, season_number, show_id) => toml::to_string(&Episode {
episode: GenericEpisode {
number: tmdb.episode_number,
name: tmdb.name,
season: season_number,
title: tmdb.title,
overview: tmdb.overview,
air_date: tmdb.air_date,
},
tmdb: Some(TmdbEpisode { show_id }),
})?,
})
}
@@ -104,6 +107,7 @@ impl TmdbObject {
.get_details(id, season, None)
.await
.with_context(|| format!("could not get show details for '{id}' S{season}"))?,
id.into(),
),
Command::Episode {
id,
@@ -117,6 +121,8 @@ impl TmdbObject {
.with_context(|| {
format!("could not get show details for '{id}' S{season}E{episode}")
})?,
season,
id.into(),
),
})
}