Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. extern crate libbig;
  2. use libbig::{BigArchive, ReadError};
  3.  
  4. extern crate clap;
  5. use clap::{Arg, App, AppSettings, SubCommand};
  6.  
  7. fn main() {
  8. let matches = App::new("sagebig")
  9. .version("0.1.0")
  10. .author("Taryn Hill <taryn@phrohdoh.com>")
  11. .about("CLI for libbig")
  12. .setting(AppSettings::SubcommandRequiredElseHelp)
  13. .subcommand(SubCommand::with_name("list")
  14. .about("List all entries in an archive")
  15. .version("0.1.0")
  16. .author("Taryn Hill <taryn@phrohdoh.com>")
  17. .arg(Arg::with_name("archive_path")
  18. .value_name("archive_path")
  19. .required(true)
  20. .index(1))
  21. .arg(Arg::with_name("internal_dir")
  22. .value_name("internal_dir")
  23. .index(2)))
  24. .subcommand(SubCommand::with_name("search")
  25. .about("Locate entries with names containing a string")
  26. .version("0.1.0")
  27. .author("Taryn Hill <taryn@phrohdoh.com>")
  28. .arg(Arg::with_name("archive_path")
  29. .value_name("archive_path")
  30. .required(true)
  31. .index(1))
  32. .arg(Arg::with_name("query")
  33. .value_name("query")
  34. .required(true)
  35. .index(2)))
  36. .subcommand(SubCommand::with_name("contains")
  37. .about("Query an archive to determine if it contains an entry with a name")
  38. .version("0.1.0")
  39. .author("Taryn Hill <taryn@phrohdoh.com>")
  40. .arg(Arg::with_name("archive_path")
  41. .value_name("archive_path")
  42. .required(true)
  43. .index(1))
  44. .arg(Arg::with_name("query")
  45. .value_name("query")
  46. .required(true)
  47. .index(2)))
  48. .get_matches();
  49.  
  50. let res = match matches.subcommand() {
  51. ("list", Some(args)) => cmd_list(args),
  52. ("search", Some(args)) => cmd_search(args),
  53. ("contains", Some(args)) => cmd_contains(args),
  54. _ => unreachable!(),
  55. };
  56.  
  57. let code = if let Some(e) = res.err() {
  58. println!("Error: {:?}", e);
  59. 255
  60. } else {
  61. 0
  62. };
  63.  
  64. std::process::exit(code);
  65. }
  66.  
  67. fn cmd_list(args: &clap::ArgMatches) -> Result<(), ReadError> {
  68. let path = args.value_of("archive_path").unwrap();
  69. let archive = try!(BigArchive::new_from_path(&path));
  70.  
  71. let names = (if let Some(internal_dir) = args.value_of("internal_dir") {
  72. archive.get_all_entry_names().filter(|n| n.starts_with(internal_dir))
  73. } else {
  74. archive.get_all_entry_names()
  75. }).collect::<Vec<_>>();
  76.  
  77. for name in names {
  78. println!("{:#?}", name);
  79. }
  80.  
  81.  
  82. Ok(())
  83. }
  84.  
  85. fn cmd_search(args: &clap::ArgMatches) -> Result<(), ReadError> {
  86. let path = args.value_of("archive_path").unwrap();
  87. let archive = try!(BigArchive::new_from_path(&path));
  88. let query = args.value_of("query").unwrap();
  89.  
  90. for name in archive.get_all_entry_names().filter(|n| n.contains(query)) {
  91. let entry = archive.get_entry(name)
  92. .expect(&format!("Failed to read known entry {} from {}", name, path));
  93. println!("{:#?}", entry);
  94. }
  95.  
  96. Ok(())
  97. }
  98.  
  99. fn cmd_contains(args: &clap::ArgMatches) -> Result<(), ReadError> {
  100. let path = args.value_of("archive_path").unwrap();
  101. let archive = try!(BigArchive::new_from_path(&path));
  102. let query = args.value_of("query").unwrap();
  103.  
  104. println!("{} contains {}: {}", path, query, archive.contains(query));
  105.  
  106. Ok(())
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement