feat(readarr): fetch Readarr disk space and expose it on the CLI
Add ReadarrEvent::GetDiskSpace, mapped to GET /diskspace, and get_readarr_diskspace, which stores the result on ReadarrData for the root folder and system views. Surface it as `managarr readarr list disk-space`, matching the other three servarrs. Readarr returns a label field on some records that DiskSpace does not model; serde drops it, so the CLI prints only the path and the free and total space, as it does for the other three servarrs.
This commit is contained in:
7 files changed
+242
-2
No files matched your search
@@ -0,0 +1,62 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use anyhow::Result;
|
||||
use clap::Subcommand;
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
use super::ReadarrCommand;
|
||||
use crate::{
|
||||
app::App,
|
||||
cli::{CliCommandHandler, Command},
|
||||
network::{NetworkTrait, readarr_network::ReadarrEvent},
|
||||
};
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "list_command_handler_tests.rs"]
|
||||
mod list_command_handler_tests;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Subcommand)]
|
||||
pub enum ReadarrListCommand {
|
||||
#[command(about = "List disk space details for all provisioned root folders in Readarr")]
|
||||
DiskSpace,
|
||||
}
|
||||
|
||||
impl From<ReadarrListCommand> for Command {
|
||||
fn from(value: ReadarrListCommand) -> Self {
|
||||
Command::Readarr(ReadarrCommand::List(value))
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) struct ReadarrListCommandHandler<'a, 'b> {
|
||||
_app: &'a Arc<Mutex<App<'b>>>,
|
||||
command: ReadarrListCommand,
|
||||
network: &'a mut dyn NetworkTrait,
|
||||
}
|
||||
|
||||
impl<'a, 'b> CliCommandHandler<'a, 'b, ReadarrListCommand> for ReadarrListCommandHandler<'a, 'b> {
|
||||
fn with(
|
||||
_app: &'a Arc<Mutex<App<'b>>>,
|
||||
command: ReadarrListCommand,
|
||||
network: &'a mut dyn NetworkTrait,
|
||||
) -> Self {
|
||||
ReadarrListCommandHandler {
|
||||
_app,
|
||||
command,
|
||||
network,
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle(self) -> Result<String> {
|
||||
let result = match self.command {
|
||||
ReadarrListCommand::DiskSpace => {
|
||||
let resp = self
|
||||
.network
|
||||
.handle_network_event(ReadarrEvent::GetDiskSpace.into())
|
||||
.await?;
|
||||
serde_json::to_string_pretty(&resp)?
|
||||
}
|
||||
};
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::Cli;
|
||||
use crate::cli::{
|
||||
Command,
|
||||
readarr::{ReadarrCommand, list_command_handler::ReadarrListCommand},
|
||||
};
|
||||
use clap::CommandFactory;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
#[test]
|
||||
fn test_readarr_list_command_from() {
|
||||
let command = ReadarrListCommand::DiskSpace;
|
||||
|
||||
let result = Command::from(command.clone());
|
||||
|
||||
assert_eq!(result, Command::Readarr(ReadarrCommand::List(command)));
|
||||
}
|
||||
|
||||
mod cli {
|
||||
use rstest::rstest;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[rstest]
|
||||
fn test_list_commands_have_no_arg_requirements(#[values("disk-space")] subcommand: &str) {
|
||||
let result = Cli::command().try_get_matches_from(["managarr", "readarr", "list", subcommand]);
|
||||
|
||||
assert_ok!(&result);
|
||||
}
|
||||
}
|
||||
|
||||
mod handler {
|
||||
use std::sync::Arc;
|
||||
|
||||
use mockall::predicate::eq;
|
||||
use serde_json::json;
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
use crate::{
|
||||
app::App,
|
||||
cli::{
|
||||
CliCommandHandler,
|
||||
readarr::list_command_handler::{ReadarrListCommand, ReadarrListCommandHandler},
|
||||
},
|
||||
models::{Serdeable, readarr_models::ReadarrSerdeable},
|
||||
network::{MockNetworkTrait, NetworkEvent, readarr_network::ReadarrEvent},
|
||||
};
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_handle_list_disk_space_command() {
|
||||
let mut mock_network = MockNetworkTrait::new();
|
||||
mock_network
|
||||
.expect_handle_network_event()
|
||||
.with(eq::<NetworkEvent>(ReadarrEvent::GetDiskSpace.into()))
|
||||
.times(1)
|
||||
.returning(|_| {
|
||||
Ok(Serdeable::Readarr(ReadarrSerdeable::Value(
|
||||
json!({"testResponse": "response"}),
|
||||
)))
|
||||
});
|
||||
let app_arc = Arc::new(Mutex::new(App::test_default()));
|
||||
let list_disk_space_command = ReadarrListCommand::DiskSpace;
|
||||
|
||||
let result =
|
||||
ReadarrListCommandHandler::with(&app_arc, list_disk_space_command, &mut mock_network)
|
||||
.handle()
|
||||
.await;
|
||||
|
||||
assert_ok!(&result);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,12 +3,14 @@ use std::sync::Arc;
|
||||
use anyhow::Result;
|
||||
use clap::Subcommand;
|
||||
use get_command_handler::{ReadarrGetCommand, ReadarrGetCommandHandler};
|
||||
use list_command_handler::{ReadarrListCommand, ReadarrListCommandHandler};
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
use super::{CliCommandHandler, Command};
|
||||
use crate::{app::App, network::NetworkTrait};
|
||||
|
||||
mod get_command_handler;
|
||||
mod list_command_handler;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "readarr_command_tests.rs"]
|
||||
@@ -21,6 +23,11 @@ pub enum ReadarrCommand {
|
||||
about = "Commands to fetch details of the resources in your Readarr instance"
|
||||
)]
|
||||
Get(ReadarrGetCommand),
|
||||
#[command(
|
||||
subcommand,
|
||||
about = "Commands to list attributes from your Readarr instance"
|
||||
)]
|
||||
List(ReadarrListCommand),
|
||||
}
|
||||
|
||||
impl From<ReadarrCommand> for Command {
|
||||
@@ -55,6 +62,11 @@ impl<'a, 'b> CliCommandHandler<'a, 'b, ReadarrCommand> for ReadarrCliHandler<'a,
|
||||
.handle()
|
||||
.await?
|
||||
}
|
||||
ReadarrCommand::List(list_command) => {
|
||||
ReadarrListCommandHandler::with(self.app, list_command, self.network)
|
||||
.handle()
|
||||
.await?
|
||||
}
|
||||
};
|
||||
|
||||
Ok(result)
|
||||
|
||||
@@ -13,6 +13,7 @@ mod readarr_network_tests;
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Clone)]
|
||||
pub enum ReadarrEvent {
|
||||
GetDiskSpace,
|
||||
GetStatus,
|
||||
HealthCheck,
|
||||
}
|
||||
@@ -20,6 +21,7 @@ pub enum ReadarrEvent {
|
||||
impl NetworkResource for ReadarrEvent {
|
||||
fn resource(&self) -> &'static str {
|
||||
match &self {
|
||||
ReadarrEvent::GetDiskSpace => "/diskspace",
|
||||
ReadarrEvent::HealthCheck => "/health",
|
||||
ReadarrEvent::GetStatus => "/system/status",
|
||||
}
|
||||
@@ -38,6 +40,10 @@ impl Network<'_, '_> {
|
||||
readarr_event: ReadarrEvent,
|
||||
) -> Result<ReadarrSerdeable> {
|
||||
match readarr_event {
|
||||
ReadarrEvent::GetDiskSpace => self
|
||||
.get_readarr_diskspace()
|
||||
.await
|
||||
.map(ReadarrSerdeable::from),
|
||||
ReadarrEvent::GetStatus => self.get_readarr_status().await.map(ReadarrSerdeable::from),
|
||||
ReadarrEvent::HealthCheck => self
|
||||
.get_readarr_healthcheck()
|
||||
|
||||
@@ -6,6 +6,7 @@ mod tests {
|
||||
use rstest::rstest;
|
||||
|
||||
#[rstest]
|
||||
#[case(ReadarrEvent::GetDiskSpace, "/diskspace")]
|
||||
#[case(ReadarrEvent::HealthCheck, "/health")]
|
||||
#[case(ReadarrEvent::GetStatus, "/system/status")]
|
||||
fn test_resource(#[case] event: ReadarrEvent, #[case] expected_uri: &str) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::models::servarr_models::SystemStatus;
|
||||
use crate::models::servarr_models::{DiskSpace, SystemStatus};
|
||||
use crate::network::readarr_network::ReadarrEvent;
|
||||
use crate::network::{Network, RequestMethod};
|
||||
use anyhow::Result;
|
||||
@@ -9,6 +9,23 @@ use log::info;
|
||||
mod readarr_system_network_tests;
|
||||
|
||||
impl Network<'_, '_> {
|
||||
pub(in crate::network::readarr_network) async fn get_readarr_diskspace(
|
||||
&mut self,
|
||||
) -> Result<Vec<DiskSpace>> {
|
||||
info!("Fetching Readarr disk space");
|
||||
let event = ReadarrEvent::GetDiskSpace;
|
||||
|
||||
let request_props = self
|
||||
.request_props_from(event, RequestMethod::Get, None::<()>, None, None)
|
||||
.await;
|
||||
|
||||
self
|
||||
.handle_request::<(), Vec<DiskSpace>>(request_props, |disk_space_vec, mut app| {
|
||||
app.data.readarr_data.disk_space_vec = disk_space_vec;
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
pub(in crate::network::readarr_network) async fn get_readarr_status(
|
||||
&mut self,
|
||||
) -> Result<SystemStatus> {
|
||||
|
||||
@@ -1,13 +1,82 @@
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::models::readarr_models::ReadarrSerdeable;
|
||||
use crate::models::servarr_models::SystemStatus;
|
||||
use crate::models::servarr_models::{DiskSpace, SystemStatus};
|
||||
use crate::network::network_tests::test_utils::{MockServarrApi, test_network};
|
||||
use crate::network::readarr_network::ReadarrEvent;
|
||||
use chrono::DateTime;
|
||||
use pretty_assertions::{assert_eq, assert_str_eq};
|
||||
use serde_json::json;
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_handle_get_readarr_diskspace_event() {
|
||||
let (mock, app, _server) = MockServarrApi::get()
|
||||
.returns(json!([
|
||||
{
|
||||
"path": "/nfs/books/",
|
||||
"freeSpace": 11039526748160i64,
|
||||
"totalSpace": 117810875334656i64
|
||||
},
|
||||
{
|
||||
"path": "/config",
|
||||
"label": "",
|
||||
"freeSpace": 783978655744i64,
|
||||
"totalSpace": 1004298338304i64
|
||||
}
|
||||
]))
|
||||
.build_for(ReadarrEvent::GetDiskSpace)
|
||||
.await;
|
||||
app.lock().await.server_tabs.set_index(3);
|
||||
let mut network = test_network(&app);
|
||||
let disk_space_vec = vec![
|
||||
DiskSpace {
|
||||
path: Some("/nfs/books/".to_owned()),
|
||||
free_space: 11039526748160,
|
||||
total_space: 117810875334656,
|
||||
},
|
||||
DiskSpace {
|
||||
path: Some("/config".to_owned()),
|
||||
free_space: 783978655744,
|
||||
total_space: 1004298338304,
|
||||
},
|
||||
];
|
||||
|
||||
let result = network
|
||||
.handle_readarr_event(ReadarrEvent::GetDiskSpace)
|
||||
.await;
|
||||
|
||||
mock.assert_async().await;
|
||||
|
||||
let ReadarrSerdeable::DiskSpaces(disk_spaces) = result.unwrap() else {
|
||||
panic!("Expected DiskSpaces")
|
||||
};
|
||||
|
||||
assert_eq!(disk_spaces, disk_space_vec);
|
||||
assert_eq!(
|
||||
app.lock().await.data.readarr_data.disk_space_vec,
|
||||
disk_space_vec
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_handle_get_readarr_diskspace_event_failure() {
|
||||
let (mock, app, _server) = MockServarrApi::get()
|
||||
.returns(json!({}))
|
||||
.status(500)
|
||||
.build_for(ReadarrEvent::GetDiskSpace)
|
||||
.await;
|
||||
app.lock().await.server_tabs.set_index(3);
|
||||
let mut network = test_network(&app);
|
||||
|
||||
let result = network
|
||||
.handle_readarr_event(ReadarrEvent::GetDiskSpace)
|
||||
.await;
|
||||
|
||||
mock.assert_async().await;
|
||||
assert_err!(result);
|
||||
assert_is_empty!(app.lock().await.data.readarr_data.disk_space_vec);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_handle_get_readarr_status_event() {
|
||||
let (mock, app, _server) = MockServarrApi::get()
|
||||
|
||||
Reference in new issue
Block a user