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
use std::path::Path;

use clap::Parser;

pub enum RuntimeBehavior {
    PrintConfigPath,
    PrintDataPath,
    DumpDefaultConfig,
    Run,
}

#[derive(Parser)]
pub struct Cli {
    /// prints the directory in which the config file is being loaded from
    #[arg(long)]
    config_dir: bool,
    /// dumps the default configuration to stdout.
    #[arg(long)]
    config_dump: bool,
    /// prints the directory in which the collections are being stored
    #[arg(long)]
    data_dir: bool,
}

impl Cli {
    pub fn parse_args() -> RuntimeBehavior {
        let args = Cli::parse();

        if args.config_dir {
            return RuntimeBehavior::PrintConfigPath;
        }
        if args.data_dir {
            return RuntimeBehavior::PrintDataPath;
        }
        if args.config_dump {
            return RuntimeBehavior::DumpDefaultConfig;
        }

        RuntimeBehavior::Run
    }

    pub fn print_data_path<P>(data_path: P)
    where
        P: AsRef<Path>,
    {
        println!(
            "collections are being stored at: {}",
            data_path.as_ref().to_string_lossy()
        );
        println!("you can change this on the configuration file by specifying `collections_dir`");
    }

    pub fn print_config_path<P>(maybe_path: Option<P>, usual_path: P)
    where
        P: AsRef<Path>,
    {
        match maybe_path {
            Some(config_dir) => {
                println!(
                    "config is being loaded from: {}",
                    config_dir.as_ref().to_string_lossy()
                );
            }
            None => {
                println!("no config file was found, the default one is being used");
                println!("the usual path for the configuration file is at:\n");
                println!("{}", usual_path.as_ref().to_string_lossy());
            }
        }
    }

    pub fn print_default_config(config_as_str: &str) {
        println!("{}", config_as_str)
    }
}