Implement a lot of CLI
This commit is contained in:
parent
11c5553e23
commit
5a4bb20433
4 changed files with 168 additions and 4 deletions
106
src/cli.rs
Normal file
106
src/cli.rs
Normal file
|
@ -0,0 +1,106 @@
|
|||
use std::{fs::File, io::BufReader, path::Path};
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
use clap::Parser;
|
||||
use serde_json::from_reader;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum Mode {
|
||||
Bink1998,
|
||||
Bink2002,
|
||||
ConfirmationId,
|
||||
}
|
||||
|
||||
impl Default for Mode {
|
||||
fn default() -> Self {
|
||||
Self::Bink1998
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(author, about, long_about = None)]
|
||||
pub struct Options {
|
||||
/// Enable verbose output
|
||||
#[arg(short, long)]
|
||||
verbose: bool,
|
||||
|
||||
/// Number of keys to generate
|
||||
#[arg(short = 'n', long = "number", default_value = "1")]
|
||||
num_keys: i32,
|
||||
|
||||
/// Specify which keys file to load
|
||||
#[arg(short = 'f', long = "file", default_value = "keys.json")]
|
||||
keys_filename: String,
|
||||
|
||||
/// Installation ID used to generate confirmation ID
|
||||
#[arg(short, long)]
|
||||
instid: Option<String>,
|
||||
|
||||
/// Specify which BINK identifier to load
|
||||
#[arg(short, long, default_value = "2E")]
|
||||
binkid: String,
|
||||
|
||||
/// Show which products/binks can be loaded
|
||||
#[arg(short, long)]
|
||||
list: bool,
|
||||
|
||||
/// Specify which Channel Identifier to use
|
||||
#[arg(short = 'c', long = "channel", default_value = "640")]
|
||||
channel_id: i32,
|
||||
|
||||
#[clap(skip)]
|
||||
application_mode: Mode,
|
||||
}
|
||||
|
||||
pub fn load_json<P: AsRef<Path>>(path: P) -> Result<serde_json::Value> {
|
||||
let file = File::open(path)?;
|
||||
let reader = BufReader::new(file);
|
||||
let json = from_reader(reader)?;
|
||||
Ok(json)
|
||||
}
|
||||
|
||||
pub fn parse_command_line() -> Result<Options> {
|
||||
let mut args = Options::parse();
|
||||
if args.instid.is_some() {
|
||||
args.application_mode = Mode::ConfirmationId;
|
||||
}
|
||||
Ok(args)
|
||||
}
|
||||
|
||||
pub fn validate_command_line(options: &mut Options) -> Result<serde_json::Value> {
|
||||
if options.verbose {
|
||||
println!("Loading keys file {}", options.keys_filename);
|
||||
}
|
||||
|
||||
let keys = load_json(&options.keys_filename)?;
|
||||
|
||||
if options.verbose {
|
||||
println!("Loaded keys from {} successfully", options.keys_filename);
|
||||
}
|
||||
|
||||
if options.list {
|
||||
let products = keys["Products"].as_object().ok_or(anyhow!(
|
||||
"`Products` object not found in {}",
|
||||
options.keys_filename
|
||||
))?;
|
||||
for (key, value) in products.iter() {
|
||||
println!("{}: {}", key, value["BINK"]);
|
||||
}
|
||||
|
||||
println!("\n\n** Please note: any BINK ID other than 2E is considered experimental at this time **\n");
|
||||
}
|
||||
|
||||
let bink_id = u32::from_str_radix(&options.binkid, 16)?;
|
||||
|
||||
if bink_id >= 0x40 {
|
||||
options.application_mode = Mode::Bink2002;
|
||||
}
|
||||
|
||||
if options.channel_id > 999 {
|
||||
return Err(anyhow!(
|
||||
"Refusing to create a key with a Channel ID greater than 999"
|
||||
));
|
||||
}
|
||||
|
||||
Ok(keys)
|
||||
}
|
|
@ -1,3 +1,10 @@
|
|||
fn main() {
|
||||
use anyhow::Result;
|
||||
|
||||
mod cli;
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let mut args = cli::parse_command_line()?;
|
||||
let _keys = cli::validate_command_line(&mut args);
|
||||
println!("Hello, world!");
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue