Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exit codes to the tool #20

Merged
merged 1 commit into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Collaborator

@dottorblaster dottorblaster Mar 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty surprised exitcode doesn't have a 1 exit code inside, I know it just wraps value from sysexits.h but it feels a little clumsy 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah man, i don't know why it doesn't include it. I was just to about to remove the complete package usage only for that hehe

}
}

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
Loading