diff --git a/src/cli/Cargo.toml b/src/cli/Cargo.toml index b42f5c8..89d519d 100644 --- a/src/cli/Cargo.toml +++ b/src/cli/Cargo.toml @@ -13,3 +13,4 @@ serde = { version = "1.0.197", features = ["derive"] } serde_yaml = "0.9.34" schemars = "0.8.16" serde_json = "1.0.115" +reqwest = "0.12.3" diff --git a/src/cli/config/config.template.yaml b/src/cli/config/config.template.yaml index 59baa6b..73dfaad 100644 --- a/src/cli/config/config.template.yaml +++ b/src/cli/config/config.template.yaml @@ -1,4 +1,4 @@ language: rust -env_path: /home/charley/polytech/cloudlet/src/cli/config/example.env -code_path: /home/charley/polytech/cloudlet/src/cli/src/main.rs +env_path: /path/cloudlet/src/cli/config/example.env +code_path: /path/cloudlet/src/cli/src/main.rs log_level: debug diff --git a/src/cli/src/args.rs b/src/cli/src/args.rs new file mode 100644 index 0000000..c78fa13 --- /dev/null +++ b/src/cli/src/args.rs @@ -0,0 +1,17 @@ +use clap::Parser; +use std::path::PathBuf; + +#[derive(Parser, Debug)] +#[command(version, about, long_about = None)] +pub struct CliArgs { + #[command(subcommand)] + pub command: Commands, +} + +#[derive(Parser, Debug)] +pub enum Commands { + Run { + #[arg(short, long)] + config_path: PathBuf, + }, +} diff --git a/src/cli/src/main.rs b/src/cli/src/main.rs index 74ceb15..1faebb8 100644 --- a/src/cli/src/main.rs +++ b/src/cli/src/main.rs @@ -1,31 +1,20 @@ use clap::Parser; -mod types; -mod utils; -use std::{ - io::{self}, - path::PathBuf, -}; -use types::{Config, Language, LogLevel}; -use utils::{load_config, read_file}; -#[derive(Parser, Debug)] -#[command(version, about, long_about = None)] -struct Args { - #[command(subcommand)] - command: Commands, -} +use args::{CliArgs, Commands}; +use request::run_request; +use request::HttpRunRequest; +use std::io::{self}; +use types::Config; +use utils::{load_config, read_file}; -#[derive(Parser, Debug)] -enum Commands { - Run { - #[arg(short, long)] - config_path: PathBuf, - }, -} +mod args; +mod request; +mod types; +mod utils; #[tokio::main] async fn main() -> io::Result<()> { - let args = Args::parse(); + let args = CliArgs::parse(); match args.command { Commands::Run { config_path } => { @@ -39,26 +28,15 @@ async fn main() -> io::Result<()> { let env = read_file(&yaml_config.env_path).expect("Error while reading the environment file"); println!("Env from file : \n{}", env); - println!("Configuration from YAML file:"); - println!( - "Language: {}", - match yaml_config.language { - Language::Rust => "Rust", - Language::Python => "Python", - Language::Node => "Node", - } - ); - println!("Env Path: {}", yaml_config.env_path); - println!( - "Log Level: {}", - match yaml_config.log_level { - LogLevel::Debug => "Debug", - LogLevel::Info => "Info", - LogLevel::Warn => "Warn", - LogLevel::Error => "Error", - } - ); - println!("Code Path: {}", yaml_config.code_path); + println!("Configuration from YAML file: \n {:#?}", yaml_config); + + let body = HttpRunRequest::new(yaml_config.language, env, code, yaml_config.log_level); + let response = run_request(body).await; + + match response { + Ok(_) => println!("Request successful"), + Err(e) => eprintln!("Error while making the request: {}", e), + } } } diff --git a/src/cli/src/request.rs b/src/cli/src/request.rs new file mode 100644 index 0000000..cb7872e --- /dev/null +++ b/src/cli/src/request.rs @@ -0,0 +1,41 @@ +use crate::types::{Language, LogLevel}; +use reqwest::Client; +use serde::Serialize; +use std::error::Error; + +#[derive(Serialize, Debug)] +pub struct HttpRunRequest { + pub language: Language, + pub env_content: String, + pub code_content: String, + pub log_level: LogLevel, +} + +impl HttpRunRequest { + pub fn new( + language: Language, + env_content: String, + code_content: String, + log_level: LogLevel, + ) -> Self { + HttpRunRequest { + language, + env_content, + code_content, + log_level, + } + } +} + +pub async fn run_request(request: HttpRunRequest) -> Result<(), Box> { + let client = Client::new(); + let res = client + .post("http://127.0.0.1:3000/run") + .body(serde_json::to_string(&request)?) + .send() + .await?; + println!("Response Status: {}", res.status()); + let body = res.text().await?; + println!("Response body: {}", body); + Ok(()) +} diff --git a/src/cli/src/types.rs b/src/cli/src/types.rs index 02f9e02..7e6fac6 100644 --- a/src/cli/src/types.rs +++ b/src/cli/src/types.rs @@ -1,7 +1,7 @@ use clap::ValueEnum; -use serde::Deserialize; +use serde::{Deserialize, Serialize}; -#[derive(Clone, Debug, ValueEnum, Deserialize)] +#[derive(Clone, Debug, ValueEnum, Deserialize, Serialize)] #[serde(rename_all = "lowercase")] pub enum Language { Rust, @@ -9,7 +9,7 @@ pub enum Language { Node, } -#[derive(Clone, Debug, ValueEnum, Deserialize)] +#[derive(Clone, Debug, ValueEnum, Deserialize, Serialize)] #[serde(rename_all = "lowercase")] pub enum LogLevel { Debug,