From efdc54a1ac7b67c999fb4bb8c8a608dfd1e27aee Mon Sep 17 00:00:00 2001 From: Charley Date: Wed, 10 Apr 2024 15:12:41 +0200 Subject: [PATCH] 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) +}