Various cleanup and CLI improvements

This commit is contained in:
Daniel Flanagan 2024-04-17 13:28:16 -05:00
parent 7dfcb2ad95
commit 9b65a2b3d0
6 changed files with 62 additions and 11 deletions

View file

@ -26,15 +26,22 @@ pub struct Cli {
#[derive(Subcommand)] #[derive(Subcommand)]
pub enum Commands { pub enum Commands {
/// Lists tasks with options for syncing /// List local tasks with options for syncing
#[clap(visible_alias = "ls")]
List(list::Args), List(list::Args),
/// Sync local tasks with GitLab and Jira /// Sync local tasks with GitLab and Jira
Sync(sync::Args), 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) /// Run the interactive terminal user interface (TUI)
#[command(subcommand)] #[clap(visible_alias = "tui")]
Ui(ui::Command), Ui(ui::Args),
/// Interact with the configured Jira endpoint /// Interact with the configured Jira endpoint
#[command(subcommand)] #[command(subcommand)]
@ -45,11 +52,39 @@ pub enum Commands {
Gitlab(gitlab::Command), Gitlab(gitlab::Command),
} }
mod ui { #[derive(Args)]
use clap::Subcommand; 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)] #[derive(Args)]
pub enum Command {} 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 { mod list {

View file

View file

@ -19,7 +19,8 @@ use cli::{Cli, Commands};
async fn main() -> Result<()> { async fn main() -> Result<()> {
let cli = Cli::new(); 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 // 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)?; let _log_guard = observe::setup_logging(cli.logs_directory, cli.tracing_env_filter)?;
@ -27,8 +28,10 @@ async fn main() -> Result<()> {
trace!("Starting..."); trace!("Starting...");
info!("Starting..."); info!("Starting...");
let result = match cli.command { 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::Sync(args) => args.sync().await,
Commands::Ui(_args) => tui::run().await,
Commands::List(args) => args.list().await, Commands::List(args) => args.list().await,
Commands::Jira(jira) => jira.exec().await, Commands::Jira(jira) => jira.exec().await,
Commands::Gitlab(gitlab) => gitlab.exec().await, Commands::Gitlab(gitlab) => gitlab.exec().await,

View file

@ -68,8 +68,18 @@ impl Display for Task {
status, status,
tags, tags,
} = self; } = self;
let tags_text = tags
.iter()
.map(String::as_str)
.collect::<Vec<&str>>()
.join(", ");
let mr_text = if merge_requests.len() > 0 {
format!(" {} MRs", merge_requests.len())
} else {
"".into()
};
f.write_fmt(format_args!( f.write_fmt(format_args!(
"{jira_key} {status:>10} {jira_priority} {description}", "{jira_key} {status:>10} {jira_priority} {description} [{tags_text}]{mr_text}",
)) ))
} }
} }

View file

@ -68,6 +68,7 @@ impl Tasks {
Ok(result) Ok(result)
} }
#[allow(dead_code)]
pub fn get(&self, key: &str) -> Result<Option<Task>> { pub fn get(&self, key: &str) -> Result<Option<Task>> {
let ivec = self.db.open_tree("tasks")?.get(key)?; let ivec = self.db.open_tree("tasks")?.get(key)?;
match ivec { match ivec {
@ -206,7 +207,7 @@ impl Tasks {
self.db self.db
.open_tree("task:meta")? .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(()) Ok(())
} }

View file

@ -13,6 +13,8 @@ use ratatui::{
}; };
use std::io::stdout; use std::io::stdout;
pub struct State {}
pub async fn run() -> Result<()> { pub async fn run() -> Result<()> {
let t = Tasks::try_new()?; let t = Tasks::try_new()?;