aboutsummaryrefslogtreecommitdiffstats
path: root/src/config/cli/mod.rs
blob: 81eb2fe295d8ce7f8bb343e17aad3d436f862673 (plain)
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
pub mod cache;

#[cfg(feature = "oauth")]
pub mod oauth;

use std::path::PathBuf;

use clap::Parser;
use url::Url;

use crate::config::{logging::LogLevel, port::port_in_range};

#[derive(Parser, Debug)]
#[command(version, about, long_about = None, name = env!("CARGO_PKG_NAME"))]
pub struct Cli {
    /// Sets the port the server listens on
    #[arg(short, long, default_value = "2210", env = "PORT", value_parser = port_in_range)]
    pub port: Option<u16>,

    /// Server's domain
    #[arg(short, long, default_value = "localhost", env = "DOMAIN")]
    pub domain: Option<String>,

    /// Sets a custom config file
    #[arg(short, long, value_name = "FILE")]
    pub config: Option<PathBuf>,

    /// Sets the application log level
    #[arg(short, long, value_enum, env = "LOG_LEVEL", default_value = "debug")]
    pub log_level: Option<LogLevel>,

    /// Request timeout duration (in seconds)
    #[arg(short, long, env = "TIMEOUT_SECONDS", default_value = "10")]
    pub timeout_duration: Option<u64>,

    /// Database connection string
    #[arg(
        long,
        env = "DATABASE_URL",
        default_value = "postgres://postgres:password@localhost:5432/sellershut"
    )]
    pub db: Option<Url>,

    #[command(flatten)]
    pub cache: Option<cache::Cache>,

    /// Server's system name
    #[arg(short, long, default_value = "sellershut", env = "SYSTEM_NAME")]
    pub system_name: Option<String>,

    /// Server's system name
    #[arg(short, long, default_value = "prod", env = "SYSTEM_NAME")]
    pub environment: Option<String>,

    /// Oauth optionas
    #[command(flatten)]
    #[cfg(feature = "oauth")]
    pub oauth: oauth::OAuth,
}

#[cfg(test)]
impl Default for Cli {
    fn default() -> Self {
        let url = Url::parse("postgres://postgres:password@localhost:5432/sellershut").ok();
        Self {
            port: Default::default(),
            config: Default::default(),
            log_level: Default::default(),
            timeout_duration: Some(10),
            domain: Default::default(),
            system_name: Default::default(),
            environment: Default::default(),
            oauth: Default::default(),
            cache: Default::default(),
            db: url,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn verify_cli() {
        use clap::CommandFactory;
        Cli::command().debug_assert();
    }
}