From 708ae57d1fc74cde210217442936532942f748c7 Mon Sep 17 00:00:00 2001 From: mace Date: Sun, 21 Jan 2024 18:45:34 +0100 Subject: [PATCH] Added Parser --- .gitignore | 1 + Cargo.lock | 7 ++++ Cargo.toml | 1 + src/config/mod.rs | 1 + src/config/parse.rs | 90 +++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 9 ++--- 6 files changed, 103 insertions(+), 6 deletions(-) create mode 100644 src/config/mod.rs create mode 100644 src/config/parse.rs diff --git a/.gitignore b/.gitignore index ea8c4bf..487bfb5 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +/conf diff --git a/Cargo.lock b/Cargo.lock index 035b887..72ecbbe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -50,6 +50,12 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "anyhow" +version = "1.0.79" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "080e9890a082662b09c1ad45f567faeeb47f22b5fb23895fbe1e651e718e25ca" + [[package]] name = "bitflags" version = "1.3.2" @@ -118,6 +124,7 @@ checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" name = "dev_tools" version = "0.1.0" dependencies = [ + "anyhow", "clap", "directories", "dotenv", diff --git a/Cargo.toml b/Cargo.toml index 77186ff..95abb12 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +anyhow = "1.0.79" clap = { version = "4.4.18", features = ["derive"] } directories = "5.0.1" dotenv = "0.15.0" diff --git a/src/config/mod.rs b/src/config/mod.rs new file mode 100644 index 0000000..ea86848 --- /dev/null +++ b/src/config/mod.rs @@ -0,0 +1 @@ +pub mod parse; diff --git a/src/config/parse.rs b/src/config/parse.rs new file mode 100644 index 0000000..cb848d7 --- /dev/null +++ b/src/config/parse.rs @@ -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> { + let file: File = File::open(GLOBAL_CONNECTION_PATH) + .with_context(|| format!("could not read file `{}`", GLOBAL_CONNECTION_PATH))?; + let reader: BufReader = BufReader::new(file); + + let modified_lines: Vec = 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 = 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 = 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(()) +// } diff --git a/src/main.rs b/src/main.rs index 345c95d..22b9554 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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> { - // 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: {}",