From e1f721b04aa27647312381edf282833838c2c899 Mon Sep 17 00:00:00 2001 From: arbulu89 Date: Thu, 14 Mar 2024 15:28:25 +0100 Subject: [PATCH] Add exit codes to the tool --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/main.rs | 10 +++++++++- src/run.rs | 17 +++++++++++++++-- 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 736d665..c2ff0dd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -96,6 +96,12 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "exitcode" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de853764b47027c2e862a995c34978ffa63c1501f2e15f987ba11bd4f9bba193" + [[package]] name = "fastrand" version = "1.6.0" @@ -528,6 +534,7 @@ name = "photofinish" version = "1.2.2" dependencies = [ "clap", + "exitcode", "reqwest", "serde", "tokio", diff --git a/Cargo.toml b/Cargo.toml index ec9dd87..e82bbc8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,3 +9,4 @@ clap = {version = "3.0.7", features = ["cargo"] } serde = { version = "1.0", features = ["derive"] } reqwest = { version = "0.11", features = ["json"] } tokio = { version = "1", features = ["full"] } +exitcode = "1.1.2" diff --git a/src/main.rs b/src/main.rs index 1d2a4e3..865ec4b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ extern crate clap; +extern crate exitcode; use clap::{App, Arg}; @@ -40,12 +41,19 @@ async fn main() { if options.subcommand_matches("list").is_some() { list::show_list(&scenarios); + std::process::exit(exitcode::OK) } if let Some(run_options) = options.subcommand_matches("run") { let scenario_label = run_options.value_of("SET").unwrap(); let endpoint_url = run_options.value_of("url").unwrap(); let api_key = run_options.value_of("API_KEY").unwrap(); - run::run(endpoint_url, api_key, scenario_label.to_string(), scenarios).await; + match run::run(endpoint_url, api_key, scenario_label.to_string(), scenarios).await { + Ok(()) => std::process::exit(exitcode::OK), + Err(()) => std::process::exit(1) + } } + + println!("Subcommand not provided. Available subcommands: list|run"); + std::process::exit(exitcode::USAGE) } diff --git a/src/run.rs b/src/run.rs index 9399eed..3a4ebd2 100644 --- a/src/run.rs +++ b/src/run.rs @@ -7,6 +7,7 @@ enum FixtureResult { Success, Retryable { file: String }, Skippable, + Unauthorized, } struct Errored { @@ -38,6 +39,10 @@ async fn post_fixture( println!("Successfully POSTed file: {}", file); Ok(FixtureResult::Success) } + StatusCode::UNAUTHORIZED => { + println!("POST request unauthorized. Set the correct API_KEY as argument"); + Ok(FixtureResult::Unauthorized) + } StatusCode::BAD_REQUEST | StatusCode::UNPROCESSABLE_ENTITY | StatusCode::NOT_FOUND => Ok(FixtureResult::Retryable { @@ -87,13 +92,16 @@ pub async fn run( api_key: &str, scenario_label: String, scenarios: Vec, -) { +) -> Result<(), ()> { let selected_scenario = scenarios .iter() .find(|current_scenario| current_scenario.label == scenario_label); match selected_scenario { - None => println!("Non-existing scenario!"), + None => { + println!("Non-existing scenario!"); + return Err(()) + }, Some(scenario) => { let fixtures_in_directories: Vec = scenario .directories @@ -113,6 +121,9 @@ pub async fn run( retryable.push(FixtureResult::Retryable { file }) } Ok(FixtureResult::Skippable | FixtureResult::Success) => (), + Ok(FixtureResult::Unauthorized) => { + return Err(()) + }, Err(Errored { file, reason }) => { println!("An error occurred in loading fixture {}: {}", file, reason) } @@ -127,6 +138,8 @@ pub async fn run( } } } + + Ok(()) } fn extract_fixtures_from_directory(directory: &String) -> Option> {