-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from charley04310/main
feat(CLI): implement reading of code and env files from config path and call "run" endpoint
- Loading branch information
Showing
8 changed files
with
109 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
HOST="localhost" | ||
PORT=3000 | ||
PASSWORD=3456 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |