Skip to content

Commit

Permalink
feat: add read_file function to return content file
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 a2c1137 commit efdc54a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 30 deletions.
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: /home/charley/polytech/cloudlet/src/cli/config/example.env
code_path: /home/charley/polytech/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
47 changes: 21 additions & 26 deletions src/cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -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);
}
}

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)
}

0 comments on commit efdc54a

Please sign in to comment.