Skip to main content

gen_crds/
gen-crds.rs

1// A developer CLI: printing progress to stdout is the point.
2#![allow(clippy::print_stdout)]
3//! Emit every hyprstream CRD as YAML into `crates/hyprstream-k8s/crds/` **and**
4//! into the Helm chart's `charts/hyprstream/crds/` (Helm installs that
5//! directory before any template; the files there cannot be templated).
6//!
7//! Run with `cargo run -p hyprstream-k8s --bin gen-crds`. The emitted files are
8//! committed in both locations; CI (or a developer) re-runs this after changing
9//! a schema, and the `committed_yaml_matches_generated` test fails if either
10//! copy is stale. No cluster is required — the CRDs are derived purely from
11//! the Rust types.
12
13use std::error::Error;
14use std::path::PathBuf;
15
16fn main() -> Result<(), Box<dyn Error>> {
17    // Write next to the crate source regardless of the current working dir.
18    let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
19    let out_dirs = [
20        manifest_dir.join("crds"),
21        // The chart ships the same manifests via its `crds/` directory.
22        manifest_dir.join("../../charts/hyprstream/crds"),
23    ];
24
25    for (crd, stem) in hyprstream_k8s::crd_manifests() {
26        let yaml = serde_yaml::to_string(&crd)?;
27        let doc = format!("# Generated by `cargo run -p hyprstream-k8s --bin gen-crds`.\n# Do not edit by hand; edit the Rust types in crates/hyprstream-k8s/src/.\n{yaml}");
28        for out_dir in &out_dirs {
29            std::fs::create_dir_all(out_dir)?;
30            let path = out_dir.join(format!("{stem}.yaml"));
31            std::fs::write(&path, &doc)?;
32            println!("wrote {}", path.display());
33        }
34    }
35    Ok(())
36}