soph_console/support/
service.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use crate::{
    async_trait,
    support::{Command, STYLES},
    Console,
};
use soph_core::{
    traits::{ApplicationTrait, ServiceTrait},
    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(())
    }
}