diff --git a/src/config/parse.rs b/src/config/parse.rs index cb848d7..b8057a0 100644 --- a/src/config/parse.rs +++ b/src/config/parse.rs @@ -31,6 +31,12 @@ pub fn set_local_db(start: bool) -> Result<(), Box> { Ok(()) } +pub fn set_dot_env(start: bool) -> Result<(), Box> { + toggle_after_line("#docker", 4, start)?; + toggle_after_line("#cidb", 2, !start)?; + Ok(()) +} + fn connections_env_comment(line: &str) -> bool { line.starts_with("ci.db.master.ip=") || line.starts_with("ci.db.master.port=") } @@ -39,52 +45,54 @@ 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(()) -// } +fn toggle_after_line( + target_line_prefix: &str, + num_lines: usize, + activate: bool, +) -> Result<(), Box> { + let file: File = File::open(LOCAL_CONNECTION_PATH) + .with_context(|| format!("could not read file `{}`", LOCAL_CONNECTION_PATH))?; + let reader: BufReader = BufReader::new(file); + + let mut modified_lines: Vec = Vec::new(); + let mut relevant = false; + let mut counter: usize = 0; + + for line in reader.lines() { + let line_content = line?; + + if counter == num_lines { + relevant = false; + } + if line_content.trim_start().starts_with(target_line_prefix) { + relevant = true; + modified_lines.push(line_content); + continue; + } + + if relevant { + modified_lines.push(toggle(&line_content, activate)); + counter += 1; + } else { + modified_lines.push(line_content); + } + } + + // Write the modified content back to the output file + fs::write(LOCAL_CONNECTION_PATH, modified_lines.join("\n")).expect(""); + + Ok(()) +} + +fn toggle(line: &str, activate: bool) -> String { + dbg!(line, activate); + if activate { + return line.replacen('#', "", 1); + } + + if !line.starts_with('#') { + return format!("#{}", line); + } + + line.to_string() +} diff --git a/src/main.rs b/src/main.rs index 22b9554..f7e957e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,10 @@ use container::docker::start_docker_compose; use env::config::{load_config, DevToolsConf}; -use crate::{arguments::Arguments, config::parse::set_local_db}; +use crate::{ + arguments::Arguments, + config::parse::{set_dot_env, set_local_db}, +}; use clap::Parser; mod arguments; @@ -17,6 +20,7 @@ fn main() -> Result<(), Box> { start_docker_compose(&config.container_service, &config.container_dir, start)?; set_local_db(start)?; + set_dot_env(start)?; println!( "Current directory: {}",