ping_viewer_next/cli/
manager.rs

1use clap;
2use clap::Parser;
3use lazy_static::lazy_static;
4use std::sync::Arc;
5
6#[derive(Parser, Debug)]
7#[command(version = env!("CARGO_PKG_VERSION"), author = env!("CARGO_PKG_AUTHORS"), about = env!("CARGO_PKG_DESCRIPTION"))]
8struct Args {
9    /// Call AutoCreate on DeviceManager during application startup.
10    #[arg(long, default_value = "false")]
11    enable_auto_create: bool,
12
13    /// Deletes settings file before starting.
14    #[arg(long)]
15    reset: bool,
16
17    /// Sets the address for the REST API server
18    #[arg(long, value_name = "IP>:<PORT", default_value = "0.0.0.0:8080")]
19    rest_server: String,
20
21    /// Turns all log categories up to Debug, for more information check RUST_LOG env variable.
22    #[arg(short, long)]
23    verbose: bool,
24
25    /// Specifies the path in witch the logs will be stored.
26    #[arg(long, default_value = "./logs")]
27    log_path: Option<String>,
28
29    /// Turns all log categories up to Trace to the log file, for more information check RUST_LOG env variable.
30    #[arg(long)]
31    enable_tracing_level_log_file: bool,
32
33    /// Filter to show only own crate related logs
34    #[arg(long, default_value = "false")]
35    log_include_all_dependencies: bool,
36
37    /// Turns on the Tracy tool integration.
38    #[arg(long)]
39    enable_tracy: bool,
40}
41
42#[derive(Debug)]
43struct Manager {
44    clap_matches: Args,
45}
46
47lazy_static! {
48    static ref MANAGER: Arc<Manager> = Arc::new(Manager::new());
49}
50
51impl Manager {
52    fn new() -> Self {
53        Self {
54            clap_matches: Args::parse(),
55        }
56    }
57}
58
59// Construct our manager, should be done inside main
60pub fn init() {
61    MANAGER.as_ref();
62}
63
64// Check if the verbosity parameter was used
65pub fn is_verbose() -> bool {
66    MANAGER.clap_matches.verbose
67}
68
69pub fn is_tracing() -> bool {
70    MANAGER.clap_matches.enable_tracing_level_log_file
71}
72
73pub fn is_tracy() -> bool {
74    MANAGER.clap_matches.enable_tracy
75}
76
77pub fn is_log_all_dependencies() -> bool {
78    MANAGER.clap_matches.log_include_all_dependencies
79}
80
81pub fn is_enable_auto_create() -> bool {
82    MANAGER.clap_matches.enable_auto_create
83}
84
85pub fn log_path() -> String {
86    let log_path =
87        MANAGER.clap_matches.log_path.clone().expect(
88            "Clap arg \"log-path\" should always be \"Some(_)\" because of the default value.",
89        );
90
91    shellexpand::full(&log_path)
92        .expect("Failed to expand path")
93        .to_string()
94}
95
96// Return the desired address for the REST API
97pub fn server_address() -> String {
98    MANAGER.clap_matches.rest_server.clone()
99}
100
101// Return the command line used to start this application
102pub fn command_line_string() -> String {
103    std::env::args().collect::<Vec<String>>().join(" ")
104}
105
106// Return a clone of current Args struct
107pub fn command_line() -> String {
108    format!("{:#?}", MANAGER.clap_matches)
109}
110
111#[cfg(test)]
112mod tests {
113    use super::*;
114
115    #[test]
116    fn default_arguments() {
117        assert!(!is_verbose());
118    }
119}