use crate::services::adopt_request;
use clap::{Parser, Subcommand, ValueEnum};
use dort_core::{
Error, bootstrap,
context::HostContext,
git, journal,
local::LocalConfig,
planner::{self, Request, UnmanageMode},
recovery,
state::WriterLock,
};
use serde::Serialize;
use std::{path::PathBuf, process::ExitCode};
#[derive(Parser)]
#[command(
version,
about = "Git-native development environment manager for local files, tools and Git"
)]
struct Cli {
#[arg(long, global = true)]
json: bool,
#[arg(long, global = true)]
repo: Option<PathBuf>,
#[arg(long, global = true)]
dry_run: bool,
#[arg(long, global = true)]
yes: bool,
#[arg(long, global = true)]
confirm_remove: Option<String>,
#[command(subcommand)]
command: Option<Command>,
}
#[derive(Subcommand)]
enum Command {
Tui,
Init { path: PathBuf },
Status {
#[arg(long = "profile")]
profiles: Vec<String>,
},
Plan {
#[arg(long = "profile")]
profiles: Vec<String>,
},
History,
Rollback {
repository_id: String,
transaction_id: String,
},
Doctor,
Files {
#[command(subcommand)]
command: FilesCommand,
},
Recovery {
#[command(subcommand)]
command: RecoveryCommand,
},
Git {
#[command(subcommand)]
command: GitCommand,
},
Tools {
#[command(subcommand)]
command: ToolsCommand,
},
Manifest {
#[command(subcommand)]
command: ManifestCommand,
},
}
#[derive(Subcommand)]
enum GitCommand {
Status,
Diff {
#[arg(long)]
staged: bool,
#[arg(long)]
patch: bool,
},
}
#[derive(Subcommand)]
enum RecoveryCommand {
Rollback {
repository_id: String,
transaction_id: String,
},
Resume {
repository_id: String,
transaction_id: String,
},
Abandon {
repository_id: String,
transaction_id: String,
},
List,
}
#[derive(Subcommand)]
enum ManifestCommand {
Check {
#[arg(long)]
manifest: Option<PathBuf>,
#[arg(long = "profile")]
profiles: Vec<String>,
},
}
#[derive(Subcommand)]
enum FilesCommand {
#[command(alias = "adopt")]
Add {
target: PathBuf,
#[arg(long)]
id: String,
#[arg(long)]
source: PathBuf,
#[arg(long = "profile")]
profiles: Vec<String>,
},
Apply {
#[arg(long = "profile")]
profiles: Vec<String>,
},
#[command(alias = "unmanage")]
Remove {
id: String,
#[arg(long, value_enum)]
mode: RemovalMode,
#[arg(long = "profile")]
profiles: Vec<String>,
},
Discover,
List {
#[arg(long = "profile")]
profiles: Vec<String>,
},
Plan {
#[arg(long = "profile", global = true)]
profiles: Vec<String>,
#[command(subcommand)]
command: PlanCommand,
},
}
#[derive(Subcommand)]
enum PlanCommand {
Apply,
Adopt {
target: PathBuf,
#[arg(long)]
id: String,
#[arg(long)]
source: PathBuf,
},
Unmanage {
id: String,
#[arg(long, value_enum)]
mode: RemovalMode,
},
}
#[derive(clap::Args)]
struct DeclaredToolsArgs {
#[arg(long)]
manifest: Option<PathBuf>,
#[arg(long = "profile")]
profiles: Vec<String>,
#[arg(long = "program", value_name = "TOOL=PATH")]
programs: Vec<String>,
#[arg(long = "npm-cli", value_name = "TOOL=PATH")]
npm_clis: Vec<String>,
#[arg(long = "npm-prefix", value_name = "TOOL=PATH")]
npm_prefixes: Vec<String>,
}
#[derive(clap::Args)]
struct InstalledToolsArgs {
#[arg(long)]
manifest: Option<PathBuf>,
#[arg(long = "program", value_name = "TOOL=PATH")]
programs: Vec<String>,
#[arg(long = "npm-cli", value_name = "TOOL=PATH")]
npm_clis: Vec<String>,
#[arg(long = "npm-prefix", value_name = "TOOL=PATH")]
npm_prefixes: Vec<String>,
}
#[derive(Subcommand)]
enum ToolsCommand {
Inspect(InstalledToolsArgs),
Check(DeclaredToolsArgs),
InstallMissing(DeclaredToolsArgs),
Declare {
#[arg(long)]
declaration: String,
#[arg(long = "profile")]
profiles: Vec<String>,
},
Edit {
#[arg(long)]
declaration: String,
#[arg(long = "profile")]
profiles: Vec<String>,
},
Remove {
id: String,
#[arg(long = "profile")]
profiles: Vec<String>,
},
Attempts {
#[arg(long)]
id: Option<String>,
},
CloseAttempt {
id: String,
#[arg(long, value_enum)]
provider: Option<InstallationProvider>,
#[arg(long)]
npm_cli: Option<PathBuf>,
#[arg(long)]
prefix: Option<PathBuf>,
#[arg(long)]
executable: Option<PathBuf>,
#[arg(long = "search-dir")]
search_dirs: Vec<PathBuf>,
},
}
#[derive(Clone, Copy, ValueEnum)]
enum InstallationProvider {
Npm,
Brew,
}
#[derive(Clone, Copy, ValueEnum)]
enum RemovalMode {
Keep,
Remove,
}
pub fn run() -> ExitCode {
let cli = Cli::parse();
let filter = tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("warn"));
tracing_subscriber::fmt()
.with_env_filter(filter)
.with_writer(std::io::stderr)
.with_target(false)
.init();
match execute(&cli) {
Ok(code) => code,
Err(error) => {
let (code, kind) = match &error {
Error::Io { .. } => (1, "io"),
Error::Locked => (3, "locked"),
Error::Parse(_)
| Error::Validation(_)
| Error::Invalid(_)
| Error::RecoveryRequired(_)
| Error::NoRepository => (2, "validation"),
};
if cli.json {
eprintln!(
"{}",
serde_json::json!({"error": error.to_string(), "kind": kind})
);
} else {
eprintln!("error: {error}");
}
ExitCode::from(code)
}
}
}
fn declared_tools(
cli: &Cli,
host: &HostContext,
args: &DeclaredToolsArgs,
install_missing: bool,
) -> dort_core::Result<ExitCode> {
use crate::services::declared_tools::{Input, Status};
if cli.confirm_remove.is_some() {
return Err(Error::Invalid(
"Tool checks and installation do not use --confirm-remove".into(),
));
}
let launch = crate::environment::launch_directory()?;
let manifest = match &args.manifest {
Some(path) => crate::services::resolve_root(path, &launch),
None => {
let local = LocalConfig::load(host)?;
let explicit = cli
.repo
.as_ref()
.map(|p| crate::services::resolve_root(p, &launch));
crate::services::declared_tools::manifest(
&local.select_repository(explicit.as_deref())?,
)
}
};
let detection = crate::environment::inspection_inputs().with_settings(
host,
&manifest,
&args.programs,
&args.npm_clis,
&args.npm_prefixes,
)?;
let preview = Input {
manifest,
profiles: args.profiles.clone(),
detection,
}
.prepare(host)?;
if cli.dry_run {
if cli.json {
print_git_json(
&serde_json::json!({"check_review":preview.review(), "install_missing":install_missing, "preview_only":true}),
)?;
} else {
print!(
"{}",
crate::services::presentation::check_review(&preview.review())
);
}
return Ok(ExitCode::SUCCESS);
}
if !cli.yes && !interactive(cli) {
return Err(Error::Invalid("Tool checks require terminal confirmation or --yes; use --dry-run for a process-free review.".into()));
}
if !cli.json {
print!(
"{}",
crate::services::presentation::check_review(&preview.review())
);
}
if !cli.yes
&& !confirmed(
"Check declared tools? Package-manager configuration and plugins may run. [y/N] ",
)?
{
return Err(Error::Invalid("Tool checks were not approved.".into()));
}
let checked = preview.approve().check(host)?;
let statuses: Vec<_> = checked.reconciliation.plan.rows.iter().map(|r|
serde_json::json!({"tool":r.id, "status":crate::services::declared_tools::status(r)})
).collect();
if !cli.json {
for row in &checked.reconciliation.plan.rows {
println!(
"{}: {}",
crate::services::presentation::inline(&row.id),
crate::services::declared_tools::status(row).label()
);
}
}
let complete = checked
.reconciliation
.plan
.rows
.iter()
.all(|r| crate::services::declared_tools::status(r) == Status::Installed);
let mut exit = if !checked.available() {
1
} else if complete {
0
} else {
4
};
if install_missing {
let preview = checked.prepare_missing(host)?;
if !cli.json {
print!(
"{}",
crate::services::presentation::install_review(&preview.review())
);
}
if preview.is_empty() {
if cli.json {
print_git_json(
&serde_json::json!({"check":checked, "statuses":statuses, "installation_review":preview.review(), "installation":null}),
)?;
}
} else {
if !cli.yes
&& !confirmed(
"Install the displayed missing tools? Partial effects are possible; no rollback. [y/N] ",
)?
{
return Err(Error::Invalid("Installation was not approved. Approved checks may already have changed package-manager caches.".into()));
}
let deferred = preview.deferred.len();
let approval = preview.approve()?;
let lock = WriterLock::acquire(host)?;
let installed = approval.execute(host, &lock, |_| {});
exit = if !installed.succeeded() {
1
} else if deferred > 0
|| checked.reconciliation.plan.rows.iter().any(|r| {
!matches!(
crate::services::declared_tools::status(r),
Status::Installed | Status::Missing
)
})
{
4
} else {
0
};
if cli.json {
print_git_json(
&serde_json::json!({"before_installation":{"check":checked, "statuses":statuses}, "installation":installed}),
)?;
} else {
print!(
"{}",
crate::services::presentation::installed_result(&installed)
);
}
}
} else if cli.json {
print_git_json(&serde_json::json!({"check":checked, "statuses":statuses}))?;
}
Ok(ExitCode::from(exit))
}
fn installed_tools(
cli: &Cli,
host: &HostContext,
args: &InstalledToolsArgs,
) -> dort_core::Result<ExitCode> {
use crate::services::{installed_tools, presentation};
if cli.confirm_remove.is_some() {
return Err(Error::Invalid(
"Inspection does not use --confirm-remove.".into(),
));
}
let launch = crate::environment::launch_directory()?;
let explicit = cli
.repo
.as_ref()
.map(|p| crate::services::resolve_root(p, &launch));
let manifest = args
.manifest
.as_ref()
.map(|p| crate::services::resolve_root(p, &launch))
.or_else(|| {
LocalConfig::load(host)
.ok()?
.select_repository(explicit.as_deref())
.ok()
.map(|r| r.join("dort.toml"))
});
let mut input = crate::environment::inspection_inputs();
if !args.programs.is_empty() || !args.npm_clis.is_empty() || !args.npm_prefixes.is_empty() {
let manifest = manifest.as_ref().ok_or_else(|| {
Error::Invalid("Select a repository before supplying tool settings.".into())
})?;
input = input.with_settings(
host,
manifest,
&args.programs,
&args.npm_clis,
&args.npm_prefixes,
)?;
}
let preview = installed_tools::prepare(host, manifest, input)?;
if cli.dry_run {
if cli.json {
print_git_json(
&serde_json::json!({"inspection_review":preview.review(), "preview_only":true}),
)?;
} else {
print!("{}", presentation::installed_review(&preview.review()));
}
return Ok(ExitCode::SUCCESS);
}
if !preview.is_empty() {
if !cli.yes && !interactive(cli) {
return Err(Error::Invalid("Inspection requires terminal confirmation or --yes; use --dry-run for a process-free review.".into()));
}
if !cli.json {
print!("{}", presentation::installed_review(&preview.review()));
}
if !cli.yes
&& !confirmed(
"Inspect installed tools? Package-manager configuration/plugins may run. [y/N] ",
)?
{
return Err(Error::Invalid("Inspection was not approved.".into()));
}
}
let current = preview.approve().inspect()?;
if cli.json {
print_git_json(¤t)?;
} else {
print!("{}", presentation::installed(¤t));
}
Ok(ExitCode::from(if current.available() { 0 } else { 1 }))
}
fn tools_command(
cli: &Cli,
host: &HostContext,
command: &ToolsCommand,
) -> dort_core::Result<ExitCode> {
match command {
ToolsCommand::Inspect(args) => return installed_tools(cli, host, args),
ToolsCommand::Check(args) => return declared_tools(cli, host, args, false),
ToolsCommand::InstallMissing(args) => return declared_tools(cli, host, args, true),
_ => (),
}
if matches!(
command,
ToolsCommand::Declare { .. } | ToolsCommand::Edit { .. } | ToolsCommand::Remove { .. }
) {
if cli.confirm_remove.is_some() {
return Err(Error::Invalid(
"declaration changes use ordinary approval; omit --confirm-remove".into(),
));
}
let local = LocalConfig::load(host)?;
let repository = local.select_repository(cli.repo.as_deref())?;
let preview = match command {
ToolsCommand::Declare {
declaration,
profiles,
}
| ToolsCommand::Edit {
declaration,
profiles,
} => {
let mut request = crate::services::declaration::Request::parse(declaration)?;
if matches!(command, ToolsCommand::Edit { .. }) {
request.change = Some(dort_core::tools::declaration::Change::Edit);
}
request.prepare(host, &repository, profiles)?
}
ToolsCommand::Remove { id, profiles } => {
crate::services::declaration::prepare_remove(host, &repository, profiles, id)?
}
_ => unreachable!(),
};
if cli.dry_run {
if cli.json {
print_git_json(&preview)?;
} else {
print!(
"{}",
crate::services::presentation::safe(&crate::services::declaration::report(
&preview
))
);
}
} else {
if !cli.json {
print!(
"{}",
crate::services::presentation::safe(&crate::services::declaration::report(
&preview
))
);
}
approve(cli)?;
let approval = dort_core::tools::declaration::Approval::from_preview(preview)?;
let lock = WriterLock::acquire(host)?;
let tx = dort_core::tools::declaration::execute(host, &lock, approval, |_| Ok(()))?;
if cli.json {
mutation_report(
match command {
ToolsCommand::Edit { .. } => "edit-tool",
ToolsCommand::Remove { .. } => "remove-tool",
_ => "declare-tool",
},
tx,
)?;
} else {
println!(
"{}",
if tx.is_some() {
"Declaration saved."
} else {
"No changes were needed."
}
);
}
}
return Ok(ExitCode::SUCCESS);
}
if cli.repo.is_some() || cli.confirm_remove.is_some() {
return Err(Error::Invalid(
"Tools uses its explicit scope; omit --repo/--confirm-remove".into(),
));
}
if let ToolsCommand::Attempts { id } = command {
let attempts = dort_core::tools::install::diagnose(host)?;
if let Some(id) = id {
let attempt = attempts
.iter()
.find(|a| &a.id == id)
.ok_or_else(|| Error::Invalid("installation record not found".into()))?;
if cli.json {
print_git_json(attempt)?;
} else {
print!("{}", crate::services::presentation::attempt(attempt));
}
} else {
if cli.json {
print_git_json(&attempts)?;
} else if attempts.is_empty() {
println!("No installation history.");
} else {
for attempt in &attempts {
print!("{}", crate::services::presentation::attempt(attempt));
}
}
}
return Ok(ExitCode::SUCCESS);
}
if let ToolsCommand::CloseAttempt {
id,
executable,
search_dirs,
provider,
npm_cli,
prefix,
} = command
{
let npm_binding = if matches!(provider, Some(InstallationProvider::Npm)) {
Some(dort_core::tools::inspection::NpmBinding {
runtime_tool: "historical-runtime".into(),
node: executable.clone().ok_or_else(|| {
Error::Invalid("npm fallback requires --executable Node".into())
})?,
npm_cli: npm_cli
.clone()
.ok_or_else(|| Error::Invalid("npm fallback requires --npm-cli".into()))?,
prefix: prefix
.clone()
.ok_or_else(|| Error::Invalid("npm fallback requires --prefix".into()))?,
})
} else {
if npm_cli.is_some()
|| (prefix.is_some() && !matches!(provider, Some(InstallationProvider::Brew)))
{
return Err(Error::Invalid(
"--npm-cli needs --provider npm; --prefix needs npm or brew".into(),
));
}
None
};
let preview = dort_core::tools::install::prepare_close(
host,
dort_core::tools::install::CloseRequest {
id: id.clone(),
executable: executable.clone(),
search_dirs: search_dirs.clone(),
npm_binding,
brew_prefix: if matches!(provider, Some(InstallationProvider::Brew)) {
Some(prefix.clone().ok_or_else(|| {
Error::Invalid("Homebrew fallback requires --prefix".into())
})?)
} else {
None
},
},
)?;
if cli.dry_run && cli.json {
print_git_json(&preview.review())?;
} else if !cli.json {
print!(
"{}",
crate::services::presentation::close_review(&preview.review())
);
}
if !cli.dry_run {
approve(cli)?;
let lock = WriterLock::acquire(host)?;
let attempt = dort_core::tools::install::close(host, &lock, preview.approve())?;
if cli.json {
print_git_json(&attempt)?;
} else {
print!("{}", crate::services::presentation::attempt(&attempt));
}
}
return Ok(ExitCode::SUCCESS);
}
unreachable!("all Tools commands are handled above")
}
fn execute(cli: &Cli) -> dort_core::Result<ExitCode> {
let command = cli.command.as_ref().unwrap_or(&Command::Tui);
if matches!(command, Command::Tui) {
if cli.json || cli.yes || cli.confirm_remove.is_some() || !interactive(cli) {
return Err(Error::Invalid("TUI requires terminal stdin/stdout/stderr, without --json/--yes/--confirm-remove; use --help or an explicit CLI command".into()));
}
crate::tui::run(
HostContext::from_env()?,
cli.repo.clone(),
cli.dry_run,
crate::environment::launch_directory()?,
crate::environment::resolve_git().map_err(|error| error.to_string()),
crate::environment::terminal_appearance(),
crate::environment::reduced_motion(),
)?;
return Ok(ExitCode::SUCCESS);
}
let host = HostContext::from_env()?;
if let Command::Tools { command } = command {
return tools_command(cli, &host, command);
}
let local = LocalConfig::load(&host)?;
if cli.repo.is_some()
&& matches!(
command,
Command::Rollback { .. }
| Command::Recovery {
command: RecoveryCommand::Rollback { .. }
| RecoveryCommand::Resume { .. }
| RecoveryCommand::Abandon { .. }
}
)
{
return Err(Error::Invalid(
"recovery uses explicit repository and operation IDs; omit --repo".into(),
));
}
match command {
Command::Tui | Command::Tools { .. } => unreachable!(),
Command::Init { path } => {
if cli.repo.is_some() {
return Err(Error::Invalid(
"init uses its explicit path; omit --repo".into(),
));
}
let root = if path.is_absolute() {
path.clone()
} else {
crate::services::resolve_root(path, &crate::environment::launch_directory()?)
};
let git = crate::environment::resolve_git()?;
let preview = crate::services::BootstrapPreview::prepare(&host, &root, &git)?;
let report = &preview.report;
if cli.dry_run {
print_git_json(&report)?;
} else {
if !cli.json {
print_git_json(&report)?;
}
approve(cli)?;
let tid = preview.approve()?.execute(&host, |_| Ok(()))?;
print_git_json(
&serde_json::json!({"schema":1,"operation":"init","completed":true,"repository":root,"transaction_id":tid}),
)?;
}
}
Command::Status { profiles } | Command::Plan { profiles } => {
return file_operation(cli, &host, &local, profiles, Request::Apply, false);
}
Command::Rollback {
repository_id,
transaction_id,
}
| Command::Recovery {
command:
RecoveryCommand::Rollback {
repository_id,
transaction_id,
},
} => {
let report = recovery::preview(&host, repository_id, transaction_id)?;
if cli.dry_run {
print_git_json(&report)?;
} else {
if !cli.json {
print_git_json(&report)?;
}
approve(cli)?;
let approval = recovery::Approval::from_preview(report)?;
let lock = WriterLock::acquire(&host)?;
recovery::execute(&host, &lock, approval)?;
mutation_report("rollback", Some(transaction_id.clone()))?;
}
}
Command::Recovery {
command:
RecoveryCommand::Resume {
repository_id,
transaction_id,
},
}
| Command::Recovery {
command:
RecoveryCommand::Abandon {
repository_id,
transaction_id,
},
} => {
let action = if matches!(
command,
Command::Recovery {
command: RecoveryCommand::Resume { .. }
}
) {
bootstrap::RecoveryAction::Resume
} else {
bootstrap::RecoveryAction::Abandon
};
let report = bootstrap::recovery_preview(&host, repository_id, transaction_id, action)?;
if cli.dry_run {
print_git_json(&report)?;
} else {
if !cli.json {
print_git_json(&report)?;
}
approve(cli)?;
let approval = bootstrap::RecoveryApproval::from_preview(report)?;
let lock = WriterLock::acquire(&host)?;
bootstrap::recover(&host, &lock, approval, |_| Ok(()))?;
mutation_report("bootstrap_recovery", Some(transaction_id.clone()))?;
}
}
Command::Doctor => {
let report = crate::services::doctor(&host, &local);
if cli.json {
print_json(&report)?;
} else {
println!("Dort: {}", report.mode);
println!(
"Platform: {:?} / {:?}",
report.host.platform, report.host.architecture
);
println!("Config: {}", report.host.config_dir.display());
println!("State: {}", report.host.state_dir.display());
println!(
"Registered repositories: {}",
report.registered_repositories
);
}
}
Command::History
| Command::Recovery {
command: RecoveryCommand::List,
} => {
if cli.repo.is_some() {
return Err(Error::Invalid(
"recovery list inspects all local repositories; omit --repo".into(),
));
}
let transactions = journal::diagnose(&host)?;
let write_blocked = transactions.iter().any(journal::Diagnosis::blocks_writes);
if cli.json {
print_json(
&serde_json::json!({"schema": 1, "read_only": true, "write_blocked": write_blocked, "transactions": transactions}),
)?;
} else {
for transaction in &transactions {
println!(
"{} / {}: {:?} ({}; {} saved steps)",
transaction.repository_id,
transaction.transaction_id,
transaction.phase,
operation_type(transaction.journal_schema),
transaction.valid_checkpoints
);
if let Some(problem) = &transaction.problem {
println!(" BLOCKED: {problem}");
}
}
println!(
"Read-only history. Writes blocked: {write_blocked}. Recovery needs a preview and approval."
);
}
if write_blocked {
return Ok(ExitCode::from(4));
}
}
Command::Git { command } => {
let root =
local
.select_repository(cli.repo.as_deref())
.map_err(|error| match error {
Error::NoRepository => Error::NoRepository,
_ => Error::Invalid("could not resolve Git repository root".into()),
})?;
let backend = git::Git::new(crate::environment::resolve_git()?)?;
match command {
GitCommand::Status => {
let report = backend.status(&root)?;
if cli.json {
print_git_json(&report)?;
} else {
for header in &report.branch {
println!("Branch: {header:?}");
}
for entry in &report.entries {
println!(
"{} {} {:?} (original: {:?})",
entry.kind,
entry.xy.as_deref().unwrap_or("?"),
entry.path,
entry.original_path
);
}
println!("Read-only Git status; {} entries.", report.entries.len());
}
}
GitCommand::Diff { staged, patch } => {
let report = backend.diff(&root, *staged, *patch)?;
if cli.json {
print_git_json(&report)?;
} else {
for entry in &report.entries {
println!(
"{} {} -> {} {:?}",
entry.change, entry.old_mode, entry.new_mode, entry.path
);
}
println!(
"Read-only Git diff; {} entries. Untracked contents excluded.",
report.entries.len()
);
if let Some(patch) = &report.patch {
println!("Explicit patch CONTENT (unsafe bytes escaped):\n{patch}");
}
}
}
}
}
Command::Files { command } => match command {
FilesCommand::Add {
target,
id,
source,
profiles,
} => {
return file_operation(
cli,
&host,
&local,
profiles,
adopt_request(target, id, source),
true,
);
}
FilesCommand::Apply { profiles } => {
return file_operation(cli, &host, &local, profiles, Request::Apply, true);
}
FilesCommand::Remove { id, mode, profiles } => {
return file_operation(
cli,
&host,
&local,
profiles,
Request::Unmanage {
id: id.clone(),
mode: match mode {
RemovalMode::Keep => UnmanageMode::Keep,
RemovalMode::Remove => UnmanageMode::Remove,
},
},
true,
);
}
FilesCommand::Discover => {
let report = crate::services::discover(&host, &local, cli.repo.as_deref())?.report;
if cli.json {
print_json(&report)?;
} else {
for candidate in &report.candidates {
println!(
"{}: {:?}, adoption candidate: {}{}",
candidate.entry.path.display(),
candidate.entry.kind,
candidate.supported_for_adoption,
candidate
.link_state
.as_ref()
.map_or(String::new(), |state| format!(", link: {state:?}"))
);
}
for warning in &report.warnings {
println!("Warning: {warning}");
}
println!("{}", report.sensitivity_notice);
}
}
FilesCommand::List { profiles } => {
let snapshot = crate::services::observe(
&host,
&local,
cli.repo.as_deref(),
profiles,
Request::Apply,
)?;
if cli.json {
print_json(&snapshot)?;
} else {
for file in &snapshot.files {
println!(
"{}: {} -> {}",
file.mapping.id,
file.target_path.display(),
file.source_path.display()
);
println!(
" Source: {:?}; destination: {:?}",
file.source.as_ref().map(|f| &f.kind),
file.destination.as_ref().map(|f| &f.kind)
);
for blocker in &file.blockers {
println!(" BLOCKED: {blocker}");
}
}
println!(
"Read-only inventory; use files plan apply for safety/conflict decisions."
);
}
}
FilesCommand::Plan { profiles, command } => {
let request = match command {
PlanCommand::Apply => Request::Apply,
PlanCommand::Adopt { target, id, source } => adopt_request(target, id, source),
PlanCommand::Unmanage { id, mode } => Request::Unmanage {
id: id.clone(),
mode: match mode {
RemovalMode::Keep => UnmanageMode::Keep,
RemovalMode::Remove => UnmanageMode::Remove,
},
},
};
let plan = planner::plan(crate::services::observe(
&host,
&local,
cli.repo.as_deref(),
profiles,
request,
)?);
if cli.json {
print_json(&plan)?;
} else {
print!("{}", plan.human());
}
if plan.has_blockers() {
return Ok(ExitCode::from(4));
}
}
},
Command::Manifest {
command: ManifestCommand::Check { manifest, profiles },
} => {
if manifest.is_some() && cli.repo.is_some() {
return Err(Error::Invalid(
"choose either --manifest or --repo, not both".into(),
));
}
let path = if let Some(path) = manifest {
path.clone()
} else {
local
.select_repository(cli.repo.as_deref())?
.join("dort.toml")
};
let report = crate::services::check_manifest(&host, &local, path, profiles)?;
if cli.json {
print_json(&report)?;
} else {
println!("Valid manifest: {}", report.manifest.display());
println!(
"Profiles: {}",
report
.active_profiles
.iter()
.cloned()
.collect::<Vec<_>>()
.join(", ")
);
println!(
"Selected: {} tools, {} files",
report.selected_tools, report.selected_files
);
for capability in &report.unsupported_capabilities {
println!("Not implemented: {capability}");
}
println!(
"Read-only structural check; live files and installed tools were not inspected."
);
}
}
}
Ok(ExitCode::SUCCESS)
}
fn operation_type(schema: Option<u32>) -> &'static str {
match schema {
Some(1) => "file adoption",
Some(2) => "file change",
Some(3) => "repository setup",
Some(4) => "file batch",
Some(5 | 6) => "tool declaration",
_ => "unknown",
}
}
fn mutation_report(operation: &str, transaction: Option<String>) -> dort_core::Result<()> {
print_git_json(
&serde_json::json!({"schema":1,"operation":operation,"completed":true,"transaction_id":transaction}),
)
}
fn interactive(cli: &Cli) -> bool {
use std::io::IsTerminal;
!cli.json
&& std::io::stdin().is_terminal()
&& std::io::stdout().is_terminal()
&& std::io::stderr().is_terminal()
}
fn answer(prompt: &str) -> dort_core::Result<String> {
use std::io::{BufRead, Read, Write};
eprint!("{prompt}");
std::io::stderr()
.flush()
.map_err(|_| Error::Invalid("could not present confirmation".into()))?;
let mut bytes = Vec::new();
std::io::stdin()
.lock()
.take(257)
.read_until(b'\n', &mut bytes)
.map_err(|_| Error::Invalid("could not read confirmation".into()))?;
if bytes.len() > 256 {
return Err(Error::Invalid("confirmation too long".into()));
}
String::from_utf8(bytes)
.map(|s| s.trim_end_matches(['\r', '\n']).to_owned())
.map_err(|_| Error::Invalid("invalid confirmation".into()))
}
fn confirmed(prompt: &str) -> dort_core::Result<bool> {
Ok(matches!(
answer(prompt)?.to_ascii_lowercase().as_str(),
"y" | "yes"
))
}
fn approve(cli: &Cli) -> dort_core::Result<()> {
if cli.yes {
return Ok(());
}
if !interactive(cli) {
return Err(Error::Invalid(
"write requires --yes in JSON/nonterminal mode; use --dry-run to preview".into(),
));
}
if !confirmed("Proceed with the reviewed changes? [y/N] ")? {
return Err(Error::Invalid("write not approved".into()));
}
Ok(())
}
fn file_operation(
cli: &Cli,
host: &HostContext,
local: &LocalConfig,
profiles: &[String],
request: Request,
write: bool,
) -> dort_core::Result<ExitCode> {
let removal = match &request {
Request::Unmanage {
id,
mode: UnmanageMode::Remove,
} => Some(id.clone()),
_ => None,
};
let plan = planner::plan(crate::services::observe(
host,
local,
cli.repo.as_deref(),
profiles,
request,
)?);
if !write || cli.dry_run || plan.has_blockers() {
if cli.json {
print_git_json(&plan)?;
} else {
print!("{}", plan.human());
}
return Ok(ExitCode::from(if plan.has_blockers() { 4 } else { 0 }));
}
if !cli.json {
print!("{}", plan.human());
}
if let Some(id) = &removal {
if cli.confirm_remove.as_ref().is_some_and(|value| value != id) {
return Err(Error::Invalid(
"--confirm-remove does not match the selected mapping ID".into(),
));
}
} else if cli.confirm_remove.is_some() {
return Err(Error::Invalid(
"--confirm-remove is only valid for remove mode".into(),
));
}
approve(cli)?;
let approval = crate::services::FileApproval::from_plan(plan, removal.as_deref())?;
let tid = approval.execute(host, |_| Ok(()))?;
mutation_report("files", tid)?;
Ok(ExitCode::SUCCESS)
}
fn print_git_json(value: &impl Serialize) -> dort_core::Result<()> {
use std::fmt::Write;
let json = serde_json::to_string_pretty(value)
.map_err(|_| Error::Invalid("could not encode Git output".into()))?;
let mut escaped = String::new();
for character in json.chars() {
if character >= '\u{7f}' {
for unit in character.encode_utf16(&mut [0; 2]) {
let _ = write!(escaped, "\\u{unit:04x}");
}
} else {
escaped.push(character);
}
}
println!("{escaped}");
Ok(())
}
fn print_json(value: &impl Serialize) -> dort_core::Result<()> {
let json = serde_json::to_string_pretty(value)
.map_err(|_| Error::Invalid("could not encode output".into()))?;
println!("{json}");
Ok(())
}