(0b1a129) Add the mv command
This commit is contained in:
@@ -55,4 +55,18 @@ pub enum Command {
|
||||
/// Ticket UUID or title to search for
|
||||
id: String,
|
||||
},
|
||||
|
||||
/// Move a ticket to a different status
|
||||
Mv {
|
||||
/// Ticket UUID or title
|
||||
ticket: String,
|
||||
|
||||
/// Target status (backlog, committed, reviewing, done)
|
||||
#[arg(short, long)]
|
||||
status: String,
|
||||
|
||||
/// Force move (allows backward or non-sequential moves)
|
||||
#[arg(short, long)]
|
||||
force: bool,
|
||||
},
|
||||
}
|
||||
|
||||
+111
-2
@@ -306,7 +306,7 @@ fn cmd_add(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn cmd_show(search: &str, worklane_dir: &Path) -> Result<()> {
|
||||
fn resolve_ticket(search: &str, worklane_dir: &Path) -> Result<(String, String, String)> {
|
||||
let statuses = status_dirs();
|
||||
let mut matches: Vec<(String, String, String)> = Vec::new();
|
||||
|
||||
@@ -364,7 +364,11 @@ fn cmd_show(search: &str, worklane_dir: &Path) -> Result<()> {
|
||||
std::process::exit(1);
|
||||
}
|
||||
|
||||
let (_status, title, content) = matches.remove(0);
|
||||
Ok(matches.remove(0))
|
||||
}
|
||||
|
||||
fn cmd_show(search: &str, worklane_dir: &Path) -> Result<()> {
|
||||
let (_status, title, content) = resolve_ticket(search, worklane_dir)?;
|
||||
|
||||
println!("{}", title);
|
||||
println!("{}", "─".repeat(title.len()));
|
||||
@@ -373,6 +377,108 @@ fn cmd_show(search: &str, worklane_dir: &Path) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn cmd_mv(ticket_id: &str, target_status: &str, force: bool, start: &Path) -> Result<()> {
|
||||
let valid_statuses = ["backlog", "committed", "reviewing", "done"];
|
||||
let forward_order = ["backlog", "committed", "reviewing", "done"];
|
||||
|
||||
if !valid_statuses.contains(&target_status) {
|
||||
return Err(anyhow!(
|
||||
"Invalid target status '{}'. Valid statuses: {}",
|
||||
target_status,
|
||||
valid_statuses.join(", ")
|
||||
));
|
||||
}
|
||||
|
||||
let worklane_dir = find_worklane_dir(start)?;
|
||||
let (current_status, title, _) = resolve_ticket(ticket_id, &worklane_dir)?;
|
||||
|
||||
let current_idx = forward_order
|
||||
.iter()
|
||||
.position(|&s| s == current_status)
|
||||
.unwrap();
|
||||
let target_idx = forward_order
|
||||
.iter()
|
||||
.position(|&s| s == target_status)
|
||||
.unwrap();
|
||||
|
||||
if !force && target_idx < current_idx {
|
||||
return Err(anyhow!(
|
||||
"Cannot move backward from {} to {}. Use --force (-f) to allow.",
|
||||
current_status,
|
||||
target_status
|
||||
));
|
||||
}
|
||||
|
||||
if !force && target_idx != current_idx + 1 && target_idx != current_idx {
|
||||
return Err(anyhow!(
|
||||
"Can only move one stage forward at a time. Use --force (-f) to skip stages."
|
||||
));
|
||||
}
|
||||
|
||||
if current_status == target_status {
|
||||
println!("Ticket is already in '{}' status.", target_status);
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let current_path = worklane_dir.join(¤t_status);
|
||||
let target_path = worklane_dir.join(target_status);
|
||||
|
||||
// Find the ticket file
|
||||
let mut found_filename = String::new();
|
||||
for entry in fs::read_dir(¤t_path)? {
|
||||
let entry = entry?;
|
||||
let path = entry.path();
|
||||
if path.extension().map_or(false, |e| e == "md") {
|
||||
let file_content = fs::read_to_string(&path)?;
|
||||
let file_title = file_content
|
||||
.lines()
|
||||
.find(|l| l.starts_with("# "))
|
||||
.map(|l| l[2..].trim())
|
||||
.unwrap_or("")
|
||||
.to_string();
|
||||
if file_title == title {
|
||||
found_filename = path.file_name().unwrap().to_string_lossy().to_string();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if found_filename.is_empty() {
|
||||
return Err(anyhow!("Could not find ticket file in {}", current_status));
|
||||
}
|
||||
|
||||
// Read and update the ticket content
|
||||
let mut content = fs::read_to_string(current_path.join(&found_filename))?;
|
||||
|
||||
// Update Status in Metadata section
|
||||
if let Some(start_pos) = content.find("- **Status**") {
|
||||
// Skip "- " to find "**Status**"
|
||||
let status_start = start_pos + 2;
|
||||
let after_status = &content[status_start + "**Status**".len()..]; // after "**Status**"
|
||||
if after_status.starts_with(": ") {
|
||||
let rest = &after_status[2..];
|
||||
let value_end = rest.find('\n').unwrap_or(rest.len());
|
||||
let _value = &rest[..value_end];
|
||||
let before = &content[..status_start];
|
||||
let after = &rest[value_end..];
|
||||
content = format!("{}**-Status**: {}\n{}", before, target_status, after);
|
||||
}
|
||||
}
|
||||
|
||||
// Move the file
|
||||
fs::rename(
|
||||
current_path.join(&found_filename),
|
||||
target_path.join(&found_filename),
|
||||
)?;
|
||||
|
||||
// Write back updated content
|
||||
fs::write(target_path.join(&found_filename), content)?;
|
||||
|
||||
println!("Moved ticket '{}' from {} to {}", title, current_status, target_status);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn content_trim_colored(content: &str) -> String {
|
||||
let lines: Vec<&str> = content.trim().lines().collect();
|
||||
let mut result = String::new();
|
||||
@@ -567,5 +673,8 @@ fn main() -> Result<()> {
|
||||
let worklane_dir = find_worklane_dir(&start)?;
|
||||
cmd_show(&id, &worklane_dir)
|
||||
}
|
||||
Command::Mv { ticket, status, force } => {
|
||||
cmd_mv(&ticket, &status, force, &start)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user