cot 0.1.4

The Rust web framework for lazy developers.
Documentation
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
//! A command line interface for Cot-based applications.

use std::collections::HashMap;
use std::path::PathBuf;
use std::str::FromStr;

use async_trait::async_trait;
pub use clap;
use clap::{value_parser, Arg, ArgMatches, Command};
use derive_more::Debug;

use crate::error::ErrorRepr;
use crate::{Bootstrapper, Error, Result};

const CONFIG_PARAM: &str = "config";
const COLLECT_STATIC_SUBCOMMAND: &str = "collect-static";
const CHECK_SUBCOMMAND: &str = "check";
const LISTEN_PARAM: &str = "listen";
const COLLECT_STATIC_DIR_PARAM: &str = "dir";

#[derive(Debug)]
pub struct Cli {
    command: Command,
    #[debug("..")]
    tasks: HashMap<Option<String>, Box<dyn CliTask + Send + 'static>>,
}

impl Cli {
    #[must_use]
    pub(crate) fn new() -> Self {
        let default_task = Self::default_task();
        let command = default_task.subcommand();

        let command = command.arg(
            Arg::new(CONFIG_PARAM)
                .short('c')
                .long("config")
                .value_name("FILE")
                .default_value("dev")
                .help("Sets a custom config file"),
        );

        let mut tasks: HashMap<Option<String>, Box<dyn CliTask + Send + 'static>> = HashMap::new();
        tasks.insert(None, Box::new(default_task));

        let mut cli = Self { command, tasks };
        cli.add_task(Check);
        cli.add_task(CollectStatic);

        cli
    }

    pub(crate) fn set_metadata(&mut self, metadata: CliMetadata) {
        let mut command = std::mem::take(&mut self.command);
        command = command.name(metadata.name).version(metadata.version);

        if !metadata.authors.is_empty() {
            command = command.author(metadata.authors);
        }

        if !metadata.description.is_empty() {
            command = command.about(metadata.description);
        }

        self.command = command;
    }

    #[must_use]
    fn default_task() -> impl CliTask {
        RunServer
    }

    pub fn add_task<C>(&mut self, task: C)
    where
        C: CliTask + Send + 'static,
    {
        let subcommand = task.subcommand();
        let name = subcommand.get_name();

        assert!(
            !self.tasks.contains_key(&Some(name.to_owned())),
            "Task with name {name} already exists"
        );

        let name = name.to_owned();
        self.command = std::mem::take(&mut self.command).subcommand(subcommand);
        self.tasks.insert(Some(name), Box::new(task));
    }

    #[must_use]
    pub fn get_common_options(&mut self) -> CommonOptions {
        let matches = self.command.get_matches_mut();
        CommonOptions::new(matches)
    }

    #[allow(clippy::future_not_send)] // Send not needed; CLI is run async in a single thread
    pub(crate) async fn execute(mut self, bootstrapper: Bootstrapper<WithConfig>) -> Result<()> {
        let matches = self.command.get_matches();

        let subcommand_name = matches.subcommand_name();
        let task = self.tasks.get_mut(&subcommand_name.map(ToOwned::to_owned));

        let matches = match subcommand_name {
            Some(name) => matches.subcommand_matches(name).unwrap(),
            None => &matches,
        };

        task.expect("subcommand should exist if get_matches() didn't fail")
            .execute(matches, bootstrapper)
            .await
    }
}

impl Default for Cli {
    fn default() -> Self {
        Self::new()
    }
}

/// Metadata for the CLI application.
///
/// This struct is used to set the name, version, authors, and description of
/// the CLI application. This is meant to be typically used in
/// [`crate::CotProjectBuilder::with_cli`] and [`metadata!`].
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct CliMetadata {
    /// The name that will be shown in the help message.
    pub name: &'static str,
    /// The version that will be shown when the `--version` flag is used.
    pub version: &'static str,
    /// The authors of the CLI application.
    pub authors: &'static str,
    /// The description that will be shown in the help message.
    pub description: &'static str,
}

/// A trait for defining a CLI command.
///
/// This is meant to be used with [`crate::CotProjectBuilder::add_task`].
#[async_trait(?Send)]
pub trait CliTask {
    /// Returns the definition of the task's options as the [`clap`] crate's
    /// [`Command`].
    fn subcommand(&self) -> Command;

    /// Executes the task with the given matches and project.
    async fn execute(
        &mut self,
        matches: &ArgMatches,
        bootstrapper: Bootstrapper<WithConfig>,
    ) -> Result<()>;
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CommonOptions {
    matches: ArgMatches,
}

impl CommonOptions {
    #[must_use]
    fn new(matches: ArgMatches) -> Self {
        Self { matches }
    }

    #[must_use]
    pub fn config(&self) -> &str {
        self.matches
            .get_one::<String>("config")
            .expect("default provided")
    }
}

struct RunServer;

#[async_trait(?Send)]
impl CliTask for RunServer {
    fn subcommand(&self) -> Command {
        Command::default().arg(
            Arg::new(LISTEN_PARAM)
                .help("Optional port to listen on, or address:port")
                .short('l')
                .long("listen")
                .default_value("127.0.0.1:8000")
                .value_name("ADDRPORT")
                .required(false),
        )
    }

    async fn execute(
        &mut self,
        matches: &ArgMatches,
        bootstrapper: Bootstrapper<WithConfig>,
    ) -> Result<()> {
        let addr_port = matches
            .get_one::<String>(LISTEN_PARAM)
            .expect("default provided");

        let addr_port = if let Ok(port) = u16::from_str(addr_port) {
            format!("127.0.0.1:{port}")
        } else {
            addr_port.to_owned()
        };

        let bootstrapper = bootstrapper.boot().await?;
        crate::run(bootstrapper, &addr_port).await
    }
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
struct CollectStatic;

#[async_trait(?Send)]
impl CliTask for CollectStatic {
    fn subcommand(&self) -> Command {
        Command::new(COLLECT_STATIC_SUBCOMMAND)
            .about("Collects all static files into a static directory")
            .arg(
                Arg::new(COLLECT_STATIC_DIR_PARAM)
                    .help("The directory to collect the static files into")
                    .value_parser(value_parser!(PathBuf))
                    .required(true),
            )
    }

    async fn execute(
        &mut self,
        matches: &ArgMatches,
        bootstrapper: Bootstrapper<WithConfig>,
    ) -> Result<()> {
        let dir = matches
            .get_one::<PathBuf>(COLLECT_STATIC_DIR_PARAM)
            .expect("required argument");
        println!("Collecting static files into {:?}", dir);

        let bootstrapper = bootstrapper.with_apps();
        StaticFiles::from(bootstrapper.context())
            .collect_into(dir)
            .map_err(|e| Error::new(ErrorRepr::CollectStatic { source: e }))?;

        Ok(())
    }
}

struct Check;
#[async_trait(?Send)]
impl CliTask for Check {
    fn subcommand(&self) -> Command {
        Command::new(CHECK_SUBCOMMAND).about(
            "Verifies the configuration, including connections to the database and other services",
        )
    }

    async fn execute(
        &mut self,
        _matches: &ArgMatches,
        bootstrapper: Bootstrapper<WithConfig>,
    ) -> Result<()> {
        bootstrapper.boot().await?;
        println!("Success verifying the configuration");
        Ok(())
    }
}

/// A macro to generate a [`CliMetadata`] struct from the Cargo manifest.
#[macro_export]
macro_rules! metadata {
    () => {{
        $crate::cli::CliMetadata {
            name: env!("CARGO_PKG_NAME"),
            version: env!("CARGO_PKG_VERSION"),
            authors: env!("CARGO_PKG_AUTHORS"),
            description: env!("CARGO_PKG_DESCRIPTION"),
        }
    }};
}

pub use metadata;

use crate::project::WithConfig;
use crate::static_files::StaticFiles;

#[cfg(test)]
mod tests {
    use bytes::Bytes;
    use clap::Command;
    use cot::test::serial_guard;
    use tempfile::tempdir;
    use thiserror::__private::AsDisplay;

    use super::*;
    use crate::config::ProjectConfig;
    use crate::{App, AppBuilder, ProjectContext};

    #[test]
    fn cli_new() {
        let cli = Cli::new();
        assert!(cli.command.get_name().is_empty());
        assert!(cli.tasks.contains_key(&None));
    }

    #[test]
    fn cli_set_metadata() {
        let mut cli = Cli::new();
        let metadata = CliMetadata {
            name: "test_app",
            version: "1.0",
            authors: "Author",
            description: "Test application",
        };
        cli.set_metadata(metadata);

        assert_eq!(cli.command.get_name(), "test_app");
        assert_eq!(cli.command.get_version().unwrap(), "1.0");
        assert_eq!(cli.command.get_author().unwrap(), "Author");
        assert_eq!(
            cli.command.get_about().unwrap().as_display().to_string(),
            "Test application"
        );
    }

    #[test]
    fn cli_add_task() {
        struct MyTask;

        #[async_trait(?Send)]
        impl CliTask for MyTask {
            fn subcommand(&self) -> Command {
                Command::new("my-task")
            }

            async fn execute(
                &mut self,
                _matches: &ArgMatches,
                _bootstrapper: Bootstrapper<WithConfig>,
            ) -> Result<()> {
                Ok(())
            }
        }

        let mut cli = Cli::new();
        cli.add_task(MyTask);

        assert!(cli.tasks.contains_key(&Some("my-task".to_owned())));
        assert!(cli
            .command
            .get_subcommands()
            .any(|sc| sc.get_name() == "my-task"));
    }

    #[test]
    fn run_server_subcommand() {
        let matches = RunServer
            .subcommand()
            .try_get_matches_from(vec!["test", "-l", "1024"]);

        assert!(matches.is_ok());
    }

    #[cot::test]
    #[cfg_attr(miri, ignore)] // unsupported operation: can't call foreign function `sqlite3_open_v2`
    async fn collect_static_execute() {
        let mut collect_static = CollectStatic;
        let temp_dir = tempdir().unwrap();
        let temp_path = temp_dir.path().join("static").clone();

        struct TestApp;
        impl App for TestApp {
            fn name(&self) -> &'static str {
                "test_app"
            }

            fn static_files(&self) -> Vec<(String, Bytes)> {
                vec![("test.txt".to_owned(), Bytes::from_static(b"test"))]
            }
        }

        let matches = CollectStatic
            .subcommand()
            .get_matches_from(vec!["test", temp_path.to_str().unwrap()]);

        struct TestProject;
        impl cot::Project for TestProject {
            fn register_apps(&self, apps: &mut AppBuilder, _context: &ProjectContext<WithConfig>) {
                apps.register(TestApp);
            }
        }

        let bootstrapper = Bootstrapper::new(TestProject).with_config(ProjectConfig::default());
        let result = collect_static.execute(&matches, bootstrapper).await;

        assert!(result.is_ok());
        assert!(temp_path.join("test.txt").exists());
    }

    #[cot::test]
    async fn check_execute() {
        let config = r#"secret_key = "123abc""#;
        let result = test_check(config).await;

        assert!(result.is_ok(), "{result:?}");
    }

    #[cot::test]
    #[cfg_attr(miri, ignore)] // unsupported operation: can't call foreign function `geteuid` on OS `linux`
    #[cfg(feature = "db")]
    async fn check_execute_db_fail() {
        let config = r#"
        [database]
        url = "postgresql://invalid:invalid@invalid/invalid"
        "#;
        let result = test_check(config).await;

        assert!(result.is_err());
    }

    async fn test_check(config: &str) -> Result<()> {
        let temp_dir = tempdir().unwrap();
        let config_path = temp_dir.path().join("config").clone();
        std::fs::create_dir_all(&config_path).unwrap();
        std::fs::write(config_path.join("test.toml"), config).unwrap();

        let mut check = Check;
        let matches = Check.subcommand().get_matches_from(Vec::<&str>::new());

        struct TestProject;
        impl cot::Project for TestProject {}

        // ensure the tests run sequentially when setting the current directory
        let _guard = serial_guard();

        std::env::set_current_dir(&temp_dir).unwrap();
        let bootstrapper = Bootstrapper::new(TestProject).with_config_name("test")?;
        check.execute(&matches, bootstrapper).await
    }
}