feat(readarr): refresh and rescan a Readarr author and expose the command on the CLI

Posts the RefreshAuthor command to the command endpoint with the author ID, queueing a metadata refresh and a disk scan for that author. The response is returned verbatim and no app state is mutated.

Adds the refresh subcommand group to the Readarr CLI.
This commit is contained in:
Dark-Alex-17 committed 2026-08-27 10:43:37 -06:00
1 parent e9b9aa3a14
commit c82006862e
7 files changed
+258 -1

No files matched your search

+12
View File
@@ -7,6 +7,7 @@ use delete_command_handler::{ReadarrDeleteCommand, ReadarrDeleteCommandHandler};
use edit_command_handler::{ReadarrEditCommand, ReadarrEditCommandHandler};
use get_command_handler::{ReadarrGetCommand, ReadarrGetCommandHandler};
use list_command_handler::{ReadarrListCommand, ReadarrListCommandHandler};
use refresh_command_handler::{ReadarrRefreshCommand, ReadarrRefreshCommandHandler};
use tokio::sync::Mutex;
use trigger_automatic_search_command_handler::{
ReadarrTriggerAutomaticSearchCommand, ReadarrTriggerAutomaticSearchCommandHandler,
@@ -22,6 +23,7 @@ mod delete_command_handler;
mod edit_command_handler;
mod get_command_handler;
mod list_command_handler;
mod refresh_command_handler;
mod trigger_automatic_search_command_handler;
#[cfg(test)]
@@ -55,6 +57,11 @@ pub enum ReadarrCommand {
about = "Commands to list attributes from your Readarr instance"
)]
List(ReadarrListCommand),
#[command(
subcommand,
about = "Commands to refresh the data in your Readarr instance"
)]
Refresh(ReadarrRefreshCommand),
#[command(
subcommand,
about = "Commands to trigger automatic searches for releases of different resources in your Readarr instance"
@@ -144,6 +151,11 @@ impl<'a, 'b> CliCommandHandler<'a, 'b, ReadarrCommand> for ReadarrCliHandler<'a,
.handle()
.await?
}
ReadarrCommand::Refresh(refresh_command) => {
ReadarrRefreshCommandHandler::with(self.app, refresh_command, self.network)
.handle()
.await?
}
ReadarrCommand::TriggerAutomaticSearch(trigger_automatic_search_command) => {
ReadarrTriggerAutomaticSearchCommandHandler::with(
self.app,
@@ -0,0 +1,72 @@
use std::sync::Arc;
use anyhow::Result;
use clap::Subcommand;
use tokio::sync::Mutex;
use crate::{
app::App,
cli::{CliCommandHandler, Command},
network::{NetworkTrait, readarr_network::ReadarrEvent},
};
use super::ReadarrCommand;
#[cfg(test)]
#[path = "refresh_command_handler_tests.rs"]
mod refresh_command_handler_tests;
#[derive(Debug, Clone, PartialEq, Eq, Subcommand)]
pub enum ReadarrRefreshCommand {
#[command(about = "Refresh author data and scan disk for the author with the given ID")]
Author {
#[arg(
long,
help = "The ID of the author to refresh information on and to scan the disk for",
required = true
)]
author_id: i64,
},
}
impl From<ReadarrRefreshCommand> for Command {
fn from(value: ReadarrRefreshCommand) -> Self {
Command::Readarr(ReadarrCommand::Refresh(value))
}
}
pub(super) struct ReadarrRefreshCommandHandler<'a, 'b> {
_app: &'a Arc<Mutex<App<'b>>>,
command: ReadarrRefreshCommand,
network: &'a mut dyn NetworkTrait,
}
impl<'a, 'b> CliCommandHandler<'a, 'b, ReadarrRefreshCommand>
for ReadarrRefreshCommandHandler<'a, 'b>
{
fn with(
_app: &'a Arc<Mutex<App<'b>>>,
command: ReadarrRefreshCommand,
network: &'a mut dyn NetworkTrait,
) -> Self {
ReadarrRefreshCommandHandler {
_app,
command,
network,
}
}
async fn handle(self) -> Result<String> {
let result = match self.command {
ReadarrRefreshCommand::Author { author_id } => {
let resp = self
.network
.handle_network_event(ReadarrEvent::UpdateAndScanAuthor(author_id).into())
.await?;
serde_json::to_string_pretty(&resp)?
}
};
Ok(result)
}
}
@@ -0,0 +1,102 @@
#[cfg(test)]
mod tests {
use pretty_assertions::assert_eq;
use crate::Cli;
use crate::cli::{
Command,
readarr::{ReadarrCommand, refresh_command_handler::ReadarrRefreshCommand},
};
use clap::CommandFactory;
#[test]
fn test_readarr_refresh_command_from() {
let command = ReadarrRefreshCommand::Author { author_id: 1 };
let result = Command::from(command.clone());
assert_eq!(result, Command::Readarr(ReadarrCommand::Refresh(command)));
}
mod cli {
use super::*;
use clap::Parser;
use clap::error::ErrorKind;
use pretty_assertions::assert_eq;
#[test]
fn test_refresh_author_requires_author_id() {
let result =
Cli::command().try_get_matches_from(["managarr", "readarr", "refresh", "author"]);
assert_err!(&result);
assert_eq!(
result.unwrap_err().kind(),
ErrorKind::MissingRequiredArgument
);
}
#[test]
fn test_refresh_author_with_author_id() {
let expected_args = ReadarrRefreshCommand::Author { author_id: 1 };
let result = Cli::try_parse_from([
"managarr",
"readarr",
"refresh",
"author",
"--author-id",
"1",
]);
assert_ok!(&result);
let Some(Command::Readarr(ReadarrCommand::Refresh(refresh_command))) =
result.unwrap().command
else {
panic!("Unexpected command type");
};
assert_eq!(refresh_command, expected_args);
}
}
mod handler {
use std::sync::Arc;
use mockall::predicate::eq;
use serde_json::json;
use tokio::sync::Mutex;
use crate::cli::readarr::refresh_command_handler::{
ReadarrRefreshCommand, ReadarrRefreshCommandHandler,
};
use crate::{app::App, cli::CliCommandHandler};
use crate::{
models::{Serdeable, readarr_models::ReadarrSerdeable},
network::{MockNetworkTrait, NetworkEvent, readarr_network::ReadarrEvent},
};
#[tokio::test]
async fn test_handle_refresh_author_command() {
let mut mock_network = MockNetworkTrait::new();
mock_network
.expect_handle_network_event()
.with(eq::<NetworkEvent>(
ReadarrEvent::UpdateAndScanAuthor(1).into(),
))
.times(1)
.returning(|_| {
Ok(Serdeable::Readarr(ReadarrSerdeable::Value(
json!({"testResponse": "response"}),
)))
});
let app_arc = Arc::new(Mutex::new(App::test_default()));
let refresh_command = ReadarrRefreshCommand::Author { author_id: 1 };
let result = ReadarrRefreshCommandHandler::with(&app_arc, refresh_command, &mut mock_network)
.handle()
.await;
assert_ok!(&result);
}
}
}