use std::path::PathBuf;
use agent_loadout_core::apply::journal::{self, Journal};
use agent_loadout_core::apply::{self, Plan, PlanOptions};
use agent_loadout_core::{CoreError, ErrorCode};
use clap::Args;
use serde_json::{Value, json};
use super::apply::ScopeArg;
use super::project::selection;
use super::{
Context, EXIT_PENDING, JSON_SCHEMA, entry_json, error_json, path_json, print_json,
read_only_json, read_only_note,
};
#[derive(Debug, Args)]
pub struct StatusArgs {
path: Option<PathBuf>,
#[arg(long = "client", value_name = "CLIENT")]
clients: Vec<String>,
#[arg(long, value_enum, default_value = "all")]
scope: ScopeArg,
#[arg(long)]
check: bool,
}
pub fn run(ctx: &Context, args: &StatusArgs) -> Result<u8, CoreError> {
super::eject::note_if_ejected(ctx)?;
let options = PlanOptions {
clients: (!args.clients.is_empty()).then(|| args.clients.clone()),
scope: args.scope.into(),
projects: selection(ctx, args.path.as_deref())?,
..PlanOptions::default()
};
let store = ctx.open_store()?;
let ax = ctx.apply_env();
let plan = apply::preview(store.as_ref(), &ax, &options)?;
let unfinished = journal::unfinished(ax.fs, &ctx.home.state_dir())?;
if ctx.json {
print_json(&json!({
"schema": JSON_SCHEMA,
"targets": plan_json(&plan),
"warnings": plan.warnings.iter().map(error_json).collect::<Vec<_>>(),
"unfinished": unfinished.iter().map(journal_json).collect::<Vec<_>>(),
}));
} else {
print_plan(&plan);
if store.is_none() {
println!("{}", super::NO_STORE_HINT);
}
for run in &unfinished {
let err = CoreError::new(
ErrorCode::UnfinishedApply,
format!(
"apply {} started {} did not finish ({} file(s) recorded)",
run.apply_id,
run.started_at,
run.files.len()
),
);
eprintln!("warning[{}]: {err}", err.code());
}
}
let pending = plan
.targets
.iter()
.flat_map(|target| &target.entries)
.any(|entry| entry.state.is_pending() || entry.unresolved);
Ok(if args.check && (pending || !unfinished.is_empty()) {
EXIT_PENDING
} else {
0
})
}
pub fn plan_json(plan: &Plan) -> Vec<Value> {
plan.targets
.iter()
.map(|target| {
json!({
"clients": target.clients,
"scope": target.scope,
"project_id": target.project_id,
"file": path_json(&target.file),
"read_only": read_only_json(target.read_only),
"problem": target.problem.as_ref().map(error_json),
"warnings": target.warnings.iter().map(error_json).collect::<Vec<_>>(),
"entries": target.entries.iter().map(entry_json).collect::<Vec<_>>(),
})
})
.collect()
}
fn journal_json(run: &Journal) -> Value {
serde_json::to_value(run).unwrap_or(Value::Null)
}
pub fn print_plan(plan: &Plan) {
if plan.targets.is_empty() {
println!("Nothing to manage: no server is enabled and nothing is owned.");
}
for target in &plan.targets {
println!(
"{} ({}){}",
target.file.display(),
target.clients.join(", "),
read_only_note(target.read_only)
);
if let Some(problem) = &target.problem {
println!(" blocked[{}]: {problem}", problem.code());
}
for warning in &target.warnings {
println!(" warning[{}]: {}", warning.code(), warning.message());
}
for entry in &target.entries {
let mut line = format!(" {:<15} {}", entry.state.as_str(), entry.name);
if entry.action != apply::Action::None {
line.push_str(&format!(" ({})", entry.action.as_str()));
}
if let Some(problem) = &entry.problem {
line.push_str(&format!(" {}[{}]", problem.message(), problem.code()));
}
if entry.unresolved {
line.push_str(" needs a decision");
}
println!("{line}");
}
}
for warning in &plan.warnings {
eprintln!("warning[{}]: {warning}", warning.code());
}
}