Skip to content

Commit 919c9c2

Browse files
committed
Added CLI
1 parent f7670b0 commit 919c9c2

File tree

5 files changed

+235
-4
lines changed

5 files changed

+235
-4
lines changed

Cargo.lock

Lines changed: 127 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ edition = "2024"
55

66
[dependencies]
77
chrono = { version = "0.4.41", features = ["serde"] }
8+
clap = { version = "4.5.38", features = ["derive"] }
89
curl = "0.4.47"
910
mockito = "1.7.0"
1011
onig = "6.5.1"

src/build_order.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,4 +357,16 @@ pub enum BuildOrderError {
357357
ParseError(String),
358358
InvalidData(String),
359359
HttpError(String),
360+
Cloaked,
361+
}
362+
363+
impl fmt::Display for BuildOrderError {
364+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
365+
match self {
366+
BuildOrderError::ParseError(msg) => write!(f, "Parse Error: {}", msg),
367+
BuildOrderError::InvalidData(msg) => write!(f, "Invalid Data: {}", msg),
368+
BuildOrderError::HttpError(msg) => write!(f, "HTTP Error: {}", msg),
369+
BuildOrderError::Cloaked => write!(f, "Build order is cloaked."),
370+
}
371+
}
360372
}

src/build_parser.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ pub fn fetch_build_order(build_id: u32) -> Result<BuildOrder, BuildOrderError> {
200200
let url = format!("{}{}/", BUILD_URL, build_id);
201201
match HttpClient::fetch_url(&url) {
202202
Ok(response) => {
203+
if response.status_code == 302 {
204+
return Err(BuildOrderError::Cloaked);
205+
}
203206
if response.status_code != 200 {
204207
return Err(BuildOrderError::HttpError(format!(
205208
"Failed to fetch build order (URL: {} ) (Status: {})",

src/main.rs

Lines changed: 92 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,98 @@
11
use build_warren::build_parser::fetch_build_order;
22
use build_warren::index_manager::get_st_highest_index;
3+
use clap::{Parser, Subcommand};
34
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+
}
435

536
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+
}
1098
}

0 commit comments

Comments
 (0)