use super::{Error, Fx, Outcome};
use crate::prompt::retarget::preflight;
use crate::template::RealGit;
use crate::workspace::{self, DEFAULT_CONFIG_NAME};
use std::path::PathBuf;
#[derive(clap::Args, Debug)]
pub struct Args {
pub workspace: PathBuf,
pub agent: String,
#[arg(long)]
pub config: Option<String>,
}
pub fn run(args: Args, _fx: &mut Fx) -> Result<Outcome, Error> {
let e = |source: &dyn std::fmt::Display| Error::new("retarget", source);
crate::name::require_agent_id(&args.agent).map_err(|s| e(&s))?;
let name = args.config.as_deref().unwrap_or(DEFAULT_CONFIG_NAME);
let git = RealGit::new();
let target = preflight(&args.workspace, &args.agent, name, &git).map_err(|s| e(&s))?;
match target {
None => eprintln!(
"lernie: {} already governs [{}] — nothing to retarget",
workspace::config_ref(name),
args.agent,
),
Some(commit) => {
workspace::retarget::write(&args.workspace, &args.agent, &commit, &git)
.map_err(|s| e(&s))?;
eprintln!(
"lernie: [{}] marked for retarget onto {} ({}); it lands at the agent's next \
step (ARCH §2.2)",
args.agent,
&commit[..commit.len().min(12)],
workspace::config_ref(name),
);
}
}
Ok(Outcome::Quiet)
}