@@ -13,6 +13,10 @@ use spin_templates::{
13
13
const INSTALL_FROM_DIR_OPT : & str = "FROM_DIR" ;
14
14
const INSTALL_FROM_GIT_OPT : & str = "FROM_GIT" ;
15
15
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
+
16
20
/// Commands for working with WebAssembly component templates.
17
21
#[ derive( Subcommand , Debug ) ]
18
22
pub enum TemplateCommands {
@@ -176,9 +180,12 @@ impl List {
176
180
. await
177
181
. context ( "Failed to list templates" ) ?;
178
182
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) ?,
182
189
} ;
183
190
184
191
Ok ( ( ) )
@@ -188,9 +195,6 @@ impl List {
188
195
let templates = & list_results. templates ;
189
196
let warnings = & list_results. warnings ;
190
197
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." ) ;
194
198
println ! ( ) ;
195
199
} else {
196
200
let mut table = Table :: new ( ) ;
@@ -265,3 +269,36 @@ fn list_warn_reason_text(reason: &InstalledTemplateWarning) -> String {
265
269
InstalledTemplateWarning :: InvalidManifest ( msg) => format ! ( "Template load error: {}" , msg) ,
266
270
}
267
271
}
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