Skip to main content

ferrous_forge/commands/template/
mod.rs

1//! Template management commands
2
3use crate::Result;
4use crate::templates::TemplateRegistry;
5use clap::Subcommand;
6use console::style;
7use std::path::PathBuf;
8
9mod creation;
10mod display;
11mod utils;
12
13pub use creation::*;
14pub use display::*;
15pub use utils::*;
16
17/// Template subcommands
18#[derive(Debug, Subcommand)]
19pub enum TemplateCommand {
20    /// List available templates
21    List,
22
23    /// Create a new project from template
24    Create {
25        /// Template name
26        template: String,
27
28        /// Output directory
29        output: PathBuf,
30
31        /// Template variables in key=value format
32        #[arg(long = "var", short = 'v')]
33        variables: Vec<String>,
34    },
35
36    /// Show detailed information about a template
37    Info {
38        /// Template name
39        template: String,
40    },
41
42    /// Validate a template manifest
43    Validate {
44        /// Path to template manifest
45        manifest: PathBuf,
46    },
47}
48
49impl TemplateCommand {
50    /// Execute the template command
51    ///
52    /// # Errors
53    ///
54    /// Returns an error if the template cannot be found, variables are
55    /// invalid, or the project files cannot be written.
56    pub async fn execute(&self) -> Result<()> {
57        match self {
58            TemplateCommand::List => {
59                println!(
60                    "{}",
61                    style("📚 Available Ferrous Forge Templates").cyan().bold()
62                );
63                println!();
64
65                let registry = TemplateRegistry::new();
66                let templates = registry.list_templates();
67
68                if templates.is_empty() {
69                    println!("   No templates found.");
70                    return Ok(());
71                }
72
73                for (name, _kind, description) in &templates {
74                    println!("  {} {}", style("•").cyan(), style(name).white().bold());
75                    println!("    {}", style(description).dim());
76                    println!();
77                }
78
79                println!(
80                    "Use {} to create a project from a template.",
81                    style("ferrous-forge template create <template-name> <output-dir>").cyan()
82                );
83                println!(
84                    "Use {} to see detailed information about a template.",
85                    style("ferrous-forge template info <template-name>").cyan()
86                );
87                Ok(())
88            }
89
90            TemplateCommand::Create {
91                template,
92                output,
93                variables,
94            } => create_from_template(template, output, variables).await,
95
96            TemplateCommand::Info { template } => show_template_info(template).await,
97
98            TemplateCommand::Validate { manifest } => validate_template_manifest(manifest).await,
99        }
100    }
101}