use std::path::Path;
use anyhow::Result;
use trusty_code::agents::deploy::{ManifestStatus, roster_manifest_status};
use trusty_code::paths::{self, EntryKind, private_state};
const REPORTED: &[(&str, &str, EntryKind)] = &[
("agents", "agents", EntryKind::Dir),
("skills", "skills", EntryKind::Dir),
("plugins", "plugins", EntryKind::Dir),
("settings", paths::SETTINGS_FILENAME, EntryKind::File),
("context", "CLAUDE.md", EntryKind::File),
];
pub fn show(project_root: &Path, json: bool) -> Result<()> {
let state_dir = private_state::private_state_dir();
let private_ok = private_state::is_restrictive(&state_dir).ok();
let (manifest_path, manifest_status) = roster_manifest_status(project_root);
if json {
let entries: serde_json::Map<String, serde_json::Value> = REPORTED
.iter()
.map(|(label, relative, kind)| {
let r = paths::resolve_project_entry(project_root, relative, *kind);
(
(*label).to_string(),
serde_json::json!({
"source": r.source.as_str(),
"path": r.path.display().to_string(),
"skipped_unreadable": r
.unreadable
.iter()
.map(|p| p.display().to_string())
.collect::<Vec<_>>(),
}),
)
})
.collect();
let doc = serde_json::json!({
"project_root": project_root.display().to_string(),
"write_root": paths::native_config_dir(project_root).display().to_string(),
"private_state_dir": state_dir.display().to_string(),
"private_state_restrictive": private_ok,
"entries": entries,
"roster_manifest": {
"path": manifest_path.display().to_string(),
"status": manifest_status.as_str(),
"managed": match &manifest_status {
ManifestStatus::Present { managed } => Some(*managed),
_ => None,
},
"detail": match &manifest_status {
ManifestStatus::Corrupt { detail } => Some(detail.clone()),
_ => None,
},
},
});
println!("{}", serde_json::to_string_pretty(&doc)?);
return Ok(());
}
println!("project root {}", project_root.display());
println!(
"write root {} (trusty-code writes only here)",
paths::native_config_dir(project_root).display()
);
println!();
for (label, relative, kind) in REPORTED {
let r = paths::resolve_project_entry(project_root, relative, *kind);
println!("{label:<10} {:<12} {}", r.source.as_str(), r.path.display());
for skipped in &r.unreadable {
println!("{:<10} {:<12} {}", "", "unreadable", skipped.display());
}
}
println!();
println!(
"roster manifest {:<12} {}",
manifest_status.as_str(),
manifest_path.display()
);
match &manifest_status {
ManifestStatus::Absent => println!(
"{:<18}no roster deployed here yet — one lands on the next run",
""
),
ManifestStatus::Present { managed } => {
println!("{:<18}{managed} deployed agent file(s) tracked", "")
}
ManifestStatus::Corrupt { detail } => println!(
"{:<18}UNREADABLE — repair or delete it; deploys refuse until then ({detail})",
""
),
}
println!();
println!("private state {}", state_dir.display());
println!(
"permissions {}",
match private_ok {
Some(true) => "owner-only".to_string(),
Some(false) => "PERMISSIVE — run `tcode paths import` or chmod 700".to_string(),
None => "unknown (directory not created yet)".to_string(),
}
);
Ok(())
}
pub const IMPORT_REFUSED_EXIT_CODE: i32 = 1;
pub fn import(project_root: &Path, dry_run: bool) -> Result<i32> {
let plan = paths::import::plan_import(project_root);
if plan.entries.is_empty() {
println!("nothing to import: no .claude/agents, .claude/skills, or .claude/settings.json");
}
let mut refused = 0usize;
for entry in &plan.entries {
match &entry.action {
paths::import::ImportAction::Copy => {
println!("copy {}", entry.to.display());
}
paths::import::ImportAction::Refuse(reason) => {
refused += 1;
println!("skip {} — {reason}", entry.from.display());
}
}
}
if dry_run {
println!("\n--dry-run: nothing was written.");
return Ok(exit_code_for(refused));
}
let report = paths::import::apply_import(project_root, &plan);
match private_state::ensure_private_state_dir() {
Ok(dir) => println!("\nprivate state {} (owner-only)", dir.display()),
Err(e) => eprintln!("\nwarning: could not create the private state directory: {e}"),
}
println!(
"imported {} file(s); skipped {}.",
report.created.len(),
report.refused.len()
);
if !report.created.is_empty() {
println!("to undo, delete exactly these files:");
for path in &report.created {
println!(" {}", path.display());
}
}
Ok(exit_code_for(report.refused.len()))
}
fn exit_code_for(refused: usize) -> i32 {
if refused == 0 {
0
} else {
IMPORT_REFUSED_EXIT_CODE
}
}