(d637288) Add campaign set, and bump to 0.21.0

`campaign add --codename` was the only writer of the field, so a campaign
already on the board could not be given one without editing the file by
hand -- which is every campaign filed before 0.20.0.

A `campaign set`, not a flag on something narrower: the file has more than
one editable thing, and the moment somebody wants to fix a typo in a title
there would be a second command to design. It carries `--title`, which
renames the file the way `set --title` does, and `--codename`, through the
same parser `campaign add` takes. Built out of `set`'s own parts --
`set_title`, `apply`, `report`, and the whole-file no-op comparison -- so
nothing about writing a field lives twice.

The codename goes through `set_or_insert_metadata_field` rather than
`set_metadata_field`: a campaign filed before the field existed has no such
line, and those are exactly the campaigns this is for. That exposed a
second thing -- `Codename` was not in `METADATA_ORDER`, so an inserted line
landed at the end of the block while `campaign_body` writes it under the
ID, two shapes of one file. It is in the list now, directly after ID.

`save_ticket` split into `save_entry` with `save_ticket` and
`save_campaign` as named wrappers, so a campaign gets the same write-then-
rename a ticket has rather than a second copy of it. `write_then_replace`
and `find_entry_file` take the noun, so a failure says "campaign" where it
is one.

No `--description`, filed as 32e98ab: `set` and `append` are a pair for a
prose section, and replacing a description with no way to add a paragraph
is the half that loses the rest of it. No `--owner` and no `--clear`, each
for a reason recorded on the ticket.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ansho Rei
2026-08-23 12:22:59 +02:00
co-authored by Claude Opus 5
parent 528566d103
commit 72dbdec2ec
12 changed files with 463 additions and 33 deletions
+121 -19
View File
@@ -7,7 +7,8 @@ use std::path::Path;
use crate::campaign::*;
use crate::command::{
AddArgs, AppendArgs, CampaignAddArgs, CheckArgs, ChownArgs, LsArgs, RelateArgs, SetArgs,
AddArgs, AppendArgs, CampaignAddArgs, CampaignSetArgs, CheckArgs, ChownArgs, LsArgs,
RelateArgs, SetArgs,
};
use crate::install;
use crate::util::*;
@@ -442,10 +443,19 @@ pub fn cmd_show(search: &str, worklane_dir: &Path) -> Result<()> {
Ok(())
}
/// What is being written, for the messages a failure carries.
///
/// A `&str` naming the thing rather than two copies of the write path: a campaign file is
/// a ticket file in every way that matters here, `<uuid>_<slug>.md` in a directory, and the
/// only difference is the word a failure has to use. Every message below puts it
/// mid-sentence so there is no capital to case.
const TICKET: &str = "ticket";
const CAMPAIGN: &str = "campaign";
/// Write `content` over `path`, without ever leaving half a file behind.
///
/// Staged in a temp file and renamed into place, because `fs::write` truncates before
/// it writes and a partial write would corrupt the ticket. The temp sits under
/// it writes and a partial write would corrupt the file. The temp sits under
/// `.worklane/` rather than beside the ticket, so a leftover from a hard kill is
/// invisible to ticket lookup and to the listing instead of showing up as a stray
/// untitled ticket.
@@ -454,6 +464,7 @@ fn write_then_replace(
path: &Path,
filename: &str,
content: &str,
kind: &str,
title: &str,
) -> Result<()> {
let temp = worklane_dir.join(format!(".wl-{}.tmp", filename));
@@ -461,7 +472,8 @@ fn write_then_replace(
if let Err(e) = fs::write(&temp, content) {
let _ = fs::remove_file(&temp);
return Err(anyhow!(
"Could not stage the update for ticket '{}' at {}: {}",
"Could not stage the update for {} '{}' at {}: {}",
kind,
title,
temp.display(),
e
@@ -470,21 +482,29 @@ fn write_then_replace(
if let Err(e) = fs::rename(&temp, path) {
let _ = fs::remove_file(&temp);
return Err(anyhow!("Could not write ticket '{}' at {}: {}", title, path.display(), e));
return Err(anyhow!(
"Could not write {} '{}' at {}: {}",
kind,
title,
path.display(),
e
));
}
Ok(())
}
/// The ticket file in `dir` carrying `uuid`.
/// The file in `dir` carrying `uuid`, ticket or campaign.
///
/// Matched on the uuid, not the title. Titles are not unique, `add` allows duplicates
/// on purpose, so a title scan could land on a different ticket than the one that was
/// resolved: `mv <uuid-of-second>` moved the first ticket sharing its title.
fn find_ticket_file(dir: &Path, uuid: &str, title: &str) -> Result<String> {
/// resolved: `mv <uuid-of-second>` moved the first ticket sharing its title. The same holds
/// for a campaign, whose file is named the same way.
fn find_entry_file(dir: &Path, kind: &str, uuid: &str, title: &str) -> Result<String> {
if uuid.trim().is_empty() || is_unknown_value(uuid) {
return Err(anyhow!(
"Ticket '{}' has no ID field, so its file cannot be located",
"Could not locate the file for {} '{}': it has no ID field",
kind,
title
));
}
@@ -507,7 +527,8 @@ fn find_ticket_file(dir: &Path, uuid: &str, title: &str) -> Result<String> {
}
Err(anyhow!(
"Could not find the file for ticket '{}' in {}",
"Could not find the file for {} '{}' in {}",
kind,
title,
dir.display()
))
@@ -537,7 +558,7 @@ fn move_ticket_file(
let source = from.join(filename);
let dest = to.join(filename);
write_then_replace(worklane_dir, &source, filename, content, title)?;
write_then_replace(worklane_dir, &source, filename, content, TICKET, title)?;
// The content is correct and in one place by now, so a failure here leaves the
// ticket where it started rather than half moved.
@@ -660,7 +681,7 @@ pub fn cmd_mv(ticket_id: &str, target: Status, force: bool, start: &Path) -> Res
)?;
let found_filename =
find_ticket_file(&current_path, &parse_ticket_field(&resolved, "ID"), &title)?;
find_entry_file(&current_path, TICKET, &parse_ticket_field(&resolved, "ID"), &title)?;
move_ticket_file(
&worklane_dir,
@@ -756,21 +777,56 @@ fn save_ticket(
new_title: Option<&String>,
content: &str,
) -> Result<()> {
let status_path = worklane_dir.join(status);
let filename = find_ticket_file(&status_path, uuid, title)?;
let path = status_path.join(&filename);
let dir = worklane_dir.join(status);
save_entry(worklane_dir, &dir, TICKET, uuid, title, new_title, content)
}
write_then_replace(worklane_dir, &path, &filename, content, title)?;
/// The same for a campaign, whose directory is `campaigns/` and which sits in no status.
///
/// A named wrapper rather than a `save_entry` at the call site, so the campaign handler
/// reads the way the ticket handlers do and neither has to know which directory the other
/// writes to.
fn save_campaign(
worklane_dir: &Path,
uuid: &str,
title: &str,
new_title: Option<&String>,
content: &str,
) -> Result<()> {
let dir = campaigns_dir(worklane_dir);
save_entry(worklane_dir, &dir, CAMPAIGN, uuid, title, new_title, content)
}
// The uuid is kept, so the new name cannot collide with another ticket, and an
/// Write a file back where it was, renaming it when the title changed.
///
/// One body for tickets and campaigns. Both are `<uuid>_<slug>.md` in a directory, both are
/// written through `write_then_replace` so a failure leaves no half file, and both rename
/// only after the write has landed: renaming first and writing second is what leaves a file
/// under a name that does not match what is in it.
fn save_entry(
worklane_dir: &Path,
dir: &Path,
kind: &str,
uuid: &str,
title: &str,
new_title: Option<&String>,
content: &str,
) -> Result<()> {
let filename = find_entry_file(dir, kind, uuid, title)?;
let path = dir.join(&filename);
write_then_replace(worklane_dir, &path, &filename, content, kind, title)?;
// The uuid is kept, so the new name cannot collide with another entry, and an
// unchanged slug means there is nothing to rename.
if let Some(new_title) = new_title {
let renamed = format!("{}_{}.md", uuid, slug(new_title));
if renamed != filename {
let destination = status_path.join(&renamed);
let destination = dir.join(&renamed);
fs::rename(&path, &destination).map_err(|e| {
anyhow!(
"Ticket '{}' was updated but could not be renamed to {}: {}",
"Could not rename {} '{}' to {} after updating it: {}",
kind,
title,
destination.display(),
e
@@ -1065,7 +1121,7 @@ pub fn cmd_campaign_add(args: &CampaignAddArgs, start: &Path) -> Result<()> {
&Local::now().format(TIMESTAMP_FORMAT).to_string(),
);
write_then_replace(&worklane_dir, &path, &filename, &body, title)?;
write_then_replace(&worklane_dir, &path, &filename, &body, CAMPAIGN, title)?;
println!("Created campaign: {}", filename);
Ok(())
@@ -1172,6 +1228,52 @@ pub fn cmd_tags(worklane_dir: &Path) -> Result<()> {
Ok(())
}
/// Replace fields on a campaign that is already filed.
///
/// `cmd_set` for a campaign, and built out of the same parts: `set_title` for the heading,
/// `set_metadata_field` for the field, `apply` for whether an edit changed anything, and
/// `report` for what it says afterwards. Nothing about writing a field lives twice, which is
/// the shape `cmd_chown` already took.
///
/// The **codename is inserted when the campaign has no such line**, rather than refused.
/// `set_metadata_field` refuses a field that is not there, which is right for the fields
/// `add` writes; every campaign filed before the field existed has none, and those are
/// exactly the campaigns this command is for. So it goes through
/// `set_or_insert_metadata_field`, the upsert `assign` already uses for the Campaign field
/// on a ticket, for the same reason.
///
/// No-op detection compares the whole file, the way `set` and `chown` do, so setting a
/// codename a campaign already carries writes nothing.
pub fn cmd_campaign_set(args: &CampaignSetArgs, start: &Path) -> Result<()> {
let worklane_dir = find_worklane_dir(start)?;
let campaign = resolve_campaign(&args.campaign, &worklane_dir)?;
let title = campaign.title.clone();
let mut content = campaign.content.clone();
let mut changed: Vec<&str> = Vec::new();
if let Some(new_title) = &args.title {
let next = set_title(&content, new_title)?;
apply(&mut content, &mut changed, "title", next);
}
if let Some(codename) = &args.codename {
let next = set_or_insert_metadata_field(&content, CODENAME_FIELD, codename, &title)?;
apply(&mut content, &mut changed, "codename", next);
}
if changed.is_empty() {
report(&[], &title, "Updated");
return Ok(());
}
let renamed = changed.contains(&"title").then_some(()).and(args.title.as_ref());
save_campaign(&worklane_dir, &campaign.uuid, &title, renamed, &content)?;
report(&changed, args.title.as_ref().unwrap_or(&title), "Updated");
Ok(())
}
/// Show a campaign, then its members grouped by status.
///
/// Every status is shown, done included, unlike `ls`, which leaves done out of its