Skip to content

Commit

Permalink
Merge pull request #20 from trento-project/add-exit-codes
Browse files Browse the repository at this point in the history
Add exit codes to the tool
  • Loading branch information
arbulu89 authored Mar 18, 2024
2 parents abdf3b4 + e1f721b commit fe8138a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
10 changes: 9 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
extern crate clap;
extern crate exitcode;

use clap::{App, Arg};

Expand Down Expand Up @@ -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)
}
17 changes: 15 additions & 2 deletions src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ enum FixtureResult {
Success,
Retryable { file: String },
Skippable,
Unauthorized,
}

struct Errored {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -87,13 +92,16 @@ pub async fn run(
api_key: &str,
scenario_label: String,
scenarios: Vec<Scenario>,
) {
) -> 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<String> = scenario
.directories
Expand All @@ -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)
}
Expand All @@ -127,6 +138,8 @@ pub async fn run(
}
}
}

Ok(())
}

fn extract_fixtures_from_directory(directory: &String) -> Option<Vec<String>> {
Expand Down

0 comments on commit fe8138a

Please sign in to comment.