diff --git a/src/cli/cli.rs b/src/cli/cli.rs index b1dba9b..0b67174 100644 --- a/src/cli/cli.rs +++ b/src/cli/cli.rs @@ -4,6 +4,7 @@ use clap::{Args, Parser, Subcommand}; use crate::features::ProjectFeatureArgs; use crate::initializers::InitProjectArgs; +use crate::extras::ListItemArgs; #[derive(Parser)] pub struct Cli { @@ -36,4 +37,7 @@ pub enum Command { #[clap(name = "feature", about = "Add a feature to the project")] ProjectFeature(ProjectFeatureArgs), + + #[clap(name = "list", about = "List available templates and features")] + ListItems(ListItemArgs) } diff --git a/src/extras/extras.rs b/src/extras/extras.rs new file mode 100644 index 0000000..67c8f47 --- /dev/null +++ b/src/extras/extras.rs @@ -0,0 +1,79 @@ +use clap::{Args, Parser, ValueEnum}; + +use std::{fmt, fmt::Write}; + +use crate::{ + app_config::AppConfig, +}; + +#[derive(Clone, Copy, PartialEq, Eq, ValueEnum)] +pub enum ListItem { + All, + Templates, + Features, +} + +impl fmt::Display for ListItem { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + ListItem::All => { + write!(f, "all") + } + ListItem::Templates => { + write!(f, "templates") + } + ListItem::Features => { + write!(f, "features") + } + } + } +} + +#[derive(Args)] +pub struct ListItemArgs { + + #[arg(short, long, default_value_t = ListItem::All)] + pub select: ListItem, +} + +pub fn list_items(args: ListItemArgs, config: AppConfig) -> Result<(), String> { + + let mut out = String::new(); + + // Gets all available projects that is configured + if args.select == ListItem::Templates || args.select == ListItem::All { + write!(out, "Project templates:\n------------------\n"); + let templates = config + .templates + .iter() + .for_each(|t| + write!(out, "Type: {}\nProfile: {}\n\n", t.name, t.profile).unwrap() + ); + } + + // Gets all available features that are configured + if args.select == ListItem::Features || args.select == ListItem::All { + write!(out, "Feature: Licenses:\n------------------\n"); + let features = config + .features + .licenses + .iter() + .for_each(|t| + write!(out, "Name: {}\n\n", t.name).unwrap() + ); + + write!(out, "Feature: Linters:\n-----------------\n"); + let linters = config + .features + .linters + .iter() + .for_each(|t| + write!(out, "Type: {}\n\n", t.name).unwrap() + ); + } + + // Get all available linters that i + println!("{}", out); + + Ok(()) +} diff --git a/src/extras/mod.rs b/src/extras/mod.rs new file mode 100644 index 0000000..be94842 --- /dev/null +++ b/src/extras/mod.rs @@ -0,0 +1,5 @@ +//! src/extras/mod.rs + +pub mod extras; + +pub use extras::{ListItem, ListItemArgs, list_items}; diff --git a/src/initializers/init_project.rs b/src/initializers/init_project.rs index dc84a24..c1136fd 100644 --- a/src/initializers/init_project.rs +++ b/src/initializers/init_project.rs @@ -48,16 +48,16 @@ impl fmt::Display for InitProjectError { write!(f, "Invalid template input: {}", e) } InitProjectError::FileWrite(e) => { - write!(f, "Template file write erro: {}", e) + write!(f, "Template file write error: {}", e) } InitProjectError::NotFound(e) => { write!(f, "Template files not found: {}", e.display()) } InitProjectError::MavenProject(e) => { - write!(f, "{}", e) + write!(f, "Maven template project error: {}", e) } InitProjectError::AnsibleProject(e) => { - write!(f, "{}", e) + write!(f, "Ansible template project error: {}", e) } } } @@ -91,6 +91,9 @@ pub struct InitProjectArgs { #[arg(long)] pub settings: Option>, +// +// #[arg(long)] +// pub list: bool, } struct ProjectInitializer { @@ -134,6 +137,18 @@ pub fn handle( config: AppConfig, cache: AppCache, ) -> Result<(), InitProjectError> { + + // // Prints all available projects that is configured + // if args.list { + // config + // .templates + // .iter() + // .for_each(|t| + // println!("{}", t.name) + // ); + // return Ok(()); + // } + // Ensure the passed project type and given profile, if any, is present in the config file // before passing it along let template: PathBuf = config diff --git a/src/lib.rs b/src/lib.rs index e1ae10f..6354fb6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,3 +5,4 @@ pub mod app_config; pub mod initializers; pub mod features; pub mod utils; +pub mod extras; diff --git a/src/main.rs b/src/main.rs index 5314b14..b9a2f99 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,6 +11,7 @@ use repotools::{ cli, features::{ProjectFeatureError, project_feature}, initializers::{InitProjectError, init_project}, + extras, }; #[derive(Debug)] @@ -81,6 +82,11 @@ fn main() -> Result<(), AppError> { eprintln!("Could not add feature: {}", e) } } + Command::ListItems(args) => { + if let Err(e) = extras::list_items(args, config) { + eprintln!("Could not list items: {}", e) + } + } } Ok(())