Skip to content

Commit d3cabf4

Browse files
committed
cli: templates check: add option to --stabilise date and RNG
1 parent d531f13 commit d3cabf4

File tree

5 files changed

+113
-92
lines changed

5 files changed

+113
-92
lines changed

crates/cli/src/commands/templates.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::{fmt::Write, process::ExitCode};
88

99
use anyhow::{Context as _, bail};
1010
use camino::Utf8PathBuf;
11+
use chrono::DateTime;
1112
use clap::Parser;
1213
use figment::Figment;
1314
use mas_config::{
@@ -34,14 +35,20 @@ enum Subcommand {
3435
/// The directory must either not exist or be empty.
3536
#[arg(long = "out-dir")]
3637
out_dir: Option<Utf8PathBuf>,
38+
39+
/// Attempt to remove 'unstable' template input data such as asset
40+
/// hashes, in order to make renders more reproducible between
41+
/// versions.
42+
#[arg(long = "stabilise")]
43+
stabilise: bool,
3744
},
3845
}
3946

4047
impl Options {
4148
pub async fn run(self, figment: &Figment) -> anyhow::Result<ExitCode> {
4249
use Subcommand as SC;
4350
match self.subcommand {
44-
SC::Check { out_dir } => {
51+
SC::Check { out_dir, stabilise } => {
4552
let _span = info_span!("cli.templates.check").entered();
4653

4754
let template_config = TemplatesConfig::extract_or_default(figment)
@@ -59,9 +66,17 @@ impl Options {
5966
let captcha_config = CaptchaConfig::extract_or_default(figment)
6067
.map_err(anyhow::Error::from_boxed)?;
6168

62-
let clock = SystemClock::default();
63-
// XXX: we should disallow SeedableRng::from_entropy
64-
let mut rng = rand_chacha::ChaChaRng::from_entropy();
69+
let now = if stabilise {
70+
DateTime::from_timestamp_secs(0).unwrap()
71+
} else {
72+
SystemClock::default().now()
73+
};
74+
let rng = if stabilise {
75+
rand_chacha::ChaChaRng::from_seed([42; 32])
76+
} else {
77+
// XXX: we should disallow SeedableRng::from_entropy
78+
rand_chacha::ChaChaRng::from_entropy()
79+
};
6580
let url_builder =
6681
mas_router::UrlBuilder::new("https://example.com/".parse()?, None, None);
6782
let site_config = site_config_from_config(
@@ -79,7 +94,7 @@ impl Options {
7994
true,
8095
)
8196
.await?;
82-
let all_renders = templates.check_render(clock.now(), &mut rng)?;
97+
let all_renders = templates.check_render(now, &rng)?;
8398

8499
if let Some(out_dir) = out_dir {
85100
// Save renders to disk.

0 commit comments

Comments
 (0)