Skip to content

Commit

Permalink
support also --html, --htmx and --api flags to scaffold cli command
Browse files Browse the repository at this point in the history
  • Loading branch information
kaplanelad committed Sep 16, 2024
1 parent 7b0ab1d commit 5899f9b
Showing 1 changed file with 52 additions and 16 deletions.
68 changes: 52 additions & 16 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use crate::{
ServeParams, StartMode,
},
environment::{resolve_from_env, Environment, DEFAULT_ENVIRONMENT},
gen::{self, Component},
gen::{self, Component, ScaffoldKind},
logger, task,
};
#[derive(Parser)]
Expand Down Expand Up @@ -160,8 +160,20 @@ enum ComponentArg {
fields: Vec<(String, String)>,

/// The kind of scaffold to generate
#[clap(short, long, value_enum, default_value_t = gen::ScaffoldKind::Api)]
kind: gen::ScaffoldKind,
#[clap(short, long, value_enum, group = "scaffold_kind_group")]
kind: Option<gen::ScaffoldKind>,

/// Use HTMX scaffold
#[clap(long, group = "scaffold_kind_group")]
htmx: bool,

/// Use HTML scaffold
#[clap(long, group = "scaffold_kind_group")]
html: bool,

/// Use API scaffold
#[clap(long, group = "scaffold_kind_group")]
api: bool,
},
/// Generate a new controller with the given controller name, and test file.
Controller {
Expand Down Expand Up @@ -189,31 +201,55 @@ enum ComponentArg {
Deployment {},
}

impl From<ComponentArg> for Component {
fn from(value: ComponentArg) -> Self {
impl TryFrom<ComponentArg> for Component {
type Error = crate::Error;
fn try_from(value: ComponentArg) -> Result<Self, Self::Error> {
match value {
#[cfg(feature = "with-db")]
ComponentArg::Model {
name,
link,
migration_only,
fields,
} => Self::Model {
} => Ok(Self::Model {
name,
link,
migration_only,
fields,
},
}),
#[cfg(feature = "with-db")]
ComponentArg::Migration { name } => Self::Migration { name },
ComponentArg::Migration { name } => Ok(Self::Migration { name }),
#[cfg(feature = "with-db")]
ComponentArg::Scaffold { name, fields, kind } => Self::Scaffold { name, fields, kind },
ComponentArg::Controller { name } => Self::Controller { name },
ComponentArg::Task { name } => Self::Task { name },
ComponentArg::Scheduler {} => Self::Scheduler {},
ComponentArg::Worker { name } => Self::Worker { name },
ComponentArg::Mailer { name } => Self::Mailer { name },
ComponentArg::Deployment {} => Self::Deployment {},
ComponentArg::Scaffold {
name,
fields,
kind,
htmx,
html,
api,
} => {
let kind = if let Some(kind) = kind {
kind
} else if htmx {
ScaffoldKind::Htmx
} else if html {
ScaffoldKind::Html
} else if api {
ScaffoldKind::Api
} else {
return Err(crate::Error::string(
"Error: One of `kind`, `htmx`, `html`, or `api` must be specified.",
));
};

Ok(Self::Scaffold { name, fields, kind })
}
ComponentArg::Controller { name } => Ok(Self::Controller { name }),
ComponentArg::Task { name } => Ok(Self::Task { name }),
ComponentArg::Scheduler {} => Ok(Self::Scheduler {}),
ComponentArg::Worker { name } => Ok(Self::Worker { name }),
ComponentArg::Mailer { name } => Ok(Self::Mailer { name }),
ComponentArg::Deployment {} => Ok(Self::Deployment {}),
}
}
}
Expand Down Expand Up @@ -375,7 +411,7 @@ pub async fn main<H: Hooks, M: MigratorTrait>() -> crate::Result<()> {
run_scheduler::<H>(&app_context, config.as_ref(), name, tag, list).await?;
}
Commands::Generate { component } => {
gen::generate::<H>(component.into(), &config)?;
gen::generate::<H>(component.try_into()?, &config)?;
}
Commands::Doctor {} => {
let mut should_exit = false;
Expand Down

0 comments on commit 5899f9b

Please sign in to comment.