diff options
| author | Teddy Wing | 2021-06-05 13:11:03 +0200 | 
|---|---|---|
| committer | Teddy Wing | 2021-06-05 13:11:03 +0200 | 
| commit | 2a05b269344ab324aa73a634256a0b82fa79f201 (patch) | |
| tree | f88049c86dc293a6f5423893b23183a569fa2012 | |
| parent | 3964a27da7cf61b540992387e8553382dffecb46 (diff) | |
| download | reflectub-2a05b269344ab324aa73a634256a0b82fa79f201.tar.bz2 | |
Add command line option parsing
Define the options we want to take. Not using them yet.
| -rw-r--r-- | Cargo.lock | 23 | ||||
| -rw-r--r-- | Cargo.toml | 2 | ||||
| -rw-r--r-- | src/main.rs | 44 | 
3 files changed, 69 insertions, 0 deletions
| @@ -228,6 +228,12 @@ dependencies = [  ]  [[package]] +name = "exitcode" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de853764b47027c2e862a995c34978ffa63c1501f2e15f987ba11bd4f9bba193" + +[[package]]  name = "filetime"  version = "0.2.14"  source = "registry+https://github.com/rust-lang/crates.io-index" @@ -381,6 +387,15 @@ dependencies = [  ]  [[package]] +name = "getopts" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14dbbfd5c71d70241ecf9e6f13737f7b5ce823821063188d7e46c41d371eebd5" +dependencies = [ + "unicode-width", +] + +[[package]]  name = "getrandom"  version = "0.2.3"  source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1024,7 +1039,9 @@ version = "0.0.1"  dependencies = [   "anyhow",   "chrono", + "exitcode",   "filetime", + "getopts",   "git2",   "reqwest",   "serde", @@ -1588,6 +1605,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"  checksum = "bb0d2e7be6ae3a5fa87eed5fb451aff96f2573d2694942e40543ae0bbe19c796"  [[package]] +name = "unicode-width" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" + +[[package]]  name = "unicode-xid"  version = "0.2.2"  source = "registry+https://github.com/rust-lang/crates.io-index" @@ -6,7 +6,9 @@ edition = "2018"  [dependencies]  anyhow = "1.0.40"  chrono = "0.4.19" +exitcode = "1.1.2"  filetime = "0.2.14" +getopts = "0.2.21"  git2 = "0.13.20"  reqwest = { version = "0.11.3", features = ["json"] }  serde = { version = "1.0.126", features = ["derive"] } diff --git a/src/main.rs b/src/main.rs index e20e860..f21f00a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,18 +1,55 @@  use anyhow::{self, Context};  use chrono::DateTime; +use exitcode;  use filetime; +use getopts::Options;  use sqlx;  use tokio;  use reflectub::{database, git, github}; +use std::env;  use std::fs;  use std::io;  use std::path::{Path, PathBuf}; +use std::process;  #[tokio::main]  async fn main() { +    let args: Vec<String> = env::args().collect(); + +    let mut opts = Options::new(); + +    opts.optopt("d", "database", "SQLite database file path (required)", "DATABASE_FILE"); +    opts.optopt("", "cgitrc", "base cgitrc file to copy to mirrored repositories", "CGITRC_FILE"); +    opts.optflag("h", "help", "print this help menu"); +    opts.optflag("V", "version", "show the program version"); + +    let opt_matches = match opts.parse(&args[1..]) { +        Ok(m) => m, +        Err(e) => { +            eprintln!("error: {}", e); + +            process::exit(exitcode::SOFTWARE); +        }, +    }; + +    if opt_matches.opt_present("h") { +        print_usage(&opts); +        process::exit(exitcode::USAGE); +    } + +    if opt_matches.opt_present("V") { +        println!("{}", env!("CARGO_PKG_VERSION")); +        process::exit(exitcode::OK); +    } + +    if opt_matches.free.len() != 2 { +        print_usage(&opts); +        process::exit(exitcode::USAGE); +    } +      // let repos = github::fetch_repos("teddywing").await.unwrap();      //      // dbg!(&repos); @@ -29,6 +66,13 @@ async fn main() {      run().await.unwrap();  } +fn print_usage(opts: &Options) { +    print!( +        "{}", +        opts.usage("usage: reflectub [options] -d DATABASE <github_username> <repository_path>"), +    ); +} +  async fn run() -> anyhow::Result<()> {      let test_repos = vec