From 82691275bb49af655227c74a32eb0ab562d4e297 Mon Sep 17 00:00:00 2001 From: itowlson Date: Mon, 9 Oct 2023 16:35:59 +1300 Subject: [PATCH] Make `redirect` template newable Signed-off-by: itowlson --- crates/templates/src/manager.rs | 11 ++++++++--- .../metadata/snippets/component.txt | 6 ++++++ .../metadata/spin-template.toml | 16 ++++++++++++++++ templates/redirect/content/.gitignore | 1 + templates/redirect/content/spin.toml | 13 +++++++++++++ .../redirect/metadata/snippets/component.txt | 2 +- templates/redirect/metadata/spin-template.toml | 9 +++++---- 7 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 crates/templates/tests/templates/add-only-redirect/metadata/snippets/component.txt create mode 100644 crates/templates/tests/templates/add-only-redirect/metadata/spin-template.toml create mode 100644 templates/redirect/content/.gitignore create mode 100644 templates/redirect/content/spin.toml diff --git a/crates/templates/src/manager.rs b/crates/templates/src/manager.rs index 677ca7e7cd..82c0479753 100644 --- a/crates/templates/src/manager.rs +++ b/crates/templates/src/manager.rs @@ -1033,10 +1033,15 @@ mod tests { let temp_dir = tempdir().unwrap(); let store = TemplateStore::new(temp_dir.path()); let manager = TemplateManager { store }; - let source = TemplateSource::File(project_root()); + let source1 = TemplateSource::File(test_data_root()); + let source2 = TemplateSource::File(project_root()); manager - .install(&source, &InstallOptions::default(), &DiscardingReporter) + .install(&source1, &InstallOptions::default(), &DiscardingReporter) + .await + .unwrap(); + manager + .install(&source2, &InstallOptions::default(), &DiscardingReporter) .await .unwrap(); @@ -1044,7 +1049,7 @@ mod tests { let manifest_path = dummy_dir.join("ignored_spin.toml"); let add_component = TemplateVariantInfo::AddComponent { manifest_path }; - let redirect = manager.get("redirect").unwrap().unwrap(); + let redirect = manager.get("add-only-redirect").unwrap().unwrap(); assert!(!redirect.supports_variant(&TemplateVariantInfo::NewApplication)); assert!(redirect.supports_variant(&add_component)); diff --git a/crates/templates/tests/templates/add-only-redirect/metadata/snippets/component.txt b/crates/templates/tests/templates/add-only-redirect/metadata/snippets/component.txt new file mode 100644 index 0000000000..5ae6628514 --- /dev/null +++ b/crates/templates/tests/templates/add-only-redirect/metadata/snippets/component.txt @@ -0,0 +1,6 @@ +[[component]] +source = { url = "https://github.com/fermyon/spin-redirect/releases/download/v0.1.0/redirect.wasm", digest = "sha256:8bee959843f28fef2a02164f5840477db81d350877e1c22cb524f41363468e52" } +id = "{{ project-name | kebab_case }}" +environment = { DESTINATION = "{{ redirect-to }}" } +[component.trigger] +route = "{{ redirect-from }}" diff --git a/crates/templates/tests/templates/add-only-redirect/metadata/spin-template.toml b/crates/templates/tests/templates/add-only-redirect/metadata/spin-template.toml new file mode 100644 index 0000000000..52d41a4915 --- /dev/null +++ b/crates/templates/tests/templates/add-only-redirect/metadata/spin-template.toml @@ -0,0 +1,16 @@ +manifest_version = "1" +id = "add-only-redirect" +description = "Redirects a HTTP route" +trigger_type = "http" +tags = ["redirect"] + +[new_application] +supported = false + +[add_component] +[add_component.snippets] +component = "component.txt" + +[parameters] +redirect-from = { type = "string", prompt = "Redirect from", pattern = "^/\\S*$" } +redirect-to = { type = "string", prompt = "Redirect to", pattern = "^/\\S*$" } diff --git a/templates/redirect/content/.gitignore b/templates/redirect/content/.gitignore new file mode 100644 index 0000000000..0688291f15 --- /dev/null +++ b/templates/redirect/content/.gitignore @@ -0,0 +1 @@ +.spin/ diff --git a/templates/redirect/content/spin.toml b/templates/redirect/content/spin.toml new file mode 100644 index 0000000000..8e9195f0cc --- /dev/null +++ b/templates/redirect/content/spin.toml @@ -0,0 +1,13 @@ +spin_manifest_version = "1" +authors = ["{{authors}}"] +description = "{{project-description}}" +name = "{{project-name}}" +trigger = { type = "http", base = "{{http-base}}" } +version = "0.1.0" + +[[component]] +source = { url = "https://github.com/fermyon/spin-redirect/releases/download/v0.1.0/redirect.wasm", digest = "sha256:8bee959843f28fef2a02164f5840477db81d350877e1c22cb524f41363468e52" } +id = "{{ project-name | kebab_case }}" +environment = { DESTINATION = "{{ redirect-to }}" } +[component.trigger] +route = "{{ redirect-from }}" diff --git a/templates/redirect/metadata/snippets/component.txt b/templates/redirect/metadata/snippets/component.txt index a91e1a8a32..5ae6628514 100644 --- a/templates/redirect/metadata/snippets/component.txt +++ b/templates/redirect/metadata/snippets/component.txt @@ -1,6 +1,6 @@ [[component]] source = { url = "https://github.com/fermyon/spin-redirect/releases/download/v0.1.0/redirect.wasm", digest = "sha256:8bee959843f28fef2a02164f5840477db81d350877e1c22cb524f41363468e52" } -id = "{{ project-name }}" +id = "{{ project-name | kebab_case }}" environment = { DESTINATION = "{{ redirect-to }}" } [component.trigger] route = "{{ redirect-from }}" diff --git a/templates/redirect/metadata/spin-template.toml b/templates/redirect/metadata/spin-template.toml index 98d14097b8..7681441047 100644 --- a/templates/redirect/metadata/spin-template.toml +++ b/templates/redirect/metadata/spin-template.toml @@ -4,13 +4,14 @@ description = "Redirects a HTTP route" trigger_type = "http" tags = ["redirect"] -[new_application] -supported = false - [add_component] +skip_files = ["spin.toml"] +skip_parameters = ["project-description", "http-base"] [add_component.snippets] component = "component.txt" [parameters] +project-description = { type = "string", prompt = "Description", default = "" } +http-base = { type = "string", prompt = "HTTP base", default = "/", pattern = "^/\\S*$" } redirect-from = { type = "string", prompt = "Redirect from", pattern = "^/\\S*$" } -redirect-to = { type = "string", prompt = "Redirect to", pattern = "^/\\S*$" } +redirect-to = { type = "string", prompt = "Redirect to" }