wsb 0.291.8591

Workspace tools for every project under $PRJ: git across submodules, template stamping, GitHub, and the workbench
Documentation
//! `wsb project` — what wsb keeps for a project as a whole. See
//! [`crate::project_rename`].

pub const USAGE: &str = "\
Usage: wsb project <command>

  rename <old> <new>  after the project's folder has moved to $PRJ/<new>
                      (`prj rename-project` moves it): move what wsb keeps
                      under the old name — the worktrees' local state, their
                      test catalogues, open folders, sessions and workbench
                      tabs — to the new one; refused while the daemon runs";

pub fn dispatch(args: &[String]) -> u8 {
    let verb = args.get(2).map(String::as_str);
    let rest = if args.len() > 3 { &args[3..] } else { &[] };
    let result = match verb {
        Some("rename") => rename(rest),
        None | Some("help") | Some("--help") | Some("-h") => {
            crate::outln!("{USAGE}");
            return 0;
        }
        Some(other) => {
            crate::errln!("wsb project: unknown command '{other}'\n\n{USAGE}");
            return 2;
        }
    };
    match result {
        Ok(()) => 0,
        Err(message) => {
            crate::errln!("wsb project: {message}");
            1
        }
    }
}

fn rename(rest: &[String]) -> Result<(), String> {
    let [old, new] = rest else {
        return Err(format!("name the project's old name and its new one\n\n{USAGE}"));
    };
    let workshop = wsb_prj::Workshop::from_env().map_err(|e| e.to_string())?;
    for line in crate::project_rename::rename(&workshop, old, new).map_err(|e| format!("{e:#}"))? {
        crate::outln!("{line}");
    }
    crate::outln!("renamed project {old} to {new}");
    Ok(())
}