Added Parser

This commit is contained in:
2024-01-21 18:45:34 +01:00
parent 114a1accde
commit 708ae57d1f
6 changed files with 103 additions and 6 deletions
+1
View File
@@ -0,0 +1 @@
pub mod parse;
+90
View File
@@ -0,0 +1,90 @@
use std::{
fs::{self, File},
io::{BufRead, BufReader},
};
use anyhow::Context;
const GLOBAL_CONNECTION_PATH: &str = "conf/global/connections.env";
const LOCAL_CONNECTION_PATH: &str = "conf/local/.env";
pub fn set_local_db(start: bool) -> Result<(), Box<dyn std::error::Error>> {
let file: File = File::open(GLOBAL_CONNECTION_PATH)
.with_context(|| format!("could not read file `{}`", GLOBAL_CONNECTION_PATH))?;
let reader: BufReader<File> = BufReader::new(file);
let modified_lines: Vec<String> = reader
.lines()
.map(|line| {
let line_content: String = line.unwrap();
if start && connections_env_comment(&line_content) {
format!("# {}", line_content)
} else if !start && should_commented_out(&line_content) {
line_content[2..].to_string()
} else {
line_content
}
})
.collect();
fs::write(GLOBAL_CONNECTION_PATH, modified_lines.join("\n")).expect("");
Ok(())
}
fn connections_env_comment(line: &str) -> bool {
line.starts_with("ci.db.master.ip=") || line.starts_with("ci.db.master.port=")
}
fn should_commented_out(line: &str) -> bool {
line.starts_with("# ci.db.master.ip=") || line.starts_with("# ci.db.master.port=")
}
// fn comment_out_lines(input_path: &str, output_path: &str) -> io::Result<()> {
// let file = File::open(input_path)?;
// let reader = BufReader::new(file);
//
// let modified_lines: Vec<String> = reader
// .lines()
// .map(|line| {
// let line_content = line?;
// if should_comment_out(&line_content) {
// format!("# {}", line_content)
// } else {
// line_content
// }
// })
// .collect();
//
// // Write the modified content back to the output file
// let mut output_file = File::create(output_path)?;
// for line in modified_lines {
// writeln!(output_file, "{}", line)?;
// }
//
// Ok(())
// }
//
// fn comment_in_lines(file_path: &str) -> io::Result<()> {
// let file = File::open(file_path)?;
// let reader = BufReader::new(file);
//
// let modified_lines: Vec<String> = reader
// .lines()
// .map(|line| {
// let line_content = line?;
// if should_commented_out(&line_content) {
// format!("{}", &line_content[2..]) // Uncomment by removing the leading "# "
// } else {
// line_content
// }
// })
// .collect();
//
// // Write the modified content back to the original file
// let mut output_file = File::create(file_path)?;
// for line in modified_lines {
// writeln!(output_file, "{}", line)?;
// }
//
// Ok(())
// }
+3 -6
View File
@@ -1,25 +1,22 @@
use container::docker::start_docker_compose;
use env::config::{load_config, DevToolsConf};
use crate::arguments::Arguments;
use crate::{arguments::Arguments, config::parse::set_local_db};
use clap::Parser;
mod arguments;
mod config;
mod container;
mod env;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// let args: Vec<_> = std::env::args().collect();
// if args.len() < 2 {
// eprintln!("Up or down is a required argument");
// std::process::exit(1);
// }
let args: Arguments = Arguments::parse();
args.validate()?;
let config: DevToolsConf = load_config()?;
let start: bool = args.action == "up";
start_docker_compose(&config.container_service, &config.container_dir, start)?;
set_local_db(start)?;
println!(
"Current directory: {}",