Skip to content

Commit 07def28

Browse files
committed
slight changes to template handling
these are kinda lame but oh well
1 parent c5bd9a1 commit 07def28

File tree

4 files changed

+73
-18
lines changed

4 files changed

+73
-18
lines changed

src/cmd/config.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::path::PathBuf;
2+
13
use anyhow::Result;
24
use clap::Args;
35

@@ -11,10 +13,26 @@ pub(crate) fn run(_args: &ConfigArgs) -> Result<()> {
1113
"adrs_bin_dir={}",
1214
std::env::current_exe().unwrap().parent().unwrap().display()
1315
);
14-
println!("adrs_template_dir=embedded");
16+
17+
// find the template directory. if ADRS_TEMPLATE is set, use that. otherwise, check
18+
// to see if there is an adr dir, and if it has a templates directory, use that.
19+
// otherwise use embedded
20+
if let Ok(template_file) = std::env::var("ADRS_TEMPLATE") {
21+
let mut path = PathBuf::from(template_file);
22+
path.pop();
23+
println!("adrs_template_dir={}", path.display());
24+
} else if let Ok(adr_dir) = read_adr_dir_file() {
25+
if adr_dir.join("templates").exists() {
26+
println!("adrs_template_dir={}", adr_dir.join("templates").display());
27+
} else {
28+
println!("adrs_template_dir=embedded");
29+
}
30+
} else {
31+
println!("adrs_template_dir=embedded");
32+
}
33+
1534
if let Ok(adr_dir) = read_adr_dir_file() {
1635
println!("adrs_dir={}", adr_dir.display());
17-
return Ok(());
1836
}
1937
Ok(())
2038
}

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_config.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,37 @@ fn test_config() {
1717
.and(predicate::str::contains("adrs_template_dir=embedded")),
1818
);
1919
}
20+
21+
#[test]
22+
#[serial_test::serial]
23+
fn test_config_with_embedded_template() {
24+
let temp = TempDir::new().unwrap();
25+
std::env::set_current_dir(temp.path()).unwrap();
26+
// std::env::set_var(
27+
// "ADRS_TEMPLATE",
28+
// temp.path().join("templates").to_str().unwrap(),
29+
// );
30+
std::env::set_var("EDITOR", "cat");
31+
32+
Command::cargo_bin("adrs")
33+
.unwrap()
34+
.arg("init")
35+
.assert()
36+
.success();
37+
38+
Command::cargo_bin("adrs")
39+
.unwrap()
40+
.arg("new")
41+
.arg("Test new")
42+
.assert()
43+
.success();
44+
45+
Command::cargo_bin("adrs")
46+
.unwrap()
47+
.arg("config")
48+
.assert()
49+
.stdout(
50+
predicate::str::contains("adrs_template_dir=embedded")
51+
.and(predicate::str::contains("adrs_dir=doc/adr")),
52+
);
53+
}

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)