diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..1d1a199 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,121 @@ +extern crate clap; +extern crate config; +extern crate db; +extern crate dirs; +extern crate front; +use clap::{App, Arg, SubCommand}; +use db::docker::ImageDrive; + +fn main() { + let matches = App::new("ImageDrive") + .version("0.1") + .author("rcmerci@gmail.com") + .arg( + Arg::with_name("config") + .short("c") + .long("config") + .value_name("FILE") + .help("Sets a custom config file") + .takes_value(true), + ) + .arg( + Arg::with_name("output") + .help("Sets an optional output file") + .index(1), + ) + .subcommand( + SubCommand::with_name("ls") + .about("list entries or items") + .arg( + Arg::with_name("entry") + .help("set it if list items") + .index(1), + ), + ) + .subcommand( + SubCommand::with_name("put") + .about("put host file to imagedrive") + .arg(Arg::with_name("entry").help("entry name").required(true)) + .arg(Arg::with_name("file").help("file path").required(true)), + ) + .subcommand( + SubCommand::with_name("export") + .about("export entry to host") + .arg(Arg::with_name("entry").help("entry name").required(true)) + .arg(Arg::with_name("dir").help("dst dir path").required(true)), + ) + .subcommand( + SubCommand::with_name("sync") + .about("sync localDB with remoteDB") + .arg( + Arg::with_name("from_remote") + .help("sync remoteDB to localDB") + .short("r") + .long("remote"), + ), + ) + .get_matches(); + + let mut config_path = None; + if matches.is_present("config") { + config_path = Some(std::path::Path::new(matches.value_of("config").unwrap()).to_path_buf()); + } else { + let home = dirs::home_dir().unwrap(); + config_path = Some(std::path::Path::new(&home).join(".imagedrive")); + } + let config_path = config_path.map(|p| { + if !p.exists() { + let errstr = format!("config file: '{:?}' not exists", p.display()); + panic!(errstr); + } + p + }); + + let cfg = config::get_config(config_path.unwrap()); + + let username = &cfg.username; + let server = &cfg.server; + let password = &cfg.password; + let image_name = &cfg.image_name; + + if let Some(matches) = matches.subcommand_matches("ls") { + // "$ myapp test" was run + if matches.is_present("entry") { + front::list_entry_item( + &ImageDrive::new(image_name, server, username, password), + matches.value_of("entry").unwrap(), + ); + } else { + front::list_entry(&ImageDrive::new(image_name, server, username, password)); + } + } + + if let Some(matches) = matches.subcommand_matches("put") { + let entry = matches.value_of("entry").unwrap(); + let filepath = matches.value_of("file").unwrap(); + front::put( + &ImageDrive::new(image_name, server, username, password), + entry, + filepath, + ); + } + + if let Some(matches) = matches.subcommand_matches("export") { + let entry = matches.value_of("entry").unwrap(); + let filepath = matches.value_of("dir").unwrap(); + front::export( + &ImageDrive::new(image_name, server, username, password), + entry, + filepath, + ); + } + + if let Some(matches) = matches.subcommand_matches("sync") { + front::sync( + &ImageDrive::new(image_name, server, username, password), + matches.is_present("from_remote"), + ); + } + + // Continued program logic goes here... +}