Skip to content

Commit

Permalink
feat: implement call to run endpoint + refactor clap arguments
Browse files Browse the repository at this point in the history
Signed-off-by: Charley <[email protected]>
  • Loading branch information
charley04310 committed Apr 10, 2024
1 parent efdc54a commit 6868159
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 47 deletions.
1 change: 1 addition & 0 deletions src/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
4 changes: 2 additions & 2 deletions src/cli/config/config.template.yaml
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions src/cli/src/args.rs
Original file line number Diff line number Diff line change
@@ -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,
},
}
62 changes: 20 additions & 42 deletions src/cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -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 } => {
Expand All @@ -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),
}
}
}

Expand Down
41 changes: 41 additions & 0 deletions src/cli/src/request.rs
Original file line number Diff line number Diff line change
@@ -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<dyn Error>> {
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(())
}
6 changes: 3 additions & 3 deletions src/cli/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
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,
Python,
Node,
}

#[derive(Clone, Debug, ValueEnum, Deserialize)]
#[derive(Clone, Debug, ValueEnum, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum LogLevel {
Debug,
Expand Down

0 comments on commit 6868159

Please sign in to comment.