Skip to content

Commit

Permalink
refactor(cli): update code quality
Browse files Browse the repository at this point in the history
Signed-off-by: Charley <[email protected]>
  • Loading branch information
charley04310 committed Apr 20, 2024
1 parent 1b36132 commit 7364de2
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[workspace]
members = [ "src/api","src/vmm", "src/cli"]
members = ["src/api", "src/vmm", "src/cli"]
resolver = "2"
11 changes: 8 additions & 3 deletions proto/vmm.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,29 @@ package vmmorchestrator;

enum Language {
PYTHON = 0;
JAVASCRIPT = 1;
NODE = 1;
RUST = 2;
}

enum LogLevel {
DEBUG = 0;
INFO = 1;
WARN = 2;
ERROR = 3;
}

service VmmService {
rpc Run (RunVmmRequest) returns (RunVmmResponse);

}

message RunVmmRequest {

Language language = 1;
LogLevel log_level = 4;
string code = 2;
string env = 3;
LogLevel log_level = 4;

}

message RunVmmResponse {
}
12 changes: 6 additions & 6 deletions src/cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use clap::Parser;

use crate::types::YamlConfigFile;
use args::{CliArgs, Commands};
use services::run_request;
use services::HttpRunRequest;

use services::HttpVmmRequest;
use std::io::{self};
use types::Config;
use utils::load_config;

mod args;
Expand All @@ -18,10 +18,10 @@ async fn main() -> io::Result<()> {

match args.command {
Commands::Run { config_path } => {
let yaml_config: Config =
let yaml_config: YamlConfigFile =
load_config(&config_path).expect("Error while loading the configuration file");
let body = HttpRunRequest::new(yaml_config);
let response = run_request(body).await;
let body = HttpVmmRequest::new(yaml_config);
let response = HttpVmmRequest::post(body).await;

match response {
Ok(_) => println!("Request successful"),
Expand Down
36 changes: 17 additions & 19 deletions src/cli/src/services.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,40 @@
use crate::types::{Config, Language, LogLevel};
use crate::types::{Language, LogLevel, YamlConfigFile};
use crate::utils::read_file;
use reqwest::Client;
use serde::Serialize;
use std::error::Error;


#[derive(Serialize, Debug)]
pub struct HttpRunRequest {
pub struct HttpVmmRequest {
pub language: Language,
pub env: String,
pub code: String,
pub log_level: LogLevel,
}

impl HttpRunRequest {
pub fn new(config: Config) -> Self {
impl HttpVmmRequest {
pub fn new(config: YamlConfigFile) -> Self {
let code: String = read_file(&config.code_path).expect("Error while reading the code file");
let env = read_file(&config.env_path).expect("Error while reading the environment file");
let language = config.language;
let log_level = config.log_level;
HttpRunRequest {
HttpVmmRequest {
language,
env,
code,
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(())
pub async fn post(request: HttpVmmRequest) -> 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(())
}
}
8 changes: 5 additions & 3 deletions src/cli/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::path::PathBuf;

use clap::ValueEnum;
use serde::{Deserialize, Serialize};

Expand All @@ -19,9 +21,9 @@ pub enum LogLevel {
}

#[derive(Deserialize, Debug)]
pub struct Config {
pub struct YamlConfigFile {
pub language: Language,
pub env_path: String,
pub code_path: String,
pub env_path: PathBuf,
pub code_path: PathBuf,
pub log_level: LogLevel,
}
8 changes: 4 additions & 4 deletions src/cli/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use crate::types::Config;
use crate::types::YamlConfigFile;
use std::fs::File;
use std::io::{self, Read};
use std::path::PathBuf;

pub fn load_config(config_path: &PathBuf) -> io::Result<Config> {
pub fn load_config(config_path: &PathBuf) -> io::Result<YamlConfigFile> {
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)
let config: YamlConfigFile = 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<String> {
pub fn read_file(file_path: &PathBuf) -> io::Result<String> {
let mut file = File::open(file_path)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
Expand Down

0 comments on commit 7364de2

Please sign in to comment.