feat(sonarr): Added CLI support for listing Sonarr logs
This commit is contained in:
@@ -21,6 +21,16 @@ mod list_command_handler_tests;
|
|||||||
pub enum SonarrListCommand {
|
pub enum SonarrListCommand {
|
||||||
#[command(about = "List all items in the Sonarr blocklist")]
|
#[command(about = "List all items in the Sonarr blocklist")]
|
||||||
Blocklist,
|
Blocklist,
|
||||||
|
#[command(about = "Fetch Sonarr logs")]
|
||||||
|
Logs {
|
||||||
|
#[arg(long, help = "How many log events to fetch", default_value_t = 500)]
|
||||||
|
events: u64,
|
||||||
|
#[arg(
|
||||||
|
long,
|
||||||
|
help = "Output the logs in the same format as they appear in the log files"
|
||||||
|
)]
|
||||||
|
output_in_log_format: bool,
|
||||||
|
},
|
||||||
#[command(about = "List all series in your Sonarr library")]
|
#[command(about = "List all series in your Sonarr library")]
|
||||||
Series,
|
Series,
|
||||||
}
|
}
|
||||||
@@ -55,6 +65,25 @@ impl<'a, 'b> CliCommandHandler<'a, 'b, SonarrListCommand> for SonarrListCommandH
|
|||||||
SonarrListCommand::Blocklist => {
|
SonarrListCommand::Blocklist => {
|
||||||
execute_network_event!(self, SonarrEvent::GetBlocklist);
|
execute_network_event!(self, SonarrEvent::GetBlocklist);
|
||||||
}
|
}
|
||||||
|
SonarrListCommand::Logs {
|
||||||
|
events,
|
||||||
|
output_in_log_format,
|
||||||
|
} => {
|
||||||
|
let logs = self
|
||||||
|
.network
|
||||||
|
.handle_network_event(SonarrEvent::GetLogs(Some(events)).into())
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
if output_in_log_format {
|
||||||
|
let log_lines = self.app.lock().await.data.sonarr_data.logs.items.clone();
|
||||||
|
|
||||||
|
let json = serde_json::to_string_pretty(&log_lines)?;
|
||||||
|
println!("{}", json);
|
||||||
|
} else {
|
||||||
|
let json = serde_json::to_string_pretty(&logs)?;
|
||||||
|
println!("{}", json);
|
||||||
|
}
|
||||||
|
}
|
||||||
SonarrListCommand::Series => {
|
SonarrListCommand::Series => {
|
||||||
execute_network_event!(self, SonarrEvent::ListSeries);
|
execute_network_event!(self, SonarrEvent::ListSeries);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,8 @@ mod tests {
|
|||||||
|
|
||||||
mod cli {
|
mod cli {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use clap::{error::ErrorKind, Parser};
|
||||||
|
use pretty_assertions::assert_eq;
|
||||||
use rstest::rstest;
|
use rstest::rstest;
|
||||||
|
|
||||||
#[rstest]
|
#[rstest]
|
||||||
@@ -28,6 +30,30 @@ mod tests {
|
|||||||
|
|
||||||
assert!(result.is_ok());
|
assert!(result.is_ok());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_list_logs_events_flag_requires_arguments() {
|
||||||
|
let result =
|
||||||
|
Cli::command().try_get_matches_from(["managarr", "sonarr", "list", "logs", "--events"]);
|
||||||
|
|
||||||
|
assert!(result.is_err());
|
||||||
|
assert_eq!(result.unwrap_err().kind(), ErrorKind::InvalidValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_list_logs_default_values() {
|
||||||
|
let expected_args = SonarrListCommand::Logs {
|
||||||
|
events: 500,
|
||||||
|
output_in_log_format: false,
|
||||||
|
};
|
||||||
|
let result = Cli::try_parse_from(["managarr", "sonarr", "list", "logs"]);
|
||||||
|
|
||||||
|
assert!(result.is_ok());
|
||||||
|
|
||||||
|
if let Some(Command::Sonarr(SonarrCommand::List(refresh_command))) = result.unwrap().command {
|
||||||
|
assert_eq!(refresh_command, expected_args);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mod handler {
|
mod handler {
|
||||||
@@ -39,8 +65,9 @@ mod tests {
|
|||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
use tokio::sync::Mutex;
|
use tokio::sync::Mutex;
|
||||||
|
|
||||||
use crate::cli::sonarr::list_command_handler::SonarrListCommand;
|
use crate::cli::sonarr::list_command_handler::{SonarrListCommand, SonarrListCommandHandler};
|
||||||
use crate::cli::CliCommandHandler;
|
use crate::cli::CliCommandHandler;
|
||||||
|
use crate::models::sonarr_models::SonarrSerdeable;
|
||||||
use crate::network::sonarr_network::SonarrEvent;
|
use crate::network::sonarr_network::SonarrEvent;
|
||||||
use crate::{
|
use crate::{
|
||||||
app::App,
|
app::App,
|
||||||
@@ -56,8 +83,6 @@ mod tests {
|
|||||||
#[case] list_command: SonarrListCommand,
|
#[case] list_command: SonarrListCommand,
|
||||||
#[case] expected_sonarr_event: SonarrEvent,
|
#[case] expected_sonarr_event: SonarrEvent,
|
||||||
) {
|
) {
|
||||||
use crate::cli::sonarr::list_command_handler::SonarrListCommandHandler;
|
|
||||||
|
|
||||||
let mut mock_network = MockNetworkTrait::new();
|
let mut mock_network = MockNetworkTrait::new();
|
||||||
mock_network
|
mock_network
|
||||||
.expect_handle_network_event()
|
.expect_handle_network_event()
|
||||||
@@ -76,5 +101,33 @@ mod tests {
|
|||||||
|
|
||||||
assert!(result.is_ok());
|
assert!(result.is_ok());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_handle_list_logs_command() {
|
||||||
|
let expected_events = 1000;
|
||||||
|
let mut mock_network = MockNetworkTrait::new();
|
||||||
|
mock_network
|
||||||
|
.expect_handle_network_event()
|
||||||
|
.with(eq::<NetworkEvent>(
|
||||||
|
SonarrEvent::GetLogs(Some(expected_events)).into(),
|
||||||
|
))
|
||||||
|
.times(1)
|
||||||
|
.returning(|_| {
|
||||||
|
Ok(Serdeable::Sonarr(SonarrSerdeable::Value(
|
||||||
|
json!({"testResponse": "response"}),
|
||||||
|
)))
|
||||||
|
});
|
||||||
|
let app_arc = Arc::new(Mutex::new(App::default()));
|
||||||
|
let list_logs_command = SonarrListCommand::Logs {
|
||||||
|
events: 1000,
|
||||||
|
output_in_log_format: false,
|
||||||
|
};
|
||||||
|
|
||||||
|
let result = SonarrListCommandHandler::with(&app_arc, list_logs_command, &mut mock_network)
|
||||||
|
.handle()
|
||||||
|
.await;
|
||||||
|
|
||||||
|
assert!(result.is_ok());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user