|
1 | 1 | use build_warren::build_parser::fetch_build_order; |
2 | 2 | use build_warren::index_manager::get_st_highest_index; |
| 3 | +use clap::{Parser, Subcommand}; |
3 | 4 | use serde_json; |
| 5 | +use std::fs; |
| 6 | + |
| 7 | +#[derive(Parser)] |
| 8 | +#[command(version, about, long_about = None)] |
| 9 | +struct Cli { |
| 10 | + #[command(subcommand)] |
| 11 | + command: Option<Commands>, |
| 12 | + |
| 13 | + #[arg(short, long)] |
| 14 | + output: Option<String>, |
| 15 | +} |
| 16 | + |
| 17 | +#[derive(Subcommand)] |
| 18 | +enum Commands { |
| 19 | + /// Fetch the highest build index from Spawning Tool |
| 20 | + BuildCount, |
| 21 | + |
| 22 | + /// Fetch a specific build order by ID |
| 23 | + Fetch { |
| 24 | + /// The ID of the build order to fetch |
| 25 | + id: u32, |
| 26 | + }, |
| 27 | + |
| 28 | + /// Fetch the latest N build orders (default: 1) |
| 29 | + FetchLatest { |
| 30 | + /// The number of latest build orders to fetch |
| 31 | + #[arg(default_value_t = 1)] |
| 32 | + count: u32, |
| 33 | + }, |
| 34 | +} |
4 | 35 |
|
5 | 36 | fn main() { |
6 | | - let highest_index = get_st_highest_index(); |
7 | | - let build = fetch_build_order(189949); |
8 | | - println!("Highest Build Index: {}", highest_index); |
9 | | - println!("Build Order: {}", serde_json::to_string(&build).unwrap()); |
| 37 | + let cli = Cli::parse(); |
| 38 | + |
| 39 | + match &cli.command { |
| 40 | + Some(Commands::BuildCount) => { |
| 41 | + let highest_index = get_st_highest_index(); |
| 42 | + if let Some(output_file) = &cli.output { |
| 43 | + fs::write(output_file, highest_index.to_string()) |
| 44 | + .expect("Failed to write to output file"); |
| 45 | + } else { |
| 46 | + println!("Highest build index: {}", highest_index); |
| 47 | + } |
| 48 | + } |
| 49 | + Some(Commands::Fetch { id }) => match fetch_build_order(*id) { |
| 50 | + Ok(build_order) => { |
| 51 | + let json_output = serde_json::to_string_pretty(&build_order) |
| 52 | + .expect("Failed to serialize build order to JSON"); |
| 53 | + if let Some(output_file) = &cli.output { |
| 54 | + fs::write(output_file, json_output) |
| 55 | + .expect("Failed to write build order to output file"); |
| 56 | + } else { |
| 57 | + println!("{}", json_output); |
| 58 | + } |
| 59 | + } |
| 60 | + Err(e) => eprintln!("Error fetching build order: {}", e), |
| 61 | + }, |
| 62 | + Some(Commands::FetchLatest { count }) => { |
| 63 | + let highest_index = get_st_highest_index(); |
| 64 | + let start_index = if *count > highest_index { |
| 65 | + 1 |
| 66 | + } else { |
| 67 | + highest_index - *count + 1 |
| 68 | + }; |
| 69 | + let mut increment = 0; |
| 70 | + let mut build_orders = Vec::new(); |
| 71 | + while increment < *count { |
| 72 | + let current_id = start_index + increment; |
| 73 | + match fetch_build_order(current_id) { |
| 74 | + Ok(build_order) => { |
| 75 | + build_orders.push(build_order); |
| 76 | + increment += 1; |
| 77 | + } |
| 78 | + Err(e) => { |
| 79 | + eprintln!("Error fetching build order {}: {}", current_id, e); |
| 80 | + increment += 1; // Increment to avoid infinite loop |
| 81 | + continue; // Some builds might not be available, continue fetching |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + let json_output = serde_json::to_string_pretty(&build_orders) |
| 86 | + .expect("Failed to serialize build orders to JSON"); |
| 87 | + if let Some(output_file) = &cli.output { |
| 88 | + fs::write(output_file, json_output) |
| 89 | + .expect("Failed to write build orders to output file"); |
| 90 | + } else { |
| 91 | + println!("{}", json_output); |
| 92 | + } |
| 93 | + } |
| 94 | + None => { |
| 95 | + eprintln!("No command provided. Use --help to see available commands."); |
| 96 | + } |
| 97 | + } |
10 | 98 | } |
0 commit comments