Skip to content

Commit 4954c34

Browse files
authored
Merge pull request #1062 from itowlson/templates-empty-list-offer-to-install
`spin templates list`: if empty, offer to install
2 parents f13890c + 350cc42 commit 4954c34

File tree

2 files changed

+46
-40
lines changed

2 files changed

+46
-40
lines changed

src/commands/new.rs

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ fn merge_values(from_file: &mut HashMap<String, String>, from_cli: &[ParameterVa
202202
}
203203

204204
async fn prompt_template(template_manager: &TemplateManager) -> anyhow::Result<Option<Template>> {
205-
let mut templates = match get_or_install_templates(template_manager).await? {
205+
let mut templates = match list_or_install_templates(template_manager).await? {
206206
Some(t) => t,
207207
None => return Ok(None),
208208
};
@@ -223,48 +223,17 @@ async fn prompt_template(template_manager: &TemplateManager) -> anyhow::Result<O
223223
Ok(Some(choice))
224224
}
225225

226-
const DEFAULT_TEMPLATES_INSTALL_PROMPT: &str =
227-
"You don't have any templates yet. Would you like to install the default set?";
228-
const DEFAULT_TEMPLATE_REPO: &str = "https://github.com/fermyon/spin";
229-
230-
async fn get_or_install_templates(
226+
async fn list_or_install_templates(
231227
template_manager: &TemplateManager,
232228
) -> anyhow::Result<Option<Vec<Template>>> {
233229
let templates = template_manager.list().await?.templates;
234230
if templates.is_empty() {
235-
let should_install = dialoguer::Confirm::new()
236-
.with_prompt(DEFAULT_TEMPLATES_INSTALL_PROMPT)
237-
.default(true)
238-
.interact_opt()?;
239-
if should_install == Some(true) {
240-
install_default_templates().await?;
241-
Ok(Some(template_manager.list().await?.templates))
242-
} else {
243-
println!(
244-
"You can install the default templates later with 'spin install --git {}'",
245-
DEFAULT_TEMPLATE_REPO
246-
);
247-
Ok(None)
248-
}
231+
super::templates::prompt_install_default_templates(template_manager).await
249232
} else {
250233
Ok(Some(templates))
251234
}
252235
}
253236

254-
async fn install_default_templates() -> Result<(), anyhow::Error> {
255-
let install_cmd = super::templates::Install {
256-
git: Some(DEFAULT_TEMPLATE_REPO.to_owned()),
257-
branch: None,
258-
dir: None,
259-
update: false,
260-
};
261-
install_cmd
262-
.run()
263-
.await
264-
.context("Failed to install the default templates")?;
265-
Ok(())
266-
}
267-
268237
async fn prompt_name() -> anyhow::Result<String> {
269238
let mut prompt = "Enter a name for your new project";
270239
loop {

src/commands/templates.rs

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ use spin_templates::{
1313
const INSTALL_FROM_DIR_OPT: &str = "FROM_DIR";
1414
const INSTALL_FROM_GIT_OPT: &str = "FROM_GIT";
1515

16+
const DEFAULT_TEMPLATES_INSTALL_PROMPT: &str =
17+
"You don't have any templates yet. Would you like to install the default set?";
18+
const DEFAULT_TEMPLATE_REPO: &str = "https://github.com/fermyon/spin";
19+
1620
/// Commands for working with WebAssembly component templates.
1721
#[derive(Subcommand, Debug)]
1822
pub enum TemplateCommands {
@@ -176,9 +180,12 @@ impl List {
176180
.await
177181
.context("Failed to list templates")?;
178182

179-
match self.format {
180-
ListFormat::Table => self.print_templates_table(&list_results),
181-
ListFormat::Json => self.print_templates_json(&list_results)?,
183+
match (&self.format, list_results.templates.is_empty()) {
184+
(ListFormat::Table, false) => self.print_templates_table(&list_results),
185+
(ListFormat::Table, true) => {
186+
prompt_install_default_templates(&template_manager).await?;
187+
}
188+
(ListFormat::Json, _) => self.print_templates_json(&list_results)?,
182189
};
183190

184191
Ok(())
@@ -188,9 +195,6 @@ impl List {
188195
let templates = &list_results.templates;
189196
let warnings = &list_results.warnings;
190197
if templates.is_empty() {
191-
println!("You have no templates installed. Run");
192-
println!("spin templates install --git https://github.com/fermyon/spin");
193-
println!("to install a starter set.");
194198
println!();
195199
} else {
196200
let mut table = Table::new();
@@ -265,3 +269,36 @@ fn list_warn_reason_text(reason: &InstalledTemplateWarning) -> String {
265269
InstalledTemplateWarning::InvalidManifest(msg) => format!("Template load error: {}", msg),
266270
}
267271
}
272+
273+
pub(crate) async fn prompt_install_default_templates(
274+
template_manager: &TemplateManager,
275+
) -> anyhow::Result<Option<Vec<Template>>> {
276+
let should_install = dialoguer::Confirm::new()
277+
.with_prompt(DEFAULT_TEMPLATES_INSTALL_PROMPT)
278+
.default(true)
279+
.interact_opt()?;
280+
if should_install == Some(true) {
281+
install_default_templates().await?;
282+
Ok(Some(template_manager.list().await?.templates))
283+
} else {
284+
println!(
285+
"You can install the default templates later with 'spin templates install --git {}'",
286+
DEFAULT_TEMPLATE_REPO
287+
);
288+
Ok(None)
289+
}
290+
}
291+
292+
async fn install_default_templates() -> anyhow::Result<()> {
293+
let install_cmd = Install {
294+
git: Some(DEFAULT_TEMPLATE_REPO.to_owned()),
295+
branch: None,
296+
dir: None,
297+
update: false,
298+
};
299+
install_cmd
300+
.run()
301+
.await
302+
.context("Failed to install the default templates")?;
303+
Ok(())
304+
}

0 commit comments

Comments
 (0)