Skip to main content

opencode_ralph_loop_cli/commands/
list.rs

1use crate::cli::OutputFormat;
2use crate::error::CliError;
3use crate::templates::{
4    RenderContext, canonical_templates, render_template, template_to_output_path,
5};
6
7pub fn run(output: &OutputFormat) -> Result<(), CliError> {
8    let ctx = RenderContext::default();
9    let mut entries: Vec<(&str, &str, String, usize)> = Vec::new();
10
11    for &template_path in canonical_templates() {
12        let rendered = render_template(template_path, &ctx)
13            .ok_or_else(|| CliError::Generic(format!("template not found: {template_path}")))?;
14        let hash = crate::hash::sha256_hex(&rendered);
15        let size = rendered.len();
16        let output_path = template_to_output_path(template_path);
17        entries.push((template_path, output_path, hash, size));
18    }
19
20    match output {
21        OutputFormat::Json => {
22            let json_entries: Vec<_> = entries
23                .iter()
24                .map(|(tpl, out, hash, size)| {
25                    serde_json::json!({
26                        "template": tpl,
27                        "output_path": out,
28                        "sha256": hash,
29                        "size_bytes": size,
30                    })
31                })
32                .collect();
33            println!(
34                "{}",
35                serde_json::to_string_pretty(&json_entries).unwrap_or_default()
36            );
37        }
38        _ => {
39            println!("{:<35} {:<35} SHA-256 (16 chars)", "Template", "Output");
40            println!("{}", "-".repeat(90));
41            for (tpl, out, hash, size) in &entries {
42                println!("{:<35} {:<35} {}... ({size}B)", tpl, out, &hash[..16]);
43            }
44        }
45    }
46
47    Ok(())
48}