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

Commit 9bfc138

Browse files
committed
wip(backend): fix yaml config validation
1 parent eed32d8 commit 9bfc138

File tree

3 files changed

+42
-145
lines changed

3 files changed

+42
-145
lines changed

backend/examples/config.yaml

Lines changed: 17 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -32,138 +32,52 @@ catalogs:
3232
default: true
3333
validate:
3434
- command:
35-
- bash
36-
- validation-script-1.sh # executed first
35+
- python
36+
- examples/validation_script_ok.py # executed first
3737
timeout: 60 # timeout in seconds
3838
- command:
3939
- bash
40-
- validation-script-2.sh # AND then this one
40+
- examples/dumb_script_ok.sh # AND then this one
4141
post-validation:
4242
- command:
43-
- bash
44-
- post-validation-script-1.sh # executed first
43+
- python
44+
- examples/validation_script_ok.py # executed first
4545
timeout: 60 # timeout in seconds
4646
output-model: string (optional) # model name
4747
- command:
4848
- bash
49-
- post-validation-script-2.sh # AND then this one
50-
output-model: string (optional) # model name
51-
- command:
52-
- bash
53-
- post-validation-script-3.sh # AND finally this one
49+
- examples/dumb_script_ok.sh # AND then this one
5450
output-model: string (optional) # model name
55-
- slug: shutdown-testing-environment
56-
name: Shutdown Testing Environment
57-
description: shutdown a temporary testing environment
51+
- slug: stop-testing-environment
52+
name: Stop Testing Environment
53+
description: stop a testing environment
5854
icon: trash
5955
fields:
60-
- slug: environment-name
61-
title: environment name
56+
- slug: name
57+
title: Name
6258
description: provide a name for your environment
6359
placeholder: testing-123
6460
type: text
6561
default: testing-123
6662
required: true
67-
autocomplete-fetcher: ./your-script.py
6863
validate:
6964
- command:
70-
- bash
71-
- validation-script-1.sh # executed first
65+
- python
66+
- examples/validation_script_ok.py # executed first
7267
timeout: 60 # timeout in seconds
7368
- command:
7469
- bash
75-
- validation-script-2.sh # AND then this one
70+
- examples/dumb_script_ok.sh # AND then this one
7671
post-validation:
7772
- command:
78-
- bash
79-
- post-validation-script-1.sh # executed first
73+
- python
74+
- examples/validation_script_ok.py # executed first
8075
timeout: 60 # timeout in seconds
8176
output-model: string (optional) # model name
8277
- command:
8378
- bash
84-
- post-validation-script-2.sh # AND then this one
79+
- examples/dumb_script_ok.sh # AND then this one
8580
output-model: string (optional) # model name
86-
- command:
87-
- bash
88-
- post-validation-script-3.sh # AND finally this one
89-
output-model: string (optional) # model name
90-
- slug: another-catalog
91-
name: Another Catalog
92-
description: Another catalog
93-
services:
94-
- slug: another-service
95-
name: another service
96-
description: another service
97-
icon: target
98-
fields:
99-
- slug: field-1
100-
title: field 1
101-
description: field 1
102-
placeholder: field 1
103-
type: text
104-
default: field 1
105-
required: true
106-
- slug: field-2
107-
title: field 2
108-
description: field 2
109-
placeholder: field 2
110-
type: text
111-
default: field 2
112-
required: true
113-
- slug: field-3
114-
title: field 3
115-
description: field 3
116-
placeholder: field 3
117-
type: text
118-
default: field 3
119-
required: true
120-
- slug: field-4
121-
title: field 4
122-
description: field 4
123-
placeholder: field 4
124-
type: text
125-
default: field 4
126-
required: true
127-
- slug: field-5
128-
title: field 5
129-
description: field 5
130-
placeholder: field 5
131-
type: text
132-
default: field 5
133-
required: true
134-
- slug: field-6
135-
title: field 6
136-
description: field 6
137-
placeholder: field 6
138-
type: text
139-
default: field 6
140-
required: true
141-
- slug: field-7
142-
title: field 7
143-
description: field 7
144-
placeholder: field 7
145-
type: text
146-
default: field 7
147-
required: true
148-
- slug: field-8
149-
title: field 8
150-
description: field 8
151-
placeholder: field 8
152-
type: text
153-
default: field 8
154-
required: true
155-
- slug: field-9
156-
title: field 9
157-
description: field 9
158-
placeholder: field 9
159-
type: text
160-
default: field 9
161-
required: true
162-
- slug: field-10
163-
title: field 10
164-
description: field 10
165-
placeholder: field 10
166-
type: text
16781
models:
16882
- name: string
16983
description: string (optional)

backend/src/main.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ async fn main() {
6464
}
6565
};
6666

67+
show_loaded_config(&yaml_config);
68+
6769
let app = Router::new()
6870
.fallback(unknown_route)
6971
.route("/", get(|| async { "OK" }))
@@ -83,3 +85,12 @@ async fn main() {
8385
let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
8486
axum::serve(listener, app).await.unwrap();
8587
}
88+
89+
fn show_loaded_config(yaml_config: &YamlConfig) {
90+
for catalog in &yaml_config.catalogs {
91+
info!("-> catalog '{}' loaded", catalog.slug);
92+
for service in catalog.services.as_ref().unwrap_or(&vec![]) {
93+
info!("\t|-> service '{}' loaded", service.slug);
94+
}
95+
}
96+
}

backend/src/yaml_config.rs

Lines changed: 14 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -91,31 +91,22 @@ impl CatalogServiceYamlConfig {
9191
pub trait ExternalCommand {
9292
fn get_command(&self) -> &Vec<String>;
9393
fn get_timeout(&self) -> u64;
94-
}
95-
96-
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
97-
pub struct CatalogServiceValidateYamlConfig {
98-
pub command: Vec<String>,
99-
pub timeout: Option<u64>,
100-
}
101-
102-
impl CatalogServiceValidateYamlConfig {
103-
pub fn validate(&self) -> Result<(), String> {
104-
if self.command.is_empty() {
94+
fn validate(&self) -> Result<(), String> {
95+
if self.get_command().is_empty() {
10596
return Err("command is empty".to_string());
10697
}
10798

10899
// check if command is valid by checking if the first element (binary) exists and is executable by the current user
109-
if self.command.len() > 1 {
110-
let command = self.command.get(0).unwrap();
100+
if self.get_command().len() >= 1 {
101+
let command = self.get_command().get(0).unwrap();
111102
if !which::which(command).is_ok() {
112-
return Err(format!("command {} not found", command));
103+
return Err(format!("command '{}' not found", command));
113104
}
114105
}
115106

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

119+
120+
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
121+
pub struct CatalogServiceValidateYamlConfig {
122+
pub command: Vec<String>,
123+
pub timeout: Option<u64>,
124+
}
125+
128126
impl ExternalCommand for CatalogServiceValidateYamlConfig {
129127
fn get_command(&self) -> &Vec<String> {
130128
&self.command
@@ -149,32 +147,6 @@ pub struct CatalogServicePostValidateYamlConfig {
149147
pub output_model: Option<String>,
150148
}
151149

152-
impl CatalogServicePostValidateYamlConfig {
153-
pub fn validate(&self) -> Result<(), String> {
154-
if self.command.is_empty() {
155-
return Err("command is empty".to_string());
156-
}
157-
158-
// check if command is valid by checking if the first element (binary) exists and is executable by the current user
159-
if self.command.len() > 1 {
160-
let command = self.command.get(0).unwrap();
161-
if !which::which(command).is_ok() {
162-
return Err(format!("command {} not found", command));
163-
}
164-
}
165-
166-
// check if the second element (file) exists and is executable by the current user
167-
if self.command.len() > 2 {
168-
let file = self.command.get(1).unwrap();
169-
if !std::path::Path::new(file).exists() {
170-
return Err(format!("file '{}' not found", file));
171-
}
172-
}
173-
174-
Ok(())
175-
}
176-
}
177-
178150
impl ExternalCommand for CatalogServicePostValidateYamlConfig {
179151
fn get_command(&self) -> &Vec<String> {
180152
&self.command

0 commit comments

Comments
 (0)