aboutsummaryrefslogtreecommitdiff
path: root/src/cli.rs
blob: 009397d2fe3c30e66e73af766f8db31320693c5c (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
use anyhow::Result;
use lazy_static::lazy_static;

use std::default::Default;
use std::path::PathBuf;

pub struct Config {
    pub port: u16,
    pub db_path: PathBuf,
}

impl Default for Config {
    fn default() -> Self {
        Config {
            port: 3000,
            db_path: "./urls.db_3".into(),
        }
    }
}

lazy_static! {
    pub static ref CONFIG: Config = parse_args().unwrap_or(Default::default());
}

fn parse_args() -> Result<Config> {
    let mut _a = pico_args::Arguments::from_env();
    return Ok(Config {
        port: _a
            .opt_value_from_fn("--port", str::parse::<u16>)?
            .unwrap_or(7878),
        db_path: _a
            .opt_value_from_str("--database")?
            .unwrap_or(PathBuf::from("./urls.db_3")),
    });
}