From 9b65a2b3d09341d36f0d0a30ab2493bb8022099a Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Wed, 17 Apr 2024 13:28:16 -0500 Subject: [PATCH] Various cleanup and CLI improvements --- src/cli.rs | 49 ++++++++++++++++++++++++++++++++++++++++++------- src/cli/ui.rs | 0 src/main.rs | 7 +++++-- src/task.rs | 12 +++++++++++- src/tasks.rs | 3 ++- src/tui.rs | 2 ++ 6 files changed, 62 insertions(+), 11 deletions(-) delete mode 100644 src/cli/ui.rs diff --git a/src/cli.rs b/src/cli.rs index ed0ec7e..8fdcf9c 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -26,15 +26,22 @@ pub struct Cli { #[derive(Subcommand)] pub enum Commands { - /// Lists tasks with options for syncing + /// List local tasks with options for syncing + #[clap(visible_alias = "ls")] List(list::Args), /// Sync local tasks with GitLab and Jira Sync(sync::Args), + /// Purge all local data + Purge(PurgeArgs), + + /// Performs various cleanup operations on local data + Cleanup(CleanupArgs), + /// Run the interactive terminal user interface (TUI) - #[command(subcommand)] - Ui(ui::Command), + #[clap(visible_alias = "tui")] + Ui(ui::Args), /// Interact with the configured Jira endpoint #[command(subcommand)] @@ -45,11 +52,39 @@ pub enum Commands { Gitlab(gitlab::Command), } -mod ui { - use clap::Subcommand; +#[derive(Args)] +pub struct PurgeArgs {} +impl PurgeArgs { + pub async fn run(&self) -> Result<()> { + let tasks = crate::tasks::Tasks::try_new()?; + tasks.purge_all()?; + println!("Local data purged!"); + Ok(()) + } +} - #[derive(Subcommand)] - pub enum Command {} +#[derive(Args)] +pub struct CleanupArgs {} +impl CleanupArgs { + pub async fn run(&self) -> Result<()> { + let tasks = crate::tasks::Tasks::try_new()?; + tasks.cleanup_all()?; + println!("Local data cleaned up!"); + Ok(()) + } +} + +mod ui { + use crate::cli::prelude::*; + + #[derive(Parser)] + pub struct Args {} + + impl Args { + pub async fn run(&self) -> Result<()> { + crate::tui::run().await + } + } } mod list { diff --git a/src/cli/ui.rs b/src/cli/ui.rs deleted file mode 100644 index e69de29..0000000 diff --git a/src/main.rs b/src/main.rs index d964f0c..0ce707b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,7 +19,8 @@ use cli::{Cli, Commands}; async fn main() -> Result<()> { let cli = Cli::new(); - // this guard causes logs to be flushed when dropped + // this guard causes logs to be flushed when dropped (which is at the end of + // this function which would be the end of our program) // https://docs.rs/tracing-appender/latest/tracing_appender/non_blocking/struct.WorkerGuard.html let _log_guard = observe::setup_logging(cli.logs_directory, cli.tracing_env_filter)?; @@ -27,8 +28,10 @@ async fn main() -> Result<()> { trace!("Starting..."); info!("Starting..."); let result = match cli.command { + Commands::Ui(args) => args.run().await, + Commands::Purge(args) => args.run().await, + Commands::Cleanup(args) => args.run().await, Commands::Sync(args) => args.sync().await, - Commands::Ui(_args) => tui::run().await, Commands::List(args) => args.list().await, Commands::Jira(jira) => jira.exec().await, Commands::Gitlab(gitlab) => gitlab.exec().await, diff --git a/src/task.rs b/src/task.rs index 18658da..ef6af1e 100644 --- a/src/task.rs +++ b/src/task.rs @@ -68,8 +68,18 @@ impl Display for Task { status, tags, } = self; + let tags_text = tags + .iter() + .map(String::as_str) + .collect::>() + .join(", "); + let mr_text = if merge_requests.len() > 0 { + format!(" {} MRs", merge_requests.len()) + } else { + "".into() + }; f.write_fmt(format_args!( - "{jira_key} {status:>10} {jira_priority} {description}", + "{jira_key} {status:>10} {jira_priority} {description} [{tags_text}]{mr_text}", )) } } diff --git a/src/tasks.rs b/src/tasks.rs index 913606b..fe07d78 100644 --- a/src/tasks.rs +++ b/src/tasks.rs @@ -68,6 +68,7 @@ impl Tasks { Ok(result) } + #[allow(dead_code)] pub fn get(&self, key: &str) -> Result> { let ivec = self.db.open_tree("tasks")?.get(key)?; match ivec { @@ -206,7 +207,7 @@ impl Tasks { self.db .open_tree("task:meta")? - .insert(b"last_sync_date", serde_json::to_vec(&now)?); + .insert(b"last_sync_date", serde_json::to_vec(&now)?)?; Ok(()) } diff --git a/src/tui.rs b/src/tui.rs index 7f83757..7be6ae4 100644 --- a/src/tui.rs +++ b/src/tui.rs @@ -13,6 +13,8 @@ use ratatui::{ }; use std::io::stdout; +pub struct State {} + pub async fn run() -> Result<()> { let t = Tasks::try_new()?;