feat: harden community production foundation through story 1.5
This commit is contained in:
1 parent
c30461cc92
commit
f6fc2e5c9b
161 files changed
+16758
-2515
No files matched your search
@@ -0,0 +1,73 @@
|
||||
use std::{env, fs, path::Path};
|
||||
|
||||
use crank_config::render::{
|
||||
BEGIN_MARKER, DOC_BEGIN_MARKER, DOC_END_MARKER, END_MARKER, env_section, reference_section,
|
||||
replace_marked, schema_json,
|
||||
};
|
||||
|
||||
fn main() {
|
||||
if let Err(error) = run() {
|
||||
eprintln!("config contract check failed: {error}");
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
fn run() -> Result<(), String> {
|
||||
let mode = env::args().nth(1).unwrap_or_else(|| "--check".to_owned());
|
||||
if !matches!(mode.as_str(), "--check" | "--write") {
|
||||
return Err("expected --check or --write".to_owned());
|
||||
}
|
||||
let write = mode == "--write";
|
||||
sync_file(
|
||||
Path::new("docs/schemas/runtime-config.schema.json"),
|
||||
schema_json(),
|
||||
write,
|
||||
)?;
|
||||
for (path, production) in [
|
||||
(".env.example", false),
|
||||
("deploy/community/.env.example", true),
|
||||
("deploy/community/.env.images.example", true),
|
||||
] {
|
||||
sync_marked(
|
||||
Path::new(path),
|
||||
BEGIN_MARKER,
|
||||
END_MARKER,
|
||||
&env_section(production),
|
||||
write,
|
||||
)?;
|
||||
}
|
||||
sync_marked(
|
||||
Path::new("docs/runtime-config.md"),
|
||||
DOC_BEGIN_MARKER,
|
||||
DOC_END_MARKER,
|
||||
&reference_section(),
|
||||
write,
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn sync_marked(
|
||||
path: &Path,
|
||||
begin: &str,
|
||||
end: &str,
|
||||
replacement: &str,
|
||||
write: bool,
|
||||
) -> Result<(), String> {
|
||||
let current =
|
||||
fs::read_to_string(path).map_err(|_| format!("cannot read {}", path.display()))?;
|
||||
let expected = replace_marked(¤t, begin, end, replacement)
|
||||
.ok_or_else(|| format!("missing generated markers in {}", path.display()))?;
|
||||
sync_file(path, expected, write)
|
||||
}
|
||||
|
||||
fn sync_file(path: &Path, expected: String, write: bool) -> Result<(), String> {
|
||||
let current = fs::read_to_string(path).unwrap_or_default();
|
||||
if current == expected {
|
||||
return Ok(());
|
||||
}
|
||||
if write {
|
||||
fs::write(path, expected).map_err(|_| format!("cannot write {}", path.display()))
|
||||
} else {
|
||||
Err(format!("{} is out of date", path.display()))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user