From 0c01ddd81d34454b088926438ff45c25609abda9 Mon Sep 17 00:00:00 2001 From: Hector Castelli Zacharias Date: Fri, 5 Jan 2024 15:43:54 +0100 Subject: [PATCH] move commands to other modules --- src/commands.rs | 21 +++++++++++++++++ src/commands/assemble.rs | 25 ++++++++++++++++++++ src/commands/initialize.rs | 28 +++++++++++++++++++++++ src/main.rs | 47 ++++++++------------------------------ 4 files changed, 83 insertions(+), 38 deletions(-) create mode 100644 src/commands.rs create mode 100644 src/commands/assemble.rs create mode 100644 src/commands/initialize.rs diff --git a/src/commands.rs b/src/commands.rs new file mode 100644 index 0000000..c33ce57 --- /dev/null +++ b/src/commands.rs @@ -0,0 +1,21 @@ +use clap::Subcommand; + +mod assemble; +mod initialize; + +#[derive(Subcommand)] +pub(crate) enum Commands { + Assemble(assemble::Assemble), + Initialize(initialize::Initialize), +} + +trait CommandExecutor { + fn execute(&self); +} + +pub(crate) fn execute_command(command: Commands) { + match command { + Commands::Assemble(a) => a.execute(), + Commands::Initialize(a) => a.execute(), + } +} \ No newline at end of file diff --git a/src/commands/assemble.rs b/src/commands/assemble.rs new file mode 100644 index 0000000..c373897 --- /dev/null +++ b/src/commands/assemble.rs @@ -0,0 +1,25 @@ +use std::path::PathBuf; + +use clap::Args; +use log::{debug, trace}; + +use super::CommandExecutor; + +#[derive(Args)] +/// Assembles a powerd6 module from a directory +pub(crate) struct Assemble { + /// The path of the directory to be assembled + #[arg(short, long, default_value = "./")] + pub(crate) config: PathBuf, +} + +impl CommandExecutor for Assemble { + fn execute(&self) { + trace!("Executing assemble"); + debug!("Open config directory: {:?}", self.config); + debug!("Assemble module information"); + debug!("Assemble authors information"); + debug!("Assemble schema information"); + debug!("Assemble contents information"); + } +} diff --git a/src/commands/initialize.rs b/src/commands/initialize.rs new file mode 100644 index 0000000..877f3b4 --- /dev/null +++ b/src/commands/initialize.rs @@ -0,0 +1,28 @@ +use std::path::PathBuf; + +use clap::Args; +use log::{debug, trace}; + +use super::CommandExecutor; + +#[derive(Args)] +/// Initializes a directory for a powerd6 module +pub(crate) struct Initialize { + /// The path of the directory to be initialized + #[arg(short, long, default_value = "./")] + pub(crate) config: PathBuf, +} + +impl CommandExecutor for Initialize { + fn execute(&self) { + trace!("Executing initialize"); + debug!( + "Create config directory if not exists: {:?}", + self.config + ); + debug!("Create module.yaml"); + debug!("Create authors directory"); + debug!("Create schema directory"); + debug!("Create contents directory"); + } +} diff --git a/src/main.rs b/src/main.rs index 956e985..3042108 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,11 @@ -use std::path::PathBuf; - -use clap::{Parser, Subcommand, ValueEnum}; -use log::{LevelFilter, trace, error, debug}; +use clap::{Parser, ValueEnum}; +use log::{LevelFilter, trace}; use simplelog::{ColorChoice, CombinedLogger, Config, TermLogger, TerminalMode}; +use crate::commands::Commands; + +mod commands; + #[derive(Parser)] #[command(version, about)] struct Cli { @@ -11,7 +13,7 @@ struct Cli { #[arg(short, long, global=true, value_enum, default_value_t=LogLevel::Info)] log_level: LogLevel, - #[command(subcommand, )] + #[command(subcommand)] command: Commands, } @@ -38,21 +40,6 @@ impl Into for LogLevel { } } -#[derive(Subcommand)] -enum Commands { - /// Assembles a powerd6 module from a directory - Assemble { - /// The path of the directory to be assembled - #[arg(short, long, default_value = "./")] - config: PathBuf, - }, - /// Initializes a directory for a powerd6 module - Initialize { - /// The path of the directory to be initialized - #[arg(short, long, default_value = "./")] - config: PathBuf, - }, -} fn main() { // Parse input @@ -68,22 +55,6 @@ fn main() { .unwrap(); trace!("Loggers initialized!"); - match &cli.command { - Commands::Assemble { config } => { - trace!("Executing assemble"); - debug!("Open config directory: {:?}", config); - debug!("Assemble module information"); - debug!("Assemble authors information"); - debug!("Assemble schema information"); - debug!("Assemble contents information"); - }, - Commands::Initialize { config } => { - trace!("Executing initialize"); - debug!("Create config directory if not exists: {:?}", config); - debug!("Create module.yaml"); - debug!("Create authors directory"); - debug!("Create schema directory"); - debug!("Create contents directory"); - }, - } + trace!("Executing command"); + commands::execute_command(cli.command) }