From efdc54a1ac7b67c999fb4bb8c8a608dfd1e27aee Mon Sep 17 00:00:00 2001 From: Charley Date: Wed, 10 Apr 2024 15:12:41 +0200 Subject: [PATCH 1/2] feat: add read_file function to return content file Signed-off-by: Charley --- src/cli/config/config.template.yaml | 4 +-- src/cli/config/example.env | 3 ++ src/cli/src/main.rs | 47 +++++++++++++---------------- src/cli/src/utils.rs | 13 ++++++-- 4 files changed, 37 insertions(+), 30 deletions(-) create mode 100644 src/cli/config/example.env diff --git a/src/cli/config/config.template.yaml b/src/cli/config/config.template.yaml index bdadbb4..59baa6b 100644 --- a/src/cli/config/config.template.yaml +++ b/src/cli/config/config.template.yaml @@ -1,4 +1,4 @@ language: rust -env_path: .env -code_path: src +env_path: /home/charley/polytech/cloudlet/src/cli/config/example.env +code_path: /home/charley/polytech/cloudlet/src/cli/src/main.rs log_level: debug diff --git a/src/cli/config/example.env b/src/cli/config/example.env new file mode 100644 index 0000000..8c01b5b --- /dev/null +++ b/src/cli/config/example.env @@ -0,0 +1,3 @@ +HOST="localhost" +PORT=3000 +PASSWORD=3456 \ No newline at end of file diff --git a/src/cli/src/main.rs b/src/cli/src/main.rs index 3e4ada0..74ceb15 100644 --- a/src/cli/src/main.rs +++ b/src/cli/src/main.rs @@ -1,9 +1,12 @@ use clap::Parser; mod types; mod utils; -use std::io::{self}; +use std::{ + io::{self}, + path::PathBuf, +}; use types::{Config, Language, LogLevel}; -use utils::load_config; +use utils::{load_config, read_file}; #[derive(Parser, Debug)] #[command(version, about, long_about = None)] @@ -14,56 +17,48 @@ struct Args { #[derive(Parser, Debug)] enum Commands { - Configure { + Run { #[arg(short, long)] - config_path: String, + config_path: PathBuf, }, - Status {}, - Apply {}, - Kill {}, } #[tokio::main] - async fn main() -> io::Result<()> { let args = Args::parse(); match args.command { - Commands::Configure { config_path } => { - let config: Config = load_config(&config_path).unwrap(); + Commands::Run { config_path } => { + let yaml_config: Config = + load_config(&config_path).expect("Error while loading the configuration file"); + + let code = + read_file(&yaml_config.code_path).expect("Error while reading the code file"); + println!("Code from file: \n{}", code); + 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 config.language { + match yaml_config.language { Language::Rust => "Rust", Language::Python => "Python", Language::Node => "Node", } ); - println!("Env Path: {}", config.env_path); - println!("Code Path: {}", config.code_path); + println!("Env Path: {}", yaml_config.env_path); println!( "Log Level: {}", - match config.log_level { + match yaml_config.log_level { LogLevel::Debug => "Debug", LogLevel::Info => "Info", LogLevel::Warn => "Warn", LogLevel::Error => "Error", } ); - } - - Commands::Status {} => { - println!("Getting status"); - } - - Commands::Apply {} => { - println!("Applying configuration"); - } - - Commands::Kill {} => { - println!("Killing configuration"); + println!("Code Path: {}", yaml_config.code_path); } } diff --git a/src/cli/src/utils.rs b/src/cli/src/utils.rs index e970e6f..e9a1992 100644 --- a/src/cli/src/utils.rs +++ b/src/cli/src/utils.rs @@ -1,11 +1,20 @@ use crate::types::Config; use std::fs::File; use std::io::{self, Read}; +use std::path::PathBuf; -pub fn load_config(config_path: &str) -> io::Result { +pub fn load_config(config_path: &PathBuf) -> io::Result { let mut file = File::open(config_path)?; let mut contents = String::new(); file.read_to_string(&mut contents)?; - let config: Config = serde_yaml::from_str(&contents).unwrap(); + let config: Config = serde_yaml::from_str(&contents) + .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; Ok(config) } + +pub fn read_file(file_path: &str) -> io::Result { + let mut file = File::open(file_path)?; + let mut contents = String::new(); + file.read_to_string(&mut contents)?; + Ok(contents) +} From 68681596b434e42be7c8b47d7b655bed2fcdf5ae Mon Sep 17 00:00:00 2001 From: Charley Date: Wed, 10 Apr 2024 16:00:27 +0200 Subject: [PATCH 2/2] feat: implement call to run endpoint + refactor clap arguments Signed-off-by: Charley --- src/cli/Cargo.toml | 1 + src/cli/config/config.template.yaml | 4 +- src/cli/src/args.rs | 17 ++++++++ src/cli/src/main.rs | 62 ++++++++++------------------- src/cli/src/request.rs | 41 +++++++++++++++++++ src/cli/src/types.rs | 6 +-- 6 files changed, 84 insertions(+), 47 deletions(-) create mode 100644 src/cli/src/args.rs create mode 100644 src/cli/src/request.rs 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,