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 eed32d8 commit 9bfc138
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 145 deletions.
120 changes: 17 additions & 103 deletions backend/examples/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,138 +32,52 @@ catalogs:
default: true
validate:
- command:
- bash
- validation-script-1.sh # executed first
- python
- examples/validation_script_ok.py # executed first
timeout: 60 # timeout in seconds
- command:
- bash
- validation-script-2.sh # AND then this one
- examples/dumb_script_ok.sh # AND then this one
post-validation:
- command:
- bash
- post-validation-script-1.sh # executed first
- python
- examples/validation_script_ok.py # executed first
timeout: 60 # timeout in seconds
output-model: string (optional) # model name
- command:
- bash
- post-validation-script-2.sh # AND then this one
output-model: string (optional) # model name
- command:
- bash
- post-validation-script-3.sh # AND finally this one
- examples/dumb_script_ok.sh # AND then this one
output-model: string (optional) # model name
- slug: shutdown-testing-environment
name: Shutdown Testing Environment
description: shutdown a temporary testing environment
- slug: stop-testing-environment
name: Stop Testing Environment
description: stop a testing environment
icon: trash
fields:
- slug: environment-name
title: environment name
- slug: name
title: Name
description: provide a name for your environment
placeholder: testing-123
type: text
default: testing-123
required: true
autocomplete-fetcher: ./your-script.py
validate:
- command:
- bash
- validation-script-1.sh # executed first
- python
- examples/validation_script_ok.py # executed first
timeout: 60 # timeout in seconds
- command:
- bash
- validation-script-2.sh # AND then this one
- examples/dumb_script_ok.sh # AND then this one
post-validation:
- command:
- bash
- post-validation-script-1.sh # executed first
- python
- examples/validation_script_ok.py # executed first
timeout: 60 # timeout in seconds
output-model: string (optional) # model name
- command:
- bash
- post-validation-script-2.sh # AND then this one
- examples/dumb_script_ok.sh # AND then this one
output-model: string (optional) # model name
- command:
- bash
- post-validation-script-3.sh # AND finally this one
output-model: string (optional) # model name
- slug: another-catalog
name: Another Catalog
description: Another catalog
services:
- slug: another-service
name: another service
description: another service
icon: target
fields:
- slug: field-1
title: field 1
description: field 1
placeholder: field 1
type: text
default: field 1
required: true
- slug: field-2
title: field 2
description: field 2
placeholder: field 2
type: text
default: field 2
required: true
- slug: field-3
title: field 3
description: field 3
placeholder: field 3
type: text
default: field 3
required: true
- slug: field-4
title: field 4
description: field 4
placeholder: field 4
type: text
default: field 4
required: true
- slug: field-5
title: field 5
description: field 5
placeholder: field 5
type: text
default: field 5
required: true
- slug: field-6
title: field 6
description: field 6
placeholder: field 6
type: text
default: field 6
required: true
- slug: field-7
title: field 7
description: field 7
placeholder: field 7
type: text
default: field 7
required: true
- slug: field-8
title: field 8
description: field 8
placeholder: field 8
type: text
default: field 8
required: true
- slug: field-9
title: field 9
description: field 9
placeholder: field 9
type: text
default: field 9
required: true
- slug: field-10
title: field 10
description: field 10
placeholder: field 10
type: text
models:
- name: string
description: string (optional)
Expand Down
11 changes: 11 additions & 0 deletions backend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ async fn main() {
}
};

show_loaded_config(&yaml_config);

let app = Router::new()
.fallback(unknown_route)
.route("/", get(|| async { "OK" }))
Expand All @@ -83,3 +85,12 @@ async fn main() {
let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
axum::serve(listener, app).await.unwrap();
}

fn show_loaded_config(yaml_config: &YamlConfig) {
for catalog in &yaml_config.catalogs {
info!("-> catalog '{}' loaded", catalog.slug);
for service in catalog.services.as_ref().unwrap_or(&vec![]) {
info!("\t|-> service '{}' loaded", service.slug);
}
}
}
56 changes: 14 additions & 42 deletions backend/src/yaml_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,31 +91,22 @@ impl CatalogServiceYamlConfig {
pub trait ExternalCommand {
fn get_command(&self) -> &Vec<String>;
fn get_timeout(&self) -> u64;
}

#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
pub struct CatalogServiceValidateYamlConfig {
pub command: Vec<String>,
pub timeout: Option<u64>,
}

impl CatalogServiceValidateYamlConfig {
pub fn validate(&self) -> Result<(), String> {
if self.command.is_empty() {
fn validate(&self) -> Result<(), String> {
if self.get_command().is_empty() {
return Err("command is empty".to_string());
}

// check if command is valid by checking if the first element (binary) exists and is executable by the current user
if self.command.len() > 1 {
let command = self.command.get(0).unwrap();
if self.get_command().len() >= 1 {
let command = self.get_command().get(0).unwrap();
if !which::which(command).is_ok() {
return Err(format!("command {} not found", command));
return Err(format!("command '{}' not found", command));
}
}

// check if the second element (file) exists and is executable by the current user
if self.command.len() > 2 {
let file = self.command.get(1).unwrap();
if self.get_command().len() >= 2 {
let file = self.get_command().get(1).unwrap();
if !std::path::Path::new(file).exists() {
return Err(format!("file '{}' not found", file));
}
Expand All @@ -125,6 +116,13 @@ impl CatalogServiceValidateYamlConfig {
}
}


#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
pub struct CatalogServiceValidateYamlConfig {
pub command: Vec<String>,
pub timeout: Option<u64>,
}

impl ExternalCommand for CatalogServiceValidateYamlConfig {
fn get_command(&self) -> &Vec<String> {
&self.command
Expand All @@ -149,32 +147,6 @@ pub struct CatalogServicePostValidateYamlConfig {
pub output_model: Option<String>,
}

impl CatalogServicePostValidateYamlConfig {
pub fn validate(&self) -> Result<(), String> {
if self.command.is_empty() {
return Err("command is empty".to_string());
}

// check if command is valid by checking if the first element (binary) exists and is executable by the current user
if self.command.len() > 1 {
let command = self.command.get(0).unwrap();
if !which::which(command).is_ok() {
return Err(format!("command {} not found", command));
}
}

// check if the second element (file) exists and is executable by the current user
if self.command.len() > 2 {
let file = self.command.get(1).unwrap();
if !std::path::Path::new(file).exists() {
return Err(format!("file '{}' not found", file));
}
}

Ok(())
}
}

impl ExternalCommand for CatalogServicePostValidateYamlConfig {
fn get_command(&self) -> &Vec<String> {
&self.command
Expand Down

0 comments on commit 9bfc138

Please sign in to comment.