1use sort_core::Param;
2use std::str::FromStr;
3
4use sort_core;
5use file_utils;
6use misc_utils;
7
8use clap::*;
9use cli_utils::*;
10
11use json_printer::BracketType;
12
13pub fn sub_command<'a, 'b>() -> App<'a, 'b> {
14 SubCommand::with_name("sort")
15 .about("Sort SBX blocks in container, can also readjust burst error resistance level
16
17===== IMPORTANT =====
18Please note that this is the last version of this software to be released under the name rsbx,
19future releases will be published under the name blkar. See project repo for details.
20=====================")
21 .arg(in_file_arg()
22 .help("SBX container to sort"))
23 .arg(out_file_arg()
24 .required(true)
25 .help("Sorted SBX container"))
26 .arg(Arg::with_name("force")
27 .short("f")
28 .long("force")
29 .help("Force overwrite even if OUT exists"))
30 .arg(pr_verbosity_level_arg())
31 .arg(burst_arg()
32 .help("Burst error resistance level to use for the output container.
33Defaults to guessing the level (guesses up to 1000) used by the
34original container and uses the result."))
35 .arg(verbose_arg()
36 .help("Show reference block info"))
37 .arg(json_arg())
38}
39
40pub fn sort<'a>(matches : &ArgMatches<'a>) -> i32 {
41 let json_printer = get_json_printer!(matches);
42
43 json_printer.print_open_bracket(None, BracketType::Curly);
44
45 let in_file = get_in_file!(matches, json_printer);
46 let out_file = {
47 let out_file = matches.value_of("out_file").unwrap();
48
49 if file_utils::check_if_file_is_dir(out_file) {
50 misc_utils::make_path(&[out_file, in_file])
51 } else {
52 String::from(out_file)
53 }
54 };
55
56 let burst = get_burst_opt!(matches, json_printer);
57
58 exit_if_file!(exists &out_file
59 => matches.is_present("force")
60 => json_printer
61 => "File \"{}\" already exists", out_file);
62
63 let pr_verbosity_level = get_pr_verbosity_level!(matches, json_printer);
64
65 let param = Param::new(get_ref_block_choice!(matches),
66 &json_printer,
67 in_file,
68 &out_file,
69 matches.is_present("verbose"),
70 pr_verbosity_level,
71 burst);
72 match sort_core::sort_file(¶m) {
73 Ok(Some(s)) => exit_with_msg!(ok json_printer => "{}", s),
74 Ok(None) => exit_with_msg!(ok json_printer => ""),
75 Err(e) => exit_with_msg!(op json_printer => "{}", e),
76 }
77}