forjar 1.4.0

Rust-native Infrastructure as Code — bare-metal first, BLAKE3 state, provenance tracing
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
//! Plan command.

use super::apply_helpers::*;
use super::helpers::*;
use super::helpers_state::*;
use super::print_helpers::*;
use super::workspace::*;
use crate::core::{planner, resolver, types};
use crate::tripwire::hasher;
use std::path::Path;

#[allow(clippy::too_many_arguments)]
pub(crate) fn cmd_plan(
    file: &Path,
    state_dir: &Path,
    machine_filter: Option<&str>,
    resource_filter: Option<&str>,
    tag_filter: Option<&str>,
    json: bool,
    verbose: bool,
    output_dir: Option<&Path>,
    env_file: Option<&Path>,
    workspace: Option<&str>,
    no_diff: bool,
    target: Option<&str>,
    cost: bool,
    what_if: &[String],
    plan_out: Option<&Path>,
    why: bool,
) -> Result<(), String> {
    // GH-91: Warn that --resource filter is not yet implemented
    if resource_filter.is_some() {
        eprintln!("Warning: --resource filter is not yet implemented for plan. Flag ignored.");
    }

    let mut config = parse_and_validate(file)?;

    // FJ-333: Apply hypothetical param overrides
    for kv in what_if {
        if let Some((key, value)) = kv.split_once('=') {
            config.params.insert(
                key.to_string(),
                serde_yaml_ng::Value::String(value.to_string()),
            );
        } else {
            return Err(format!(
                "invalid --what-if format '{kv}': expected KEY=VALUE"
            ));
        }
    }
    if !what_if.is_empty() {
        println!(
            "{}",
            dim(&format!(
                "[what-if] Hypothetical params: {}",
                what_if.join(", ")
            ))
        );
    }
    if let Some(path) = env_file {
        load_env_params(&mut config, path)?;
    }
    inject_workspace_param(&mut config, workspace);
    resolver::resolve_data_sources(&mut config)?;

    // FJ-285: --target filters config to one resource + transitive deps
    if let Some(target_id) = target {
        let keep = collect_transitive_deps(&config, target_id)?;
        config.resources.retain(|k, _| keep.contains(k));
    }

    if verbose {
        eprintln!(
            "Planning {} ({} machines, {} resources)",
            config.name,
            config.machines.len(),
            config.resources.len()
        );
    }
    let execution_order = resolver::build_execution_order(&config)?;

    // Load existing locks so plan shows accurate Create vs Update vs NoOp
    let locks = load_machine_locks(&config, state_dir, machine_filter)?;
    let plan = planner::plan(&config, &execution_order, &locks, tag_filter);

    if let Some(dir) = output_dir {
        export_scripts(&config, dir)?;
    }

    // FJ-1250: Write plan to file for later execution
    if let Some(out_path) = plan_out {
        save_plan_file(&plan, &config, file, out_path)?;
        println!("Plan saved to {}", out_path.display());
        return Ok(());
    }

    if why {
        print_why_explanation(&config, &locks, &execution_order, tag_filter);
    }

    if json {
        print_plan_json(&plan, &config)?;
    } else {
        let show_diff = !no_diff;
        print_plan(
            &plan,
            machine_filter,
            if show_diff { Some(&config) } else { None },
        );
    }

    if cost && !plan.changes.is_empty() {
        print_plan_cost(&plan);
    }

    Ok(())
}

/// FJ-301: Serialize plan as enriched JSON with resource metadata.
fn print_plan_json(
    plan: &types::ExecutionPlan,
    config: &types::ForjarConfig,
) -> Result<(), String> {
    let changes: Vec<serde_json::Value> = plan
        .changes
        .iter()
        .map(|c| {
            let mut entry = serde_json::json!({
                "resource_id": c.resource_id,
                "machine": c.machine,
                "resource_type": c.resource_type,
                "action": c.action,
                "description": c.description,
            });
            if let Some(res) = config.resources.get(&c.resource_id) {
                if let Some(ref rg) = res.resource_group {
                    entry["resource_group"] = serde_json::json!(rg);
                }
                if !res.tags.is_empty() {
                    entry["tags"] = serde_json::json!(res.tags);
                }
                if !res.depends_on.is_empty() {
                    entry["depends_on"] = serde_json::json!(res.depends_on);
                }
            }
            entry
        })
        .collect();
    let change_ids: std::collections::HashSet<&str> = plan
        .changes
        .iter()
        .map(|c| c.resource_id.as_str())
        .collect();
    let filtered_order: Vec<&str> = plan
        .execution_order
        .iter()
        .filter(|id| change_ids.contains(id.as_str()))
        .map(|s| s.as_str())
        .collect();
    let output = serde_json::json!({
        "name": plan.name,
        "to_create": plan.to_create,
        "to_update": plan.to_update,
        "to_destroy": plan.to_destroy,
        "unchanged": plan.unchanged,
        "execution_order": filtered_order,
        "changes": changes,
    });
    println!(
        "{}",
        serde_json::to_string_pretty(&output).map_err(|e| format!("JSON error: {e}"))?
    );
    Ok(())
}

/// FJ-312: Compute and print change cost summary.
fn type_weight(t: &types::ResourceType) -> u32 {
    match t {
        types::ResourceType::Package => 3,
        types::ResourceType::Service => 3,
        types::ResourceType::Mount => 4,
        types::ResourceType::Docker | types::ResourceType::Pepita => 5,
        types::ResourceType::User => 3,
        types::ResourceType::Network => 2,
        types::ResourceType::Gpu => 4,
        types::ResourceType::Model => 5,
        types::ResourceType::Cron => 2,
        _ => 1, // file, recipe
    }
}

pub(crate) fn print_plan_cost(plan: &types::ExecutionPlan) {
    let total_cost: u32 = plan
        .changes
        .iter()
        .map(|c| type_weight(&c.resource_type))
        .sum();
    let destroy_cost: u32 = plan
        .changes
        .iter()
        .filter(|c| c.action == types::PlanAction::Destroy)
        .map(|c| type_weight(&c.resource_type) * 2) // destructive = 2x
        .sum();
    println!(
        "\nCost: {} total (create/update: {}, destroy: {})",
        total_cost + destroy_cost,
        total_cost,
        destroy_cost
    );
    if destroy_cost > 10 {
        println!(
            "  {} High destructive cost — consider --dry-run first",
            red("!")
        );
    }
}

/// FJ-344: Compact one-line-per-resource plan output.
pub(crate) fn cmd_plan_compact(
    file: &Path,
    state_dir: &Path,
    machine_filter: Option<&str>,
    json: bool,
) -> Result<(), String> {
    let config = parse_and_validate(file)?;
    let execution_order = resolver::build_execution_order(&config)?;
    let locks = load_machine_locks(&config, state_dir, machine_filter)?;
    let plan = planner::plan(&config, &execution_order, &locks, None);

    if json {
        let compact: Vec<serde_json::Value> = plan
            .changes
            .iter()
            .map(|c| {
                serde_json::json!({
                    "resource": c.resource_id,
                    "action": format!("{:?}", c.action),
                    "machine": c.machine,
                })
            })
            .collect();
        println!(
            "{}",
            serde_json::to_string_pretty(&compact).unwrap_or_default()
        );
    } else {
        for change in &plan.changes {
            let icon = match change.action {
                types::PlanAction::Create => green("+"),
                types::PlanAction::Update => yellow("~"),
                types::PlanAction::Destroy => red("-"),
                types::PlanAction::NoOp => dim("="),
            };
            println!("  {} {} ({})", icon, change.resource_id, change.machine,);
        }
        println!(
            "\n{} change(s)",
            plan.changes
                .iter()
                .filter(|c| c.action != types::PlanAction::NoOp)
                .count()
        );
    }

    Ok(())
}

/// FJ-1250: Save an execution plan to a JSON file with config integrity hash.
pub(crate) fn save_plan_file(
    plan: &types::ExecutionPlan,
    config: &types::ForjarConfig,
    config_path: &Path,
    out_path: &Path,
) -> Result<(), String> {
    let config_yaml =
        serde_yaml_ng::to_string(config).map_err(|e| format!("serialize config: {e}"))?;
    let config_hash = hasher::hash_string(&config_yaml);

    let changes: Vec<serde_json::Value> = plan
        .changes
        .iter()
        .map(|c| {
            serde_json::json!({
                "resource_id": c.resource_id,
                "machine": c.machine,
                "resource_type": c.resource_type,
                "action": c.action,
                "description": c.description,
            })
        })
        .collect();

    let output = serde_json::json!({
        "format": "forjar-plan-v1",
        "config_file": config_path.display().to_string(),
        "config_hash": config_hash,
        "name": plan.name,
        "to_create": plan.to_create,
        "to_update": plan.to_update,
        "to_destroy": plan.to_destroy,
        "unchanged": plan.unchanged,
        "execution_order": plan.execution_order,
        "changes": changes,
    });

    let json = serde_json::to_string_pretty(&output).map_err(|e| format!("JSON error: {e}"))?;
    std::fs::write(out_path, json).map_err(|e| format!("write plan file: {e}"))?;
    Ok(())
}

/// FJ-1250: Load a saved plan file, validate config hash, and return the plan.
pub(crate) fn load_plan_file(
    plan_path: &Path,
    config: &types::ForjarConfig,
) -> Result<types::ExecutionPlan, String> {
    let content = std::fs::read_to_string(plan_path).map_err(|e| format!("read plan file: {e}"))?;
    let doc: serde_json::Value =
        serde_json::from_str(&content).map_err(|e| format!("parse plan file: {e}"))?;

    let format = doc.get("format").and_then(|v| v.as_str()).unwrap_or("");
    if format != "forjar-plan-v1" {
        return Err(format!("unsupported plan format: '{format}'"));
    }

    let stored_hash = doc
        .get("config_hash")
        .and_then(|v| v.as_str())
        .unwrap_or("");
    let config_yaml =
        serde_yaml_ng::to_string(config).map_err(|e| format!("serialize config: {e}"))?;
    let current_hash = hasher::hash_string(&config_yaml);
    if stored_hash != current_hash {
        return Err(
            "config has changed since plan was created — re-run `forjar plan` to regenerate"
                .to_string(),
        );
    }

    let changes_arr = doc
        .get("changes")
        .and_then(|v| v.as_array())
        .ok_or("plan file missing 'changes' array")?;
    let mut changes = Vec::new();
    for entry in changes_arr {
        let action_str = entry
            .get("action")
            .and_then(|v| v.as_str())
            .unwrap_or("no_op");
        let action = match action_str {
            "create" => types::PlanAction::Create,
            "update" => types::PlanAction::Update,
            "destroy" => types::PlanAction::Destroy,
            _ => types::PlanAction::NoOp,
        };
        let rt_str = entry
            .get("resource_type")
            .and_then(|v| v.as_str())
            .unwrap_or("file");
        let resource_type = match rt_str {
            "package" => types::ResourceType::Package,
            "service" => types::ResourceType::Service,
            "mount" => types::ResourceType::Mount,
            "user" => types::ResourceType::User,
            "docker" => types::ResourceType::Docker,
            "pepita" => types::ResourceType::Pepita,
            "network" => types::ResourceType::Network,
            "cron" => types::ResourceType::Cron,
            "recipe" => types::ResourceType::Recipe,
            "model" => types::ResourceType::Model,
            "gpu" => types::ResourceType::Gpu,
            _ => types::ResourceType::File,
        };
        changes.push(types::PlannedChange {
            resource_id: entry
                .get("resource_id")
                .and_then(|v| v.as_str())
                .unwrap_or("")
                .to_string(),
            machine: entry
                .get("machine")
                .and_then(|v| v.as_str())
                .unwrap_or("")
                .to_string(),
            resource_type,
            action,
            description: entry
                .get("description")
                .and_then(|v| v.as_str())
                .unwrap_or("")
                .to_string(),
        });
    }

    let execution_order: Vec<String> = doc
        .get("execution_order")
        .and_then(|v| v.as_array())
        .map(|arr| {
            arr.iter()
                .filter_map(|v| v.as_str().map(String::from))
                .collect()
        })
        .unwrap_or_default();

    Ok(types::ExecutionPlan {
        name: doc
            .get("name")
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .to_string(),
        changes,
        execution_order,
        to_create: doc.get("to_create").and_then(|v| v.as_u64()).unwrap_or(0) as u32,
        to_update: doc.get("to_update").and_then(|v| v.as_u64()).unwrap_or(0) as u32,
        to_destroy: doc.get("to_destroy").and_then(|v| v.as_u64()).unwrap_or(0) as u32,
        unchanged: doc.get("unchanged").and_then(|v| v.as_u64()).unwrap_or(0) as u32,
    })
}

/// FJ-1379: Print per-resource --why explanation.
fn print_why_explanation(
    config: &types::ForjarConfig,
    locks: &std::collections::HashMap<String, types::StateLock>,
    execution_order: &[String],
    tag_filter: Option<&str>,
) {
    println!("\n{}", bold("Change Explanations (--why):"));
    let reasons = collect_why_reasons(config, locks, execution_order, tag_filter);
    for reason in &reasons {
        let icon = action_icon(&reason.action);
        println!("  {} {} on {}", icon, reason.resource_id, reason.machine);
        for r in &reason.reasons {
            println!("    {}", dim(&format!("- {r}")));
        }
    }
    println!();
}

/// Collect non-noop change reasons for all matching resources.
fn collect_why_reasons(
    config: &types::ForjarConfig,
    locks: &std::collections::HashMap<String, types::StateLock>,
    execution_order: &[String],
    tag_filter: Option<&str>,
) -> Vec<crate::core::planner::why::ChangeReason> {
    use crate::core::planner::why;
    let mut results = Vec::new();
    for resource_id in execution_order {
        let Some(resource) = config.resources.get(resource_id) else {
            continue;
        };
        if let Some(tag) = tag_filter {
            if !resource.tags.iter().any(|t| t == tag) {
                continue;
            }
        }
        for machine_name in resource.machine.iter() {
            let reason = why::explain_why(resource_id, resource, machine_name, locks);
            if reason.action != types::PlanAction::NoOp {
                results.push(reason);
            }
        }
    }
    results
}

/// Action icon for display.
fn action_icon(action: &types::PlanAction) -> String {
    match action {
        types::PlanAction::Create => green("+"),
        types::PlanAction::Update => yellow("~"),
        types::PlanAction::Destroy => red("-"),
        types::PlanAction::NoOp => dim("="),
    }
}