toha 0.4.2

Generate projects and files from templates through one interview for people, scripts, agents, and Rust callers.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
// ---
// relationships:
//   implements: architecture
// ---
//! The template-update route: `apply --update` and `apply --baseline`.
//!
//! An update re-renders a template and merges the difference into the target,
//! three-way against a base. `--update` reads the base from a stored application and
//! replays its recorded answers through the unchanged interview engine; the
//! `--answers` envelope supplies overrides and `--reanswer` re-asks every answer.
//! `--baseline` merges the whole render against an empty base, so unrelated local
//! edits survive. An update refuses a dirty target, an application taken for another
//! target, and a template whose source is not the application's. At a
//! terminal a one-shot update prompts the questions the recorded answers do not
//! settle; the agent stages it with `stage --update` (see `update_stage`).

use std::path::{Path, PathBuf};

use indexmap::IndexMap;
use serde_json::{Value, json};
use std::io::IsTerminal;
use toha::hook::ProcessRunner;

use toha::applied::{Action, Change, ConflictKind, Merged, RepoPath, UpdateDrive};
use toha::protocol::Context;

use crate::{Dirs, Outcome, read_answers_text, requirement_error, setup};

pub use toha::updates::UpdateBase;

/// `apply --update PATH [--answers FILE] [--reanswer]` and `apply --baseline
/// TEMPLATE PATH --answers FILE`.
#[allow(clippy::too_many_arguments)]
pub fn run_update(
    base: UpdateBase,
    template_arg: Option<String>,
    path: &Path,
    answers: Option<String>,
    reanswer: bool,
    force: bool,
    dry_run: bool,
    trust: bool,
    input: &[PathBuf],
    parts: toha::parts::Selection,
    dirs: &Dirs,
) -> Outcome {
    let drift_guidance = crate::guidance::update_drift(
        &crate::guidance::Invocation::Apply {
            template: template_arg.as_deref().map(crate::guidance::Arg::Given),
            path,
            answers: answers.as_deref().map(crate::guidance::Arg::Given),
            force,
            dry_run,
            trust,
        },
        matches!(base, UpdateBase::Baseline),
        reanswer,
        input,
        &parts,
    );
    let (target, _store) = match setup(path, dirs) {
        Ok(value) => value,
        Err(error) => return Outcome::Error(error),
    };
    let prep = match prepare(
        &base,
        template_arg,
        &target,
        dirs,
        trust,
        PrepareMode::Apply,
        false,
        answers.is_some(),
    ) {
        Ok(value) => value,
        Err(outcome) => return outcome,
    };

    // The override answers from `--answers`, identity-checked against the
    // resolved template.
    let overrides = match &answers {
        Some(file) => {
            let text = match read_answers_text(file) {
                Ok(value) => value,
                Err(error) => return Outcome::Error(error),
            };
            match toha::protocol::document_answers(&prep.resolved.formal_name, &text) {
                Ok(value) => value,
                Err(error) => return Outcome::Error(error.to_string()),
            }
        }
        None => IndexMap::new(),
    };

    // One-shot part flags are usage errors before lazy document-file admission.
    if let Err(error) = parts.admit(&prep.template, &overrides) {
        return Outcome::Usage(error);
    }

    let document_context =
        Context::for_template(&target, &prep.resolved.formal_name, &prep.resolved.commit);
    let mut session = match toha::updates::UpdateSession::start(
        prep,
        target.clone(),
        base,
        reanswer,
        parts,
        input,
        &overrides,
    ) {
        Ok(v) => v,
        Err(toha::updates::SessionError::Operation(e)) => {
            return crate::operation_error(e, answers.as_ref().map(|_| &document_context));
        }
        Err(e) => return Outcome::Error(e.to_string()),
    };
    if answers.is_none() && std::io::stdin().is_terminal() {
        loop {
            let submission = match session.replay() {
                Ok(UpdateDrive::Ask { pending, .. }) => {
                    match crate::terminal::prompt_batch(&pending, &mut crate::terminal::InquireAsk)
                    {
                        Ok(v) => v,
                        Err(e) => return Outcome::Error(e),
                    }
                }
                Ok(_) => break,
                Err(e) => return Outcome::Error(e.to_string()),
            };
            if let Err(e) = session.answer(submission) {
                return Outcome::Error(e.to_string());
            }
        }
    }
    match session.state() {
        Ok(toha::operations::SessionState::Completed(done)) => finish_merge(session, done, toha::updates::UpdateOptions { force, dry_run }, answers.is_some(), None, &drift_guidance),
        Ok(toha::operations::SessionState::Asking { .. }) if answers.is_none() => Outcome::Error("the update has questions the recorded answers do not settle; stage it with `stage --update` to answer them".into()),
        Ok(toha::operations::SessionState::Asking { .. }) => match session.replay() {
            Ok(UpdateDrive::Ask { pending, rejections, .. }) => Outcome::Document(toha::protocol::batch_document(pending.batch(), &crate::context(&target, &session.record), Some(&rejections)), 4),
            Err(e) => Outcome::Error(e.to_string()), _ => unreachable!("asking update"),
        },
        Ok(toha::operations::SessionState::Ended(ended)) => { eprintln!("{}", crate::guidance::flow_ended(&ended)); Outcome::Error(format!("the template ended the interview: {:?}", ended.kind())) },
        Err(e) => Outcome::Error(e.to_string()), _ => unreachable!("known session state"),
    }
}

pub use toha::updates::{PrepareMode, Prepared};
fn update_error(error: toha::updates::UpdateError, scripted: bool) -> Outcome {
    match error {
        toha::updates::UpdateError::WithWarnings { warnings, source } => {
            crate::report_config_warnings(&warnings);
            update_error(*source, scripted)
        }
        toha::updates::UpdateError::StaleApplication => Outcome::Error(
            "the applied record entry changed; run stage TEMPLATE PATH --update".into(),
        ),
        toha::updates::UpdateError::InvalidApplication => {
            Outcome::Error("invalid staged application; run stage TEMPLATE PATH --update".into())
        }
        toha::updates::UpdateError::Base {
            template,
            revision,
            reason,
        } => Outcome::Error(format!(
            "cannot render merge base for {template} at {revision:?}: {reason}; use apply TEMPLATE PATH --baseline to re-baseline"
        )),
        toha::updates::UpdateError::Error(e) => Outcome::Error(e),
        toha::updates::UpdateError::Resolve(e) => crate::resolve_error(e),
        toha::updates::UpdateError::Requirements(e) => {
            if scripted {
                requirement_error(&e)
            } else {
                Outcome::Error(e.to_string())
            }
        }
        other => Outcome::Error(other.to_string()),
    }
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn prepare(
    base: &UpdateBase,
    template_arg: Option<String>,
    target: &toha::staging::CanonicalTarget,
    dirs: &Dirs,
    trust: bool,
    mode: PrepareMode,
    interactive: bool,
    scripted: bool,
) -> Result<Prepared, Outcome> {
    toha::updates::open_project(target).map_err(|e| update_error(e, false))?;
    let cwd = std::env::current_dir().map_err(|e| Outcome::Error(e.to_string()))?;
    let (config, registry) = cli_context(dirs, &cwd)?;
    let template_arg = template_arg.ok_or_else(|| {
        Outcome::Error(
            "apply --update and --baseline need a template operand: apply TEMPLATE PATH --update"
                .into(),
        )
    })?;
    let preparation = toha::updates::Preparation::new(
        base,
        &template_arg,
        target,
        dirs,
        &config,
        &registry,
        &cwd,
    )
    .map_err(|e| update_error(e, scripted))?;
    let now = crate::runtime_now().map_err(Outcome::Error)?;
    let mut source = crate::HostEnvironmentSource;
    let mut environment = toha::updates::Environment {
        config: &config,
        registry: &registry,
        cwd: &cwd,
        now,
        host: toha::context::HostFacts::capture(),
        is_admin: crate::is_admin(),
        source: &mut source,
    };
    let result = preparation
        .finish(trust, mode, interactive, &mut environment)
        .map_err(|e| update_error(e, scripted))?;
    crate::report_config_warnings(&result.warnings);
    Ok(result)
}
pub(crate) fn resume_prepared(
    saved: &crate::StagedRecord,
    base: &UpdateBase,
    target: &toha::staging::CanonicalTarget,
    dirs: &Dirs,
    trust: bool,
) -> Result<Prepared, Outcome> {
    let admission =
        toha::updates::admit_resume(saved, base, target).map_err(|e| update_error(e, false))?;
    let cwd = std::env::current_dir().map_err(|e| Outcome::Error(e.to_string()))?;
    let (config, registry) = cli_context(dirs, &cwd)?;
    let now = crate::runtime_now().map_err(Outcome::Error)?;
    let mut source = crate::HostEnvironmentSource;
    let mut environment = toha::updates::Environment {
        config: &config,
        registry: &registry,
        cwd: &cwd,
        now,
        host: toha::context::HostFacts::capture(),
        is_admin: crate::is_admin(),
        source: &mut source,
    };
    let result =
        toha::updates::resume_admitted(admission, saved, target, dirs, trust, &mut environment)
            .map_err(|e| update_error(e, false))?;
    crate::report_config_warnings(&result.warnings);
    Ok(result)
}

/// Build the application inputs from a completed update interview and merge them
/// into the project, mapping the merge result to an `Outcome`.
pub(crate) fn finish_merge(
    session: toha::updates::UpdateSession,
    completed: toha::Completed,
    options: toha::updates::UpdateOptions,
    scripted: bool,
    store: Option<&toha::staging::Store>,
    drift_guidance: &str,
) -> Outcome {
    let prep = &session.prepared;
    let target = &session.target;
    let dry_run = toha::updates::effective_dry_run(options.dry_run, &completed);
    // The result documents carry the invocation context; build it from `prep`
    // before the merge consumes the base.
    let saved = crate::StagedRecord::new_with_context(
        prep.context.clone(),
        prep.resolved.commit.clone(),
        prep.resolved.named,
        prep.now.to_string(),
        vec![],
    );
    let ctx = crate::context(target, &saved);
    let base_id = prep
        .base_application
        .as_ref()
        .map(|application| application.target().to_string());

    // Record the revision actually rendered, including a newly selected content
    // revision when the caller names the current template explicitly.
    let revision = match crate::cli::resolve::applied_revision(
        &prep.resolved.formal_name,
        &prep.resolved.commit,
    ) {
        Ok(revision) => revision,
        Err(error) => return Outcome::Error(error.to_string()),
    };
    let metadata = crate::parts_output::metadata(
        prep.base_application.as_ref().map(|a| a.revision()),
        &revision,
        prep.base_application
            .as_ref()
            .map(|a| a.parts.as_slice())
            .unwrap_or(&[]),
        &prep.template.selected_parts(&completed),
    );
    let runner: &dyn toha::hook::HookRunner = if scripted {
        &crate::ScriptedRunner
    } else {
        &ProcessRunner
    };
    match session.apply_with_options(&completed, options, runner, store) {
        Ok((merged, report)) => {
            let outcome = match merged_outcome(merged, &ctx, base_id.as_deref()) {
                Outcome::Document(document, code) => {
                    Outcome::Document(crate::with_application(document, &metadata), code)
                }
                other => other,
            };
            if scripted {
                outcome
            } else {
                crate::update_output::text_outcome(outcome, &report, dry_run)
            }
        }
        Err(toha::updates::SessionError::Merge(toha::applied::MergeError::Apply(error)))
            if scripted =>
        {
            crate::scripted_apply_error(*error, &ctx)
        }
        Err(toha::updates::SessionError::Merge(toha::applied::MergeError::Apply(error)))
            if matches!(*error, toha::ApplyError::Drift(_)) =>
        {
            Outcome::Error(format!("{error}\n{drift_guidance}"))
        }
        Err(toha::updates::SessionError::Merge(toha::applied::MergeError::Apply(error)))
            if matches!(*error, toha::ApplyError::Conflicts(_)) =>
        {
            Outcome::Error(format!(
                "{error}\n{}",
                crate::guidance::update_outside_conflict()
            ))
        }
        Err(error) => Outcome::Error(error.to_string()),
    }
}

/// Load the command context, mapping a resolve error to an `Outcome`.
fn cli_context(
    dirs: &Dirs,
    cwd: &Path,
) -> Result<(toha::config::Config, toha::registry::Registry), Outcome> {
    crate::cli::resolve::load_context(dirs, cwd).map_err(crate::resolve_error)
}

/// Map a merge result to the caller's result document. An update reports the
/// canonical `applied`/`planned`/`already-current` document with its `merge`
/// member (the changed paths and any conflicts) and, when it saved one, a
/// `record` member.
fn merged_outcome(merged: Merged, ctx: &Context, _base_id: Option<&str>) -> Outcome {
    match merged {
        Merged::AlreadyCurrent => {
            let document = toha::protocol::shell("already-current", ctx);
            Outcome::Document(document, 0)
        }
        // An untrusted update previews the plan and writes nothing, like a dry run.
        Merged::NeedsTrust => {
            let mut document = toha::protocol::shell("planned", ctx);
            document["files"] = json!([]);
            document["messages"] = json!([]);
            document["hooks"] = json!([]);
            document["trusted"] = json!(false);
            document["merge"] = json!({ "changes": [], "conflicted": [] });
            Outcome::Document(document, 0)
        }
        Merged::Planned { changes } => {
            let mut document = toha::protocol::shell("planned", ctx);
            document["files"] = json!([]);
            document["messages"] = json!([]);
            document["hooks"] = json!([]);
            document["trusted"] = json!(true);
            document["merge"] = merge_member(&changes, &[]);
            Outcome::Document(document, 0)
        }
        Merged::Written {
            record,
            changes,
            conflicted,
        } => {
            let mut document = toha::protocol::shell("applied", ctx);
            document["files"] = json!([]);
            document["messages"] = json!([]);
            document["hooks"] = json!([]);
            document["record"] = json!({ "path": record });
            document["merge"] = merge_member(&changes, &conflicted);
            Outcome::Document(document, 0)
        }
    }
}

/// The `merge` member: the changed paths with their actions, and the conflicted
/// paths the operator must resolve.
fn merge_member(changes: &[Change], conflicted: &[RepoPath]) -> Value {
    json!({
        "changes": changes.iter().map(change_json).collect::<Vec<_>>(),
        "conflicted": conflicted.iter().map(|path| path.to_string()).collect::<Vec<_>>(),
    })
}

/// One merge change as `{ path, action, conflict? }`.
fn change_json(change: &Change) -> Value {
    let (action, conflict) = match change.action {
        Action::Added => ("added", None),
        Action::Updated => ("updated", None),
        Action::Merged => ("merged", None),
        Action::Deleted => ("deleted", None),
        Action::NotPreviewed => ("not-previewed", None),
        Action::Conflicted(kind) => ("conflicted", Some(conflict_name(kind))),
    };
    let mut object = json!({ "path": change.path.to_string(), "action": action });
    if let Some(conflict) = conflict {
        object["conflict"] = json!(conflict);
    }
    object
}

/// The stable name of a conflict kind.
fn conflict_name(kind: ConflictKind) -> &'static str {
    match kind {
        ConflictKind::Content => "content",
        ConflictKind::AddAdd => "add-add",
        ConflictKind::ModifyDelete => "modify-delete",
        ConflictKind::FileDirectory => "file-directory",
        ConflictKind::Binary => "binary",
        ConflictKind::Driver => "driver",
    }
}