Skip to content

Commit 1987881

Browse files
committed
feat: add accept-defaults to new templates
Signed-off-by: karthik Ganeshram <karthik.ganeshram@fermyon.com>
1 parent f57cf9f commit 1987881

File tree

11 files changed

+68
-11
lines changed

11 files changed

+68
-11
lines changed

crates/templates/src/manager.rs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ impl InstallationResults {
387387

388388
#[cfg(test)]
389389
mod tests {
390-
use std::{fs, path::PathBuf};
390+
use std::{collections::HashMap, fs, path::PathBuf};
391391

392392
use tempfile::tempdir;
393393

@@ -629,6 +629,7 @@ mod tests {
629629
output_path: output_dir.clone(),
630630
name: "my project".to_owned(),
631631
values,
632+
accept_defaults: false,
632633
};
633634

634635
template
@@ -644,4 +645,46 @@ mod tests {
644645
.unwrap();
645646
assert!(cargo.contains("name = \"my-project\""));
646647
}
648+
649+
#[tokio::test]
650+
async fn can_run_template_with_accept_defaults() {
651+
let temp_dir = tempdir().unwrap();
652+
let store = TemplateStore::new(temp_dir.path());
653+
let manager = TemplateManager { store };
654+
let source = TemplateSource::File(project_root());
655+
656+
manager
657+
.install(&source, &InstallOptions::default(), &DiscardingReporter)
658+
.await
659+
.unwrap();
660+
661+
let template = manager.get("http-rust").unwrap().unwrap();
662+
663+
let dest_temp_dir = tempdir().unwrap();
664+
let output_dir = dest_temp_dir.path().join("myproj");
665+
let values = HashMap::new();
666+
let options = RunOptions {
667+
output_path: output_dir.clone(),
668+
name: "my project".to_owned(),
669+
values,
670+
accept_defaults: true,
671+
};
672+
673+
template
674+
.run(options)
675+
.silent()
676+
.await
677+
.execute()
678+
.await
679+
.unwrap();
680+
681+
let cargo = tokio::fs::read_to_string(output_dir.join("Cargo.toml"))
682+
.await
683+
.unwrap();
684+
assert!(cargo.contains("name = \"my-project\""));
685+
let spin_toml = tokio::fs::read_to_string(output_dir.join("spin.toml"))
686+
.await
687+
.unwrap();
688+
assert!(spin_toml.contains("route = \"/...\""));
689+
}
647690
}

crates/templates/src/run.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ pub struct RunOptions {
2525
pub output_path: PathBuf,
2626
/// The values to use for template parameters.
2727
pub values: HashMap<String, String>,
28+
/// If true accept default values where available
29+
pub accept_defaults: bool,
2830
}
2931

3032
enum Cancellable<T, E> {
@@ -282,7 +284,10 @@ impl Run {
282284
fn populate_parameter_interactive(&self, parameter: &TemplateParameter) -> Option<String> {
283285
match self.options.values.get(parameter.id()) {
284286
Some(s) => Some(s.clone()),
285-
None => crate::interaction::prompt_parameter(parameter),
287+
None => match (self.options.accept_defaults, parameter.default_value()) {
288+
(true, Some(v)) => Some(v.to_string()),
289+
_ => crate::interaction::prompt_parameter(parameter),
290+
},
286291
}
287292
}
288293

@@ -298,7 +303,10 @@ impl Run {
298303
fn populate_parameter_silent(&self, parameter: &TemplateParameter) -> anyhow::Result<String> {
299304
match self.options.values.get(parameter.id()) {
300305
Some(s) => Ok(s.clone()),
301-
None => Err(anyhow!("Parameter '{}' not provided", parameter.id())),
306+
None => match (self.options.accept_defaults, parameter.default_value()) {
307+
(true, Some(v)) => Ok(v.to_string()),
308+
_ => Err(anyhow!("Parameter '{}' not provided", parameter.id())),
309+
},
302310
}
303311
}
304312

src/commands/new.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ pub struct NewCommand {
3434
/// file.
3535
#[clap(long = "values-file")]
3636
pub values_file: Option<PathBuf>,
37+
38+
/// An optional argument that allows to skip prompts for the manifest file
39+
/// by accepting the defaults if available on the template
40+
#[clap(long = "accept-defaults", takes_value = false)]
41+
pub accept_defaults: bool,
3742
}
3843

3944
impl NewCommand {
@@ -59,6 +64,7 @@ impl NewCommand {
5964
name: self.name.clone(),
6065
output_path,
6166
values,
67+
accept_defaults: self.accept_defaults,
6268
};
6369

6470
match template {

templates/http-c/metadata/spin-template.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ id = "http-c"
33
description = "HTTP request handler using C and the Zig toolchain"
44

55
[parameters]
6-
project-description = { type = "string", prompt = "Project description" }
6+
project-description = { type = "string", prompt = "Project description", default = "" }
77
http-base = { type = "string", prompt = "HTTP base", default = "/", pattern = "^/\\S*$" }
88
http-path = { type = "string", prompt = "HTTP path", default = "/...", pattern = "^/\\S*$" }

templates/http-go/metadata/spin-template.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ id = "http-go"
33
description = "HTTP request handler using (Tiny)Go"
44

55
[parameters]
6-
project-description = { type = "string", prompt = "Project description" }
6+
project-description = { type = "string", prompt = "Project description", default = "" }
77
http-base = { type = "string", prompt = "HTTP base", default = "/", pattern = "^/\\S*$" }
88
http-path = { type = "string", prompt = "HTTP path", default = "/...", pattern = "^/\\S*$" }

templates/http-grain/metadata/spin-template.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ id = "http-grain"
33
description = "HTTP request handler using Grain"
44

55
[parameters]
6-
project-description = { type = "string", prompt = "Project description" }
6+
project-description = { type = "string", prompt = "Project description", default = "" }
77
http-base = { type = "string", prompt = "HTTP base", default = "/", pattern = "^/\\S*$" }
88
http-path = { type = "string", prompt = "HTTP path", default = "/...", pattern = "^/\\S*$" }

templates/http-rust/metadata/spin-template.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ id = "http-rust"
33
description = "HTTP request handler using Rust"
44

55
[parameters]
6-
project-description = { type = "string", prompt = "Project description" }
6+
project-description = { type = "string", prompt = "Project description", default = "" }
77
http-base = { type = "string", prompt = "HTTP base", default = "/", pattern = "^/\\S*$" }
88
http-path = { type = "string", prompt = "HTTP path", default = "/...", pattern = "^/\\S*$" }

templates/http-swift/metadata/spin-template.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ id = "http-swift"
33
description = "HTTP request handler using SwiftWasm"
44

55
[parameters]
6-
project-description = { type = "string", prompt = "Project description" }
6+
project-description = { type = "string", prompt = "Project description", default = "" }
77
http-base = { type = "string", prompt = "HTTP base", default = "/", pattern = "^/\\S*$" }
88
http-path = { type = "string", prompt = "HTTP path", default = "/...", pattern = "^/\\S*$" }

templates/http-zig/metadata/spin-template.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ id = "http-zig"
33
description = "HTTP request handler using Zig"
44

55
[parameters]
6-
project-description = { type = "string", prompt = "Project description" }
6+
project-description = { type = "string", prompt = "Project description", default = "" }
77
http-base = { type = "string", prompt = "HTTP base", default = "/", pattern = "^/\\S*$" }
88
http-path = { type = "string", prompt = "HTTP path", default = "/...", pattern = "^/\\S*$" }

templates/redis-go/metadata/spin-template.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ id = "redis-go"
33
description = "Redis message handler using (Tiny)Go"
44

55
[parameters]
6-
project-description = { type = "string", prompt = "Project description" }
6+
project-description = { type = "string", prompt = "Project description", default = "" }
77
redis-address = { type = "string", prompt = "Redis address", default = "redis://localhost:6379" }
88
redis-channel = { type = "string", prompt = "Redis channel" }

0 commit comments

Comments
 (0)