~vpzom/lotide

89f94cd649f3f729089958c0fd7b1235012ebb45 — Colin Reeder 25 days ago 8eb81ff
Switch to using clap for argument parsing
5 files changed, 81 insertions(+), 36 deletions(-)

M Cargo.lock
M Cargo.toml
M src/config.rs
M src/main.rs
M src/migrate.rs
M Cargo.lock => Cargo.lock +46 -0
@@ 74,6 74,15 @@ dependencies = [
]

[[package]]
name = "ansi_term"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
dependencies = [
 "winapi 0.3.9",
]

[[package]]
name = "arc-swap"
version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"


@@ 285,6 294,21 @@ dependencies = [
]

[[package]]
name = "clap"
version = "2.33.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002"
dependencies = [
 "ansi_term",
 "atty",
 "bitflags",
 "strsim",
 "textwrap",
 "unicode-width",
 "vec_map",
]

[[package]]
name = "cloudabi"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"


@@ 1159,6 1183,7 @@ dependencies = [
 "bumpalo",
 "bytes",
 "chrono",
 "clap",
 "config 0.11.0",
 "deadpool-postgres",
 "either",


@@ 2163,6 2188,12 @@ dependencies = [
]

[[package]]
name = "strsim"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"

[[package]]
name = "subtle"
version = "2.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"


@@ 2214,6 2245,15 @@ dependencies = [
]

[[package]]
name = "textwrap"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
dependencies = [
 "unicode-width",
]

[[package]]
name = "thiserror"
version = "1.0.20"
source = "registry+https://github.com/rust-lang/crates.io-index"


@@ 2555,6 2595,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6454029bf181f092ad1b853286f23e2c507d8e8194d01d92da4a55c274a5508c"

[[package]]
name = "vec_map"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"

[[package]]
name = "version_check"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"

M Cargo.toml => Cargo.toml +1 -0
@@ 56,6 56,7 @@ log = "0.4"
env_logger = "0.8"
v_htmlescape = "0.12.0"
config = { version = "0.11.0", default-features = false, features = ["ini"] }
clap = "2.33.3"

[dev-dependencies]
rand = "0.7.3"

M src/config.rs => src/config.rs +5 -9
@@ 26,19 26,15 @@ pub struct Config {
}

impl Config {
    pub fn load() -> Result<Self, config::ConfigError> {
    pub fn load(config_file_path: Option<&std::ffi::OsStr>) -> Result<Self, config::ConfigError> {
        let mut src = config::Config::new()
            .with_merged(config::Environment::new())?
            .with_merged(config::Environment::with_prefix("LOTIDE"))?;

        {
            let mut args = std::env::args();
            while let Some(arg) = args.next() {
                if arg == "-c" {
                    let path = args.next().expect("Missing parameter for config argument");
                    src.merge(SpecificFile { path: path.into() })?;
                }
            }
        if let Some(config_file_path) = config_file_path {
            src.merge(SpecificFile {
                path: config_file_path.into(),
            })?;
        }

        src.try_into()

M src/main.rs => src/main.rs +27 -11
@@ 916,21 916,37 @@ pub fn on_post_add_comment(comment: CommentInfo<'static>, ctx: Arc<crate::RouteC

fn main() -> Result<(), Box<dyn std::error::Error>> {
    env_logger::init();
    let mut args = std::env::args();
    args.next(); // discard first element
    match args.next().as_deref() {
        Some("migrate") => {
            crate::migrate::run(args);
            Ok(())
        }
        _ => run(),

    let matches = clap::App::new("lotide")
        .arg(
            clap::Arg::with_name("config")
                .short("c")
                .value_name("FILE")
                .help("Sets a path to a config file")
                .takes_value(true),
        )
        .subcommand(
            clap::SubCommand::with_name("migrate").arg(
                clap::Arg::with_name("ACTION")
                    .required(true)
                    .default_value("up")
                    .possible_values(&["up", "down", "setup"]),
            ),
        )
        .get_matches();

    let config = Config::load(matches.value_of_os("config")).expect("Failed to load config");

    if let Some(matches) = matches.subcommand_matches("migrate") {
        crate::migrate::run(config, matches);
        Ok(())
    } else {
        run(config)
    }
}

#[tokio::main]
async fn run() -> Result<(), Box<dyn std::error::Error>> {
    let config = Config::load().expect("Failed to load config");

async fn run(config: Config) -> Result<(), Box<dyn std::error::Error>> {
    let db_pool = deadpool_postgres::Pool::new(
        deadpool_postgres::Manager::new(
            config.database_url.parse().unwrap(),

M src/migrate.rs => src/migrate.rs +2 -16
@@ 6,22 6,8 @@ pub struct StaticMigration {
    down: &'static str,
}

pub fn run(mut args: std::env::Args) {
    let config = crate::config::Config::load().expect("Failed to load config");

    let mut action = None;
    while let Some(arg) = args.next() {
        if arg == "-c" {
            args.next(); // skip parameter
        } else {
            if action == None {
                action = Some(arg);
            } else {
                panic!("Unexpected parameter");
            }
        }
    }
    let action = action.as_deref().unwrap_or("up");
pub fn run(config: crate::Config, matches: &clap::ArgMatches) {
    let action = matches.value_of("ACTION").unwrap_or("up");

    let db_cfg: tokio_postgres::Config = config
        .database_url