greentic_component/cmd/
hash.rs

1use std::fs;
2use std::path::{Path, PathBuf};
3
4use anyhow::{Context, Result};
5use clap::{Args, Parser};
6use serde_json::Value;
7
8use crate::path_safety::normalize_under_root;
9
10#[derive(Args, Debug, Clone)]
11#[command(about = "Recompute the wasm hash inside component.manifest.json")]
12pub struct HashArgs {
13    /// Path to component.manifest.json
14    #[arg(default_value = "component.manifest.json")]
15    pub manifest: PathBuf,
16    /// Optional override for the wasm artifact path
17    #[arg(long)]
18    pub wasm: Option<PathBuf>,
19}
20
21#[derive(Parser, Debug)]
22struct HashCli {
23    #[command(flatten)]
24    args: HashArgs,
25}
26
27pub fn parse_from_cli() -> HashArgs {
28    HashCli::parse().args
29}
30
31pub fn run(args: HashArgs) -> Result<()> {
32    let workspace_root = std::env::current_dir()
33        .context("failed to read current directory")?
34        .canonicalize()
35        .context("failed to canonicalize workspace root")?;
36    let manifest_path =
37        normalize_or_canonicalize(&workspace_root, &args.manifest).with_context(|| {
38            format!(
39                "manifest path escapes workspace root: {}",
40                args.manifest.display()
41            )
42        })?;
43    let manifest_text = fs::read_to_string(&manifest_path)
44        .with_context(|| format!("failed to read {}", manifest_path.display()))?;
45    let mut manifest: Value = serde_json::from_str(&manifest_text)
46        .with_context(|| format!("invalid json: {}", manifest_path.display()))?;
47    let manifest_root = manifest_path
48        .parent()
49        .unwrap_or(workspace_root.as_path())
50        .canonicalize()
51        .with_context(|| {
52            format!(
53                "failed to canonicalize manifest directory {}",
54                manifest_path.display()
55            )
56        })?;
57    let wasm_candidate = resolve_wasm_path(&manifest, args.wasm.as_deref())?;
58    let wasm_path =
59        normalize_or_canonicalize(&manifest_root, &wasm_candidate).with_context(|| {
60            format!(
61                "wasm path escapes manifest root {}",
62                manifest_root.display()
63            )
64        })?;
65    let wasm_bytes = fs::read(&wasm_path)
66        .with_context(|| format!("failed to read wasm at {}", wasm_path.display()))?;
67    let digest = blake3::hash(&wasm_bytes).to_hex().to_string();
68    manifest["hashes"]["component_wasm"] = Value::String(format!("blake3:{digest}"));
69    let formatted = serde_json::to_string_pretty(&manifest)?;
70    fs::write(&manifest_path, formatted + "\n")
71        .with_context(|| format!("failed to write {}", manifest_path.display()))?;
72    println!(
73        "Updated {} with hash of {}",
74        manifest_path.display(),
75        wasm_path.display()
76    );
77    Ok(())
78}
79
80fn resolve_wasm_path(manifest: &Value, override_path: Option<&Path>) -> Result<PathBuf> {
81    if let Some(path) = override_path {
82        return Ok(path.to_path_buf());
83    }
84    let artifact = manifest
85        .get("artifacts")
86        .and_then(|art| art.get("component_wasm"))
87        .and_then(Value::as_str)
88        .ok_or_else(|| anyhow::anyhow!("manifest is missing artifacts.component_wasm"))?;
89    Ok(PathBuf::from(artifact))
90}
91
92fn normalize_or_canonicalize(root: &Path, candidate: &Path) -> Result<PathBuf> {
93    if candidate.is_absolute() {
94        return candidate
95            .canonicalize()
96            .with_context(|| format!("failed to canonicalize {}", candidate.display()));
97    }
98    normalize_under_root(root, candidate)
99}