soph_console/support/
service.rsuse crate::{
async_trait,
support::{Command, STYLES},
traits::{ApplicationTrait, ServiceTrait},
Console, Result,
};
const LOGO: &str = r"
░░ ░░░░ ░░░ ░░░ ░░░░ ░
▒ ▒▒▒▒▒▒▒▒ ▒▒▒▒ ▒▒ ▒▒▒▒ ▒▒ ▒▒▒▒ ▒
▓▓ ▓▓▓ ▓▓▓▓ ▓▓ ▓▓▓ ▓
███████ ██ ████ ██ ████████ ████ █
██ ████ ███ ████████ ████ █
";
#[async_trait]
impl ServiceTrait for Console {
type Item = Command;
fn new() -> Self {
let command = clap::Command::default()
.styles(STYLES)
.name("soph")
.version(env!("CARGO_PKG_VERSION"))
.about(format!("\n {} \n {}", LOGO, env!("CARGO_PKG_DESCRIPTION")));
Self {
inner: command,
..Default::default()
}
}
fn register(mut self, command: Self::Item) -> Self {
self.commands
.insert(command.name().to_string(), command.command().clone());
self.closures
.insert(command.name().to_string(), command.handler().clone());
self
}
fn init<A: ApplicationTrait>() -> Self {
let mut console = (&A::with_console() as &dyn std::any::Any)
.downcast_ref::<Self>()
.cloned()
.unwrap_or_default();
console = console.register(Command::new::<A, crate::support::commands::Publish>());
#[cfg(feature = "command-make")]
{
console = console.register(Command::new::<A, crate::support::commands::Make>());
}
#[cfg(feature = "command-migrate")]
{
console = console.register(Command::new::<A, crate::support::commands::Migrate>());
}
#[cfg(feature = "command-new")]
{
console = console.register(Command::new::<A, crate::support::commands::New>());
}
#[cfg(feature = "command-queue")]
{
console = console.register(Command::new::<A, crate::support::commands::Queue>());
}
#[cfg(feature = "command-server")]
{
console = console.register(Command::new::<A, crate::support::commands::Server>());
}
#[cfg(feature = "command-schedule")]
{
console = console.register(Command::new::<A, crate::support::commands::Schedule>());
}
#[cfg(any(
feature = "cache",
feature = "database",
feature = "mail",
feature = "redis",
feature = "storage",
feature = "tracing",
))]
{
console = console.register(Command::new::<A, crate::support::commands::Status>());
}
console
}
async fn run(self) -> Result<()> {
let commands = self.inner.subcommands(self.commands.values());
match commands.clone().get_matches().subcommand() {
Some((name, arg_matches)) => {
if let Some(closure) = self.closures.get(name) {
if let Some(err) = closure(arg_matches.to_owned()).await.err() {
tracing::error!("[command] `{}` handle error: {}", name, err);
}
}
}
None => commands.clone().print_help()?,
}
Ok(())
}
}