use clap::load_yaml; use clap::App; use std::fs::File; use std::io::Read; use std::net::SocketAddr; use std::path::Path; use std::str::FromStr; use torment_core::metainfo::Torrent; pub mod download; fn main() { let yml = load_yaml!("../cli.yml"); let matches = App::from_yaml(yml).get_matches(); match matches.subcommand() { ("torrent", Some(subcmd)) => { let file = subcmd.value_of("file").unwrap(); let mut bytes: Vec = vec![]; File::open(file).unwrap().read_to_end(&mut bytes).unwrap(); match Torrent::from_bytes( &bytes, Path::new(file) .file_stem() .and_then(|file_name| file_name.to_str()) .map(|str| str.to_string()), ) { Ok(torrent) => { println!("{:#?}", torrent); } Err(err) => println!("Error: {}", err), } } ("download", Some(subcmd)) => { let file = subcmd.value_of("file").unwrap(); let peers = subcmd .values_of("peer") .map(|values| { values .into_iter() .filter_map(|res| match SocketAddr::from_str(res) { Ok(addr) => Some(addr), Err(err) => { println!("Failed to parse {}: {}", res, err); None } }) .collect() }) .unwrap_or(vec![]); let mut bytes: Vec = vec![]; File::open(file).unwrap().read_to_end(&mut bytes).unwrap(); match Torrent::from_bytes( &bytes, Path::new(file) .file_stem() .and_then(|file_name| file_name.to_str()) .map(|str| str.to_string()), ) { Ok(torrent) => download::download(torrent, peers), Err(err) => println!("Error: {}", err), } } _ => {} } }