Skip to content

Commit

Permalink
feature: added --version flag
Browse files Browse the repository at this point in the history
  • Loading branch information
shenek committed Sep 9, 2024
1 parent 5432ac0 commit 6108a62
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
17 changes: 10 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@ use hyper_util as _;
use indicatif as _;
use regex as _;

fn print_help(error: Option<String>) {
let error = if let Some(err_msg) = error {
format!("{}\n", err_msg)
} else {
String::new()
};
fn print_help(error: String) {
let first_line = if cfg!(feature = "http") {
"wait-for-them [-t timeout] [-s] host:port|url [host:port|url [host:port|url...]] [-- command [arg [arg...]]"
} else {
Expand Down Expand Up @@ -57,10 +52,18 @@ async fn main() {
silent,
} = match options::parse(args) {
Ok(options) => options,
Err(message) => {
Err(options::Action::Failed(message)) => {
print_help(message);
exit(999);
}
Err(options::Action::Version) => {
println!("wait-for-them {}", env!("CARGO_PKG_VERSION"));
exit(0);
}
Err(options::Action::Help) => {
print_help(String::new());
exit(0);
}
};

let instant = Instant::now();
Expand Down
32 changes: 26 additions & 6 deletions src/options.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use wait_for_them::ToCheck;

#[derive(Default)]
#[derive(Default, PartialEq, Debug)]
pub struct Options {
pub to_check: Vec<ToCheck>,
pub timeout: Option<u64>,
Expand All @@ -14,7 +14,14 @@ enum ParseState {
Command,
}

pub fn parse(args: Vec<String>) -> Result<Options, Option<String>> {
#[derive(PartialEq, Debug)]
pub enum Action {
Help,
Version,
Failed(String),
}

pub fn parse(args: Vec<String>) -> Result<Options, Action> {
let mut options = Options::default();

let mut state = ParseState::Host;
Expand All @@ -34,26 +41,29 @@ pub fn parse(args: Vec<String>) -> Result<Options, Option<String>> {
ParseState::Timeout => {
options.timeout = Some(
arg.parse()
.map_err(|_| Some("Failed to parse timeout".to_string()))?,
.map_err(|_| Action::Failed("Failed to parse timeout".to_string()))?,
);
state = ParseState::Host;
}
ParseState::Host => match arg.as_ref() {
"-t" | "--timeout" => state = ParseState::Timeout,
"-s" | "--silent" => options.silent = true,
"-h" | "--help" => return Err(None),
"-v" | "--version" => return Err(Action::Version),
"-h" | "--help" => return Err(Action::Help),
"--" => {
state = ParseState::Command;
}
_ => {
options.to_check.push(arg.parse::<ToCheck>()?);
options
.to_check
.push(arg.parse::<ToCheck>().map_err(|e| Action::Failed(e))?);
}
},
}
}

if options.to_check.is_empty() {
Err(Some(
Err(Action::Failed(
"You need to set at least one item to verify".to_string(),
))
} else {
Expand Down Expand Up @@ -116,6 +126,16 @@ mod tests {
assert!(options.unwrap().silent);
}

#[test]
fn version() {
assert_eq!(parse(vec!["--version".into(), "www.example.com:888".into()]), Err(super::Action::Version));
}

#[test]
fn help() {
assert_eq!(parse(vec!["--help".into(), "www.example.com:888".into()]), Err(super::Action::Help));
}

#[cfg(feature = "http")]
#[test]
fn uri() {
Expand Down

0 comments on commit 6108a62

Please sign in to comment.