From c77bcaed5fe22fab8557c5aadf57c344cb9da90c Mon Sep 17 00:00:00 2001 From: yellowhatpro Date: Tue, 27 Aug 2024 11:17:54 +0530 Subject: [PATCH] refactor:updated cli commands --- src/cli/args.rs | 21 ++++++------------ src/cli/mod.rs | 55 ++++++++++++++++++------------------------------ src/cli/utils.rs | 18 ++++++++++++---- 3 files changed, 41 insertions(+), 53 deletions(-) diff --git a/src/cli/args.rs b/src/cli/args.rs index 043d294..c9a23cf 100644 --- a/src/cli/args.rs +++ b/src/cli/args.rs @@ -18,20 +18,13 @@ pub struct CliArgs { #[derive(Subcommand, Debug)] pub enum Commands { ///Queue a single URL to be archived in Internet Archive History - QueueURL { - url: Option, - }, - /// Queue a Edit Data row to be archived in Internet Archive History - QueueEditData { - row_id: Option, - }, - /// Queue a Edit Note row to be archived in Internet Archive History - QueueEditNote { - row_id: Option, - }, - CheckStatus { - job_id: Option, - }, + QueueURL { url: String }, + /// Queue an Edit Data row to be archived in Internet Archive History + QueueEditData { row_id: i32 }, + /// Queue an Edit Note row to be archived in Internet Archive History + QueueEditNote { row_id: i32 }, + /// Check the archival status of any URL by `job_id` + CheckStatus { job_id: String }, /// Start the app to poll from Edit Data and Edit Note tables. It is the default behaviour Poll, } diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 65f73de..18087f9 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -1,4 +1,5 @@ use crate::cli::args::{CliArgs, Commands}; +use crate::cli::utils::check_before_inserting_url; use clap::Parser; use colorize::AnsiColor; use sqlx::PgPool; @@ -10,13 +11,6 @@ pub async fn start(pool: &PgPool) { let args = CliArgs::parse(); match &args.command { Some(Commands::QueueEditData { row_id }) => { - // Argument check - // TODO: Concider making the argument mandatory by removing the `Option`? - let Some(row_id) = row_id else { - println!("{}", "Please pass row id".red()); - return; - }; - match utils::insert_edit_data_row_to_internet_archive_urls(*row_id, pool).await { // We got urls! Ok(true) => println!( @@ -37,13 +31,6 @@ pub async fn start(pool: &PgPool) { } Some(Commands::QueueEditNote { row_id }) => { - // Argument check - // TODO: Concider making the argument mandatory by removing the `Option`? - let Some(row_id) = row_id else { - println!("{}", "Please pass row id".red()); - return; - }; - match utils::insert_edit_note_row_to_internet_archive_urls(*row_id, pool).await { // We got urls! Ok(true) => println!( @@ -62,15 +49,16 @@ pub async fn start(pool: &PgPool) { } } } - Some(Commands::QueueURL { url }) => { - // Argument check - // TODO: Concider making the argument mandatory by removing the `Option`? - let Some(url) = url else { - println!("{}", "Please pass URL".red()); - return; - }; + Some(Commands::QueueURL { url }) => match check_before_inserting_url(url, pool).await { + Ok(false) => { + println!("{}", "URL is already queued: ".red(),); + } + Err(err) => { + println!("{}", "Some error occurred".red()); + eprintln!("{err}"); + } - match utils::insert_url_to_internet_archive_urls(url, pool).await { + Ok(true) => match utils::insert_url_to_internet_archive_urls(url, pool).await { Ok(id) => println!( "{} {}", "URL queued in internet_archive_urls, id: ".green(), @@ -81,20 +69,17 @@ pub async fn start(pool: &PgPool) { println!("{}", "Some error occurred".red()); eprintln!("{err}"); } - } - } + }, + }, Some(Commands::CheckStatus { job_id }) => { - // Argument check - // TODO: Concider making the argument mandatory by removing the `Option`? - let Some(job_id) = job_id else { - println!("{}", "Please pass job id".red()); - return; - }; - - println!("Job id: {:?}", job_id); - utils::get_job_id_status(job_id.to_owned(), pool) - .await - .unwrap(); + match utils::get_job_id_status(job_id.as_str(), pool).await { + Ok(res) => { + println!("Status: {}", res.status) + } + Err(e) => { + println!("Failed: {}", e) + } + } } Some(Commands::Poll) | None => { diff --git a/src/cli/utils.rs b/src/cli/utils.rs index 0d2904f..a512668 100644 --- a/src/cli/utils.rs +++ b/src/cli/utils.rs @@ -1,4 +1,8 @@ +use crate::archival::archival_response::ArchivalStatusResponse; +use crate::archival::error::ArchivalError; +use crate::archival::utils::make_archival_status_request; use crate::poller; +use crate::poller::utils::should_insert_url_to_internet_archive_urls; use colorize::AnsiColor; use mb_rs::schema::{EditData, EditNote}; use sqlx::{Error, PgPool}; @@ -18,6 +22,10 @@ pub async fn insert_url_to_internet_archive_urls(url: &str, pool: &PgPool) -> Re .map(|result| result.id) } +pub async fn check_before_inserting_url(url: &str, pool: &PgPool) -> Result { + should_insert_url_to_internet_archive_urls(url, pool).await +} + /// This function takes in an `edit_data` `row_id`, extract the urls contained inside, then insert them into the `internet_archive_urls` table pub async fn insert_edit_data_row_to_internet_archive_urls( row_id: i32, @@ -84,8 +92,10 @@ pub async fn insert_edit_note_row_to_internet_archive_urls( Ok(!urls.is_empty()) } -pub async fn get_job_id_status(job_id: String, _pool: &PgPool) -> Result<&str, Error> { - // TODO: Concider using &str for job_id? - println!("job_id: {},", job_id); - Ok("") +pub async fn get_job_id_status( + job_id: &str, + _pool: &PgPool, +) -> Result { + let status = make_archival_status_request(job_id).await?; + Ok(status) }