Skip to content
This repository has been archived by the owner on Aug 13, 2024. It is now read-only.

Commit

Permalink
wip(backend): fix yaml config validation
Browse files Browse the repository at this point in the history
  • Loading branch information
evoxmusic committed Dec 26, 2023
1 parent 9bfc138 commit 0183fdc
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions backend/src/catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub struct JobResults {

#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct JobOutputResult {
pub slug: String,
pub one_liner_command: String,
pub output: serde_json::Value,
}

Expand Down Expand Up @@ -92,8 +92,8 @@ pub async fn exec_catalog_service_validate_scripts(
results: vec![],
};

for v in service.validate.as_ref().unwrap_or(&vec![]) {
let job_output_result = match execute_command(service_slug.as_str(), v, req.payload.to_string().as_str()).await {
for cmd in service.validate.as_ref().unwrap_or(&vec![]) {
let job_output_result = match execute_command(cmd, req.payload.to_string().as_str()).await {
Ok(job_output_result) => job_output_result,
Err(err) => return (StatusCode::BAD_REQUEST, Json(JobResponse {
message: Some(err),
Expand Down Expand Up @@ -132,8 +132,8 @@ pub async fn exec_catalog_service_post_validate_scripts(
};

// execute validate scripts
for v in service.validate.as_ref().unwrap_or(&vec![]) {
let _ = match execute_command(service_slug.as_str(), v, req.payload.to_string().as_str()).await {
for cmd in service.validate.as_ref().unwrap_or(&vec![]) {
let _ = match execute_command(cmd, req.payload.to_string().as_str()).await {
Ok(_) => (),
Err(err) => return (StatusCode::BAD_REQUEST, Json(JobResponse {
message: Some(err),
Expand All @@ -151,8 +151,8 @@ pub async fn exec_catalog_service_post_validate_scripts(
results: vec![],
};

for v in service.post_validate.as_ref().unwrap_or(&vec![]) {
let job_output_result = match execute_command(service_slug.as_str(), v, req.payload.to_string().as_str()).await {
for cmd in service.post_validate.as_ref().unwrap_or(&vec![]) {
let job_output_result = match execute_command(cmd, req.payload.to_string().as_str()).await {
Ok(job_output_result) => job_output_result,
Err(err) => todo!("{}", err) // TODO persist error in database
};
Expand All @@ -179,11 +179,11 @@ fn find_catalog_service_by_slug<'a>(catalog: &'a CatalogYamlConfig, service_slug
fn consume_job_output_result_from_json_output_env(service_slug: &str) -> JobOutputResult {
let job_output_result = match std::env::var("TORII_JSON_OUTPUT") {
Ok(json_output) => JobOutputResult {
slug: service_slug.to_string(),
one_liner_command: service_slug.to_string(),
output: serde_json::from_str(json_output.as_str()).unwrap_or(serde_json::json!({})),
},
Err(_) => JobOutputResult {
slug: service_slug.to_string(),
one_liner_command: service_slug.to_string(),
output: serde_json::json!({}),
}
};
Expand Down Expand Up @@ -229,10 +229,9 @@ fn check_json_payload_against_yaml_config_fields(
Ok(())
}

async fn execute_command<'a, T>(
service_slug: &'a str,
async fn execute_command<T>(
external_command: &T,
json_payload: &'a str,
json_payload: &str,
) -> Result<JobOutputResult, String> where T: ExternalCommand {
let cmd_one_line = external_command.get_command().join(" ");

Expand Down Expand Up @@ -278,7 +277,7 @@ async fn execute_command<'a, T>(

// TODO parse output.stdout and output.stderr and forward to the frontend

Ok(consume_job_output_result_from_json_output_env(service_slug))
Ok(consume_job_output_result_from_json_output_env(cmd_one_line.as_str()))
}

fn get_catalog_and_service<'a>(
Expand Down

0 comments on commit 0183fdc

Please sign in to comment.