Initial commit
This commit is contained in:
+166
@@ -0,0 +1,166 @@
|
||||
use anyhow::{anyhow, Result};
|
||||
use clap::Parser;
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
|
||||
mod command;
|
||||
|
||||
use command::*;
|
||||
|
||||
fn find_worklane_dir(start: &Path) -> Result<std::path::PathBuf> {
|
||||
let mut current = start.canonicalize()?;
|
||||
|
||||
loop {
|
||||
let worklane_dir = current.join(".worklane");
|
||||
if worklane_dir.exists() {
|
||||
return Ok(worklane_dir);
|
||||
}
|
||||
|
||||
if !current.pop() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Err(anyhow!(
|
||||
"No .worklane directory found. Run `worklane init` first."
|
||||
))
|
||||
}
|
||||
|
||||
fn slug(title: &str) -> String {
|
||||
title
|
||||
.to_lowercase()
|
||||
.chars()
|
||||
.filter(|c| c.is_alphanumeric() || *c == ' ' || *c == '-' || *c == '_')
|
||||
.map(|c| if c == ' ' { '-' } else { c })
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn cmd_init(start: &Path) -> Result<()> {
|
||||
let dir = start.canonicalize().unwrap_or_else(|_| start.to_path_buf());
|
||||
let worklane_dir = dir.join(".worklane");
|
||||
|
||||
if worklane_dir.exists() {
|
||||
println!("worklane already initialized in: {}", worklane_dir.display());
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
fs::create_dir_all(&worklane_dir)?;
|
||||
|
||||
let folders = ["backlog", "committed", "reviewing", "done"];
|
||||
for folder in &folders {
|
||||
fs::create_dir_all(worklane_dir.join(folder))?;
|
||||
}
|
||||
|
||||
println!("Initialized worklane in: {}", worklane_dir.display());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn cmd_add(
|
||||
title: &str,
|
||||
description: &Option<String>,
|
||||
acceptance: &[String],
|
||||
plan: &[String],
|
||||
tags: &[String],
|
||||
due: &Option<String>,
|
||||
owner: &Option<String>,
|
||||
start: &Path,
|
||||
) -> Result<()> {
|
||||
let worklane_dir = find_worklane_dir(start)?;
|
||||
let backlog_dir = worklane_dir.join("backlog");
|
||||
|
||||
if !backlog_dir.exists() {
|
||||
return Err(anyhow!(
|
||||
"Backlog directory not found. Run `worklane init` first."
|
||||
));
|
||||
}
|
||||
|
||||
let id = uuid::Uuid::new_v4();
|
||||
let slug = slug(title);
|
||||
let filename = format!("{}_{}.md", id, slug);
|
||||
let filepath = backlog_dir.join(&filename);
|
||||
|
||||
if filepath.exists() {
|
||||
return Err(anyhow!("A ticket with this title already exists."));
|
||||
}
|
||||
|
||||
let description_text = description
|
||||
.as_deref()
|
||||
.unwrap_or("_No description provided._");
|
||||
|
||||
let mut body = format!(
|
||||
"# {}\n\
|
||||
\n\
|
||||
## Description\n\
|
||||
{}\n\
|
||||
\n\
|
||||
## Acceptance Criteria\n\
|
||||
{}\n\
|
||||
\n\
|
||||
## Implementation Plan\n\
|
||||
{}\n\
|
||||
\n\
|
||||
## Metadata\n\
|
||||
\n\
|
||||
- **ID**: {}\n\
|
||||
- **Tags**: {}\n\
|
||||
- **Due**: {}\n\
|
||||
- **Owner**: {}\n\
|
||||
- **Status**: backlog\n\
|
||||
",
|
||||
title,
|
||||
description_text,
|
||||
if acceptance.is_empty() {
|
||||
"_No acceptance criteria provided._".to_string()
|
||||
} else {
|
||||
acceptance
|
||||
.iter()
|
||||
.map(|c| format!("- [ ] {}", c))
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n")
|
||||
},
|
||||
if plan.is_empty() {
|
||||
"_No implementation plan provided._".to_string()
|
||||
} else {
|
||||
plan.iter()
|
||||
.enumerate()
|
||||
.map(|(i, s)| format!("{}. {}", i + 1, s))
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n")
|
||||
},
|
||||
id,
|
||||
if tags.is_empty() {
|
||||
"_None_".to_string()
|
||||
} else {
|
||||
tags.join(", ")
|
||||
},
|
||||
due.as_deref().unwrap_or("_Not set_"),
|
||||
owner.as_deref().unwrap_or("_Not set_"),
|
||||
);
|
||||
body.push('\n');
|
||||
|
||||
fs::write(&filepath, body)?;
|
||||
|
||||
println!("Created ticket: {}", filename);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let cli = Cli::parse();
|
||||
|
||||
let start = std::env::current_dir()?;
|
||||
|
||||
match cli.command {
|
||||
Command::Init => cmd_init(&start),
|
||||
Command::Add {
|
||||
title,
|
||||
description,
|
||||
acceptance,
|
||||
plan,
|
||||
tags,
|
||||
due,
|
||||
owner,
|
||||
} => cmd_add(
|
||||
&title, &description, &acceptance, &plan, &tags, &due, &owner, &start,
|
||||
),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user