use std::error::Error; use clap::{Parser, Subcommand}; use gitea_kanban_tui::api::{load_project_board, GiteaClient}; use gitea_kanban_tui::config::{Config, SharedArgs}; use gitea_kanban_tui::model::EditIssuePayload; use gitea_kanban_tui::operations; use gitea_kanban_tui::text::sanitize_terminal_text; #[derive(Debug, Parser)] #[command( version, about = "Control native Gitea project boards from the command line" )] struct Cli { #[command(flatten)] shared: SharedArgs, #[command(subcommand)] command: Command, } #[derive(Debug, Subcommand)] enum Command { /// Print project columns and their issues. Board, /// Create an issue assigned to selected project. Create { #[arg(long)] title: String, #[arg(long, default_value = "")] body: String, }, /// Replace issue title and body. Edit { issue: u64, #[arg(long)] title: String, #[arg(long)] body: String, }, /// Delete an issue. Requires repository admin permission. Delete { issue: u64, /// Confirm irreversible deletion. #[arg(long)] yes: bool, }, /// Move an issue to a project column. Move { issue_id: u64, #[arg(long)] column_id: u64, #[arg(long)] sorting: Option, }, /// Manage comments on an issue in selected project. Comment { #[command(subcommand)] command: CommentCommand, }, } #[derive(Debug, Subcommand)] enum CommentCommand { /// List comments for an issue. List { issue: u64 }, /// Add a comment to an issue. Add { issue: u64, #[arg(long)] body: String, }, /// Replace a comment body. Edit { issue: u64, comment_id: u64, #[arg(long)] body: String, }, /// Delete a comment. Delete { issue: u64, comment_id: u64, #[arg(long)] yes: bool, }, } fn main() { if let Err(error) = run() { eprintln!("error: {}", sanitize_terminal_text(&error.to_string())); std::process::exit(1); } } fn run() -> Result<(), Box> { let cli = Cli::parse(); let config = Config::from_shared_args_with(cli.shared, |name| std::env::var(name).ok())?; let client = GiteaClient::new(&config)?; match cli.command { Command::Board => { let board = load_project_board(&client, &config)?; println!( "{} (project {})", sanitize_terminal_text(&board.project.title), board.project.id ); for (column, issues) in board.columns.iter().zip(board.issues_by_column) { println!( "\n{} ({})", sanitize_terminal_text(&column.title), column.id ); for issue in issues { println!( " #{} {}", issue.number, sanitize_terminal_text(&issue.title) ); } } } Command::Create { title, body } => { let board = load_project_board(&client, &config)?; let issue = operations::create_project_issue(&client, board.project.id, title, body)?; println!("created issue #{}", issue.number); } Command::Edit { issue, title, body } => { let board = load_project_board(&client, &config)?; if !board .issues_by_column .iter() .flatten() .any(|candidate| candidate.number == issue) { return Err(format!("issue #{issue} is not assigned to selected project").into()); } operations::edit_issue(&client, issue, &EditIssuePayload { title, body })?; println!("updated issue #{issue}"); } Command::Delete { issue, yes } => { if !yes { return Err("deletion requires --yes".into()); } let board = load_project_board(&client, &config)?; if !board .issues_by_column .iter() .flatten() .any(|candidate| candidate.number == issue) { return Err(format!("issue #{issue} is not assigned to selected project").into()); } operations::delete_issue(&client, issue)?; println!("deleted issue #{issue}"); } Command::Move { issue_id, column_id, sorting, } => { let board = load_project_board(&client, &config)?; operations::move_project_issue( &client, board.project.id, issue_id, &gitea_kanban_tui::model::MoveProjectIssuePayload { column_id, sorting }, )?; println!("moved issue {issue_id} to column {column_id}"); } Command::Comment { command } => match command { CommentCommand::List { issue } => { for comment in project_issue_comments(&client, &config, issue)? { let author = comment .user .as_ref() .map(|user| user.login.as_str()) .unwrap_or("unknown"); println!( "#{} {}\n{}", comment.id, sanitize_terminal_text(author), sanitize_terminal_text(&comment.body) ); } } CommentCommand::Add { issue, body } => { project_issue_comments(&client, &config, issue)?; let comment = operations::create_issue_comment(&client, issue, body)?; println!("created comment #{} on issue #{}", comment.id, issue); } CommentCommand::Edit { issue, comment_id, body, } => { ensure_comment(&client, &config, issue, comment_id)?; operations::edit_issue_comment(&client, comment_id, body)?; println!("updated comment #{comment_id}"); } CommentCommand::Delete { issue, comment_id, yes, } => { if !yes { return Err("comment deletion requires --yes".into()); } ensure_comment(&client, &config, issue, comment_id)?; operations::delete_issue_comment(&client, comment_id)?; println!("deleted comment #{comment_id}"); } }, } Ok(()) } fn project_issue_comments( client: &GiteaClient, config: &Config, issue: u64, ) -> Result, Box> { let board = load_project_board(client, config)?; if !board .issues_by_column .iter() .flatten() .any(|candidate| candidate.number == issue) { return Err(format!("issue #{issue} is not assigned to selected project").into()); } Ok(operations::list_issue_comments(client, issue)?) } fn ensure_comment( client: &GiteaClient, config: &Config, issue: u64, comment_id: u64, ) -> Result<(), Box> { if !project_issue_comments(client, config, issue)? .iter() .any(|comment| comment.id == comment_id) { return Err(format!("comment #{comment_id} is not attached to issue #{issue}").into()); } Ok(()) }