aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/geniza-register.rs
blob: 6962be14c40d4f405331360a74258168c0e37d63 (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
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

#[macro_use]
extern crate clap;

extern crate geniza;

// TODO: more careful import
use geniza::*;
use std::path::Path;

use clap::{App, SubCommand};

fn run() -> Result<()> {
 
    let matches = App::new("geniza-register")
        .version(env!("CARGO_PKG_VERSION"))
        .subcommand(SubCommand::with_name("info")
            .about("Reads a SLEEP dir register and shows some basic metadata")
            .arg_from_usage("<DIR> 'directory containing files'")
            .arg_from_usage("<prefix> 'prefix for each data file'"))
        .subcommand(SubCommand::with_name("create")
            .about("Creates an SLEEP directory register (with header)")
            .arg_from_usage("<DIR> 'directory containing files'")
            .arg_from_usage("<prefix> 'prefix for each data file'"))
        .get_matches();


    match matches.subcommand() {
        ("info", Some(subm)) => {
            let dir = Path::new(subm.value_of("DIR").unwrap());
            let prefix = subm.value_of("prefix").unwrap();
            let sdr = SleepDirRegister::open(dir, prefix, false)?;
            //debug!(println!("{:?}", sdr));
            println!("Entry count: {}", sdr.len()?);
        },
        ("create", Some(subm)) => {
            let dir = Path::new(subm.value_of("DIR").unwrap());
            let prefix = subm.value_of("prefix").unwrap();
            SleepDirRegister::create(dir, prefix)?;
            println!("Done!");
        },
        _ => {
            println!("Missing or unimplemented command!");
            println!("{}", matches.usage());
            ::std::process::exit(-1);
        },
    }
    Ok(())
}

// TODO: is there a shorter error_chain 'main()' to use here?
fn main() {
    if let Err(ref e) = run() {
        println!("error: {}", e);

        for e in e.iter().skip(1) {
            println!("caused by: {}", e);
        }

        // The backtrace is not always generated. Try to run this example
        // with `RUST_BACKTRACE=1`.
        if let Some(backtrace) = e.backtrace() {
            println!("backtrace: {:?}", backtrace);
        }

        ::std::process::exit(1);
    }
}