Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(CLI): implement reading of code and env files from config path and call "run" endpoint #15

Merged
merged 2 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: .env
code_path: src
env_path: /path/cloudlet/src/cli/config/example.env
code_path: /path/cloudlet/src/cli/src/main.rs
log_level: debug
3 changes: 3 additions & 0 deletions src/cli/config/example.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
HOST="localhost"
PORT=3000
PASSWORD=3456
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,
},
}
89 changes: 31 additions & 58 deletions src/cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,69 +1,42 @@
use clap::Parser;
mod types;
mod utils;
use std::io::{self};
use types::{Config, Language, LogLevel};
use utils::load_config;

#[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 {
Configure {
#[arg(short, long)]
config_path: String,
},
Status {},
Apply {},
Kill {},
}
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::Configure { config_path } => {
let config: Config = load_config(&config_path).unwrap();

println!("Configuration from YAML file:");
println!(
"Language: {}",
match config.language {
Language::Rust => "Rust",
Language::Python => "Python",
Language::Node => "Node",
}
);
println!("Env Path: {}", config.env_path);
println!("Code Path: {}", config.code_path);
println!(
"Log Level: {}",
match 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");
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: \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
13 changes: 11 additions & 2 deletions src/cli/src/utils.rs
Original file line number Diff line number Diff line change
@@ -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<Config> {
pub fn load_config(config_path: &PathBuf) -> io::Result<Config> {
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<String> {
let mut file = File::open(file_path)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
Ok(contents)
}