This commit is contained in:
@@ -0,0 +1,145 @@
|
||||
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<u64>,
|
||||
},
|
||||
}
|
||||
|
||||
fn main() {
|
||||
if let Err(error) = run() {
|
||||
eprintln!("error: {}", sanitize_terminal_text(&error.to_string()));
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
fn run() -> Result<(), Box<dyn Error>> {
|
||||
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}");
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user