refactor: cli to work next to lib

This commit is contained in:
Cain 2023-10-17 00:05:32 +01:00
parent 6084c56d4f
commit 7d4649b6f5
4 changed files with 22 additions and 158 deletions

View file

@ -1,6 +1,8 @@
[package]
name = "gamedig-cli"
authors = ["rust-GameDig contributors [https://github.com/gamedig/rust-gamedig/contributors]"]
authors = [
"rust-GameDig contributors [https://github.com/gamedig/rust-gamedig/contributors]",
]
description = "A command line interface for gamedig"
license = "MIT"
version = "0.4.1"
@ -11,12 +13,4 @@ edition = "2021"
[dependencies]
clap = { version = "4.1.11", features = ["derive"] }
gamedig = { version = "*", path = "../lib" }
strum = "0.25.0"
strum_macros = "0.24.3"
thiserror = "1.0.43"
[profile.release]
opt-level = 3
debug = false
rpath = true
lto = true

View file

@ -11,6 +11,6 @@ pub enum Error {
#[error("Gamedig Error: {0}")]
Gamedig(#[from] gamedig::errors::GDError),
#[error("Strum Error: {0}")]
Strum(#[from] strum::ParseError),
#[error("Unknown Game: {0}")]
UnknownGame(String),
}

View file

@ -1,93 +0,0 @@
use strum_macros::{Display, EnumString};
#[derive(EnumString, Display, Debug, Clone)]
pub enum Game {
#[strum(serialize = "aliens")]
AlienSwarm,
#[strum(serialize = "aoc")]
AgeOfChivalry,
#[strum(serialize = "arma2oa")]
ARMA2OperationArrowhead,
#[strum(serialize = "ase")]
ARKSurvivalEvolved,
#[strum(serialize = "asrd")]
AlienSwarmReactiveDrop,
#[strum(serialize = "avorion")]
Avorion,
#[strum(serialize = "bat1944")]
Battalion1944,
#[strum(serialize = "bb2")]
BrainBread2,
#[strum(serialize = "bf1942")]
Battlefield1942,
#[strum(serialize = "bm")]
BlackMesa,
#[strum(serialize = "bo")]
BallisticOverkill,
#[strum(serialize = "ccure")]
CodenameCURE,
#[strum(serialize = "cosu")]
ColonySurvival,
#[strum(serialize = "cs")]
CounterStrike,
#[strum(serialize = "cscz")]
CounterStrikeConditionZero,
#[strum(serialize = "csgo")]
CounterStrikeGlobalOffensive,
#[strum(serialize = "css")]
CounterStrikeSource,
#[strum(serialize = "dod")]
DayOfDefeat,
#[strum(serialize = "dods")]
DayOfDefeatSource,
#[strum(serialize = "doi")]
DayOfInfamy,
#[strum(serialize = "dst")]
DontStarveTogether,
#[strum(serialize = "gm")]
GarrysMod,
#[strum(serialize = "hl2dm")]
HalfLife2Deathmatch,
#[strum(serialize = "hldms")]
HalfLifeDeathmatchSource,
#[strum(serialize = "ins")]
Insurgency,
#[strum(serialize = "insmic")]
InsurgencyModernInfantryCombat,
#[strum(serialize = "inss")]
InsurgencySandstorm,
#[strum(serialize = "l4d")]
Left4Dead,
#[strum(serialize = "l4d2")]
Left4Dead2,
#[strum(serialize = "mc")]
Minecraft,
#[strum(serialize = "ohd")]
OperationHarshDoorstop,
#[strum(serialize = "onset")]
Onset,
#[strum(serialize = "pz")]
ProjectZomboid,
#[strum(serialize = "ror2")]
RiskOfRain2,
#[strum(serialize = "rust")]
Rust,
#[strum(serialize = "sc")]
SvenCoOp,
#[strum(serialize = "sdtd")]
SevenDaysToDie,
#[strum(serialize = "tf")]
TeamFortress,
#[strum(serialize = "tf2")]
TeamFortress2,
#[strum(serialize = "tfc")]
TeamFortressClassic,
#[strum(serialize = "ts")]
TheShip,
#[strum(serialize = "unturned")]
Unturned,
#[strum(serialize = "ut")]
UnrealTournament,
#[strum(serialize = "vr")]
VRising,
}

View file

@ -1,73 +1,36 @@
mod error;
mod key;
use self::{error::Result, key::Game};
use std::net::IpAddr;
use clap::Parser;
use gamedig::games::*;
mod error;
use self::error::Result;
#[derive(Parser)]
#[command(author, version, about)]
struct Cli {
#[arg(short, long)]
game: Game,
game: String,
#[arg(short, long)]
ip: String,
ip: IpAddr,
#[arg(short, long)]
port: u16,
port: Option<u16>,
}
#[rustfmt::skip]
fn main() -> Result<()> {
let args = Cli::parse();
match args.game {
Game::AlienSwarm => Ok(println!("{:#?}", aliens::query(&args.ip, Some(args.port))?)),
Game::AgeOfChivalry => Ok(println!("{:#?}", aoc::query(&args.ip, Some(args.port))?)),
Game::ARMA2OperationArrowhead => Ok(println!("{:#?}", arma2oa::query(&args.ip, Some(args.port))?)),
Game::ARKSurvivalEvolved => Ok(println!("{:#?}", ase::query(&args.ip, Some(args.port))?)),
Game::AlienSwarmReactiveDrop => Ok(println!("{:#?}", asrd::query(&args.ip, Some(args.port))?)),
Game::Avorion => Ok(println!("{:#?}", avorion::query(&args.ip, Some(args.port))?)),
Game::Battalion1944 => Ok(println!("{:#?}", bat1944::query(&args.ip, Some(args.port))?)),
Game::BrainBread2 => Ok(println!("{:#?}", bb2::query(&args.ip, Some(args.port))?)),
Game::Battlefield1942 => Ok(println!("{:#?}", bf1942::query(&args.ip, Some(args.port))?)),
Game::BlackMesa => Ok(println!("{:#?}", bm::query(&args.ip, Some(args.port))?)),
Game::BallisticOverkill => Ok(println!("{:#?}", bo::query(&args.ip, Some(args.port))?)),
Game::CodenameCURE => Ok(println!("{:#?}", ccure::query(&args.ip, Some(args.port))?)),
Game::ColonySurvival => Ok(println!("{:#?}", cosu::query(&args.ip, Some(args.port))?)),
Game::CounterStrike => Ok(println!("{:#?}", cs::query(&args.ip, Some(args.port))?)),
Game::CounterStrikeConditionZero => Ok(println!("{:#?}", cscz::query(&args.ip, Some(args.port))?)),
Game::CounterStrikeGlobalOffensive => Ok(println!("{:#?}", csgo::query(&args.ip, Some(args.port))?)),
Game::CounterStrikeSource => Ok(println!("{:#?}", css::query(&args.ip, Some(args.port))?)),
Game::DayOfDefeat => Ok(println!("{:#?}", dod::query(&args.ip, Some(args.port))?)),
Game::DayOfDefeatSource => Ok(println!("{:#?}", dods::query(&args.ip, Some(args.port))?)),
Game::DayOfInfamy => Ok(println!("{:#?}", doi::query(&args.ip, Some(args.port))?)),
Game::DontStarveTogether => Ok(println!("{:#?}", dst::query(&args.ip, Some(args.port))?)),
Game::GarrysMod => Ok(println!("{:#?}", gm::query(&args.ip, Some(args.port))?)),
Game::HalfLife2Deathmatch => Ok(println!("{:#?}", hl2dm::query(&args.ip, Some(args.port))?)),
Game::HalfLifeDeathmatchSource => Ok(println!("{:#?}", hldms::query(&args.ip, Some(args.port))?)),
Game::Insurgency => Ok(println!("{:#?}", ins::query(&args.ip, Some(args.port))?)),
Game::InsurgencyModernInfantryCombat => Ok(println!("{:#?}", insmic::query(&args.ip, Some(args.port))?)),
Game::InsurgencySandstorm => Ok(println!("{:#?}", inss::query(&args.ip, Some(args.port))?)),
Game::Left4Dead => Ok(println!("{:#?}", l4d::query(&args.ip, Some(args.port))?)),
Game::Left4Dead2 => Ok(println!("{:#?}", l4d2::query(&args.ip, Some(args.port))?)),
Game::Minecraft => Ok(println!("{:#?}", mc::query(&args.ip, Some(args.port))?)),
Game::OperationHarshDoorstop => Ok(println!("{:#?}", ohd::query(&args.ip, Some(args.port))?)),
Game::Onset => Ok(println!("{:#?}", onset::query(&args.ip, Some(args.port))?)),
Game::ProjectZomboid => Ok(println!("{:#?}", pz::query(&args.ip, Some(args.port))?)),
Game::RiskOfRain2 => Ok(println!("{:#?}", ror2::query(&args.ip, Some(args.port))?)),
Game::Rust => Ok(println!("{:#?}", rust::query(&args.ip, Some(args.port))?)),
Game::SvenCoOp => Ok(println!("{:#?}", sc::query(&args.ip, Some(args.port))?)),
Game::SevenDaysToDie => Ok(println!("{:#?}", sdtd::query(&args.ip, Some(args.port))?)),
Game::TeamFortress => Ok(println!("{:#?}", tf::query(&args.ip, Some(args.port))?)),
Game::TeamFortress2 => Ok(println!("{:#?}", tf2::query(&args.ip, Some(args.port))?)),
Game::TeamFortressClassic => Ok(println!("{:#?}", tfc::query(&args.ip, Some(args.port))?)),
Game::TheShip => Ok(println!("{:#?}", ts::query(&args.ip, Some(args.port))?)),
Game::Unturned => Ok(println!("{:#?}", unturned::query(&args.ip, Some(args.port))?)),
Game::UnrealTournament => Ok(println!("{:#?}", ut::query(&args.ip, Some(args.port))?)),
Game::VRising => Ok(println!("{:#?}", vr::query(&args.ip, Some(args.port))?)),
let game = match GAMES.get(&args.game) {
Some(game) => game,
None => return Err(error::Error::UnknownGame(args.game)),
};
println!("{:#?}", query(game, &args.ip, args.port)?.as_json());
Ok(())
}
}
}