Skip to content

Commit eb8b4c2

Browse files
committed
slight changes to template handling
1 parent c5bd9a1 commit eb8b4c2

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

src/cmd/new.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ use crate::adr::{
1212
remove_status,
1313
};
1414

15-
static NEW_TEMPLATE: &str = include_str!("../../templates/nygard/new.md");
15+
static DEFAULT_NEW_TEMPLATE: &str = include_str!("../../templates/nygard/new.md");
16+
static DEFAULT_CUSTOM_TEMPLATE_FILENAME: &str = "templates/template.md";
1617

1718
#[derive(Debug, Args)]
1819
#[command(version, about, long_about = None)]
@@ -28,13 +29,8 @@ pub(crate) struct NewArgs {
2829
title: Vec<String>,
2930
/// Use a custom template when generating the new Architectural Decision Record.
3031
/// Relative paths are resolved with respect to the directory specified in `.adr-dir`.
31-
#[arg(
32-
default_value = "templates/template.md",
33-
env = "ADRS_TEMPLATE_DIR",
34-
short = 'T',
35-
long
36-
)]
37-
template: PathBuf,
32+
#[arg(env = "ADRS_TEMPLATE", short, long)]
33+
template: Option<PathBuf>,
3834
}
3935

4036
#[derive(Debug, Serialize)]
@@ -48,9 +44,22 @@ struct NewAdrContext {
4844

4945
pub(crate) fn run(args: &NewArgs) -> Result<()> {
5046
let adr_dir = find_adr_dir().context("No ADR directory found")?;
51-
let number = next_adr_number(&adr_dir)?;
47+
let raw_template = if let Some(template) = &args.template {
48+
if !template.exists() {
49+
return Err(anyhow::anyhow!(
50+
"Template file not found: {}",
51+
template.display()
52+
));
53+
}
54+
read_to_string(template)?
55+
} else if let Ok(template) = read_to_string(adr_dir.join(DEFAULT_CUSTOM_TEMPLATE_FILENAME)) {
56+
template
57+
} else {
58+
DEFAULT_NEW_TEMPLATE.to_string()
59+
};
5260

5361
let title = args.title.join(" ");
62+
let number = next_adr_number(&adr_dir)?;
5463

5564
let superseded = args
5665
.superseded
@@ -102,12 +111,6 @@ pub(crate) fn run(args: &NewArgs) -> Result<()> {
102111
linked,
103112
};
104113

105-
let template_file = adr_dir.join(&args.template);
106-
let raw_template = if template_file.exists() {
107-
read_to_string(template_file)?
108-
} else {
109-
NEW_TEMPLATE.to_string()
110-
};
111114
let mut registry = Handlebars::new();
112115
registry.register_template_string("new_adr", raw_template)?;
113116
let rendered = registry.render("new_adr", &new_context)?;

tests/test_new.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ fn test_new_template() {
209209
Command::cargo_bin("adrs")
210210
.unwrap()
211211
.arg("new")
212-
.arg("-T")
212+
.arg("-t")
213213
.arg(custom_location_template_path)
214214
.arg("Test template custom location")
215215
.assert()

0 commit comments

Comments
 (0)