added set dot env

main
Mathias Rothenhaeusler 2024-01-22 19:30:27 +01:00
parent 708ae57d1f
commit 1993aca4f2
2 changed files with 62 additions and 50 deletions

View File

@ -31,6 +31,12 @@ pub fn set_local_db(start: bool) -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}
pub fn set_dot_env(start: bool) -> Result<(), Box<dyn std::error::Error>> {
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<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(())
// }
fn toggle_after_line(
target_line_prefix: &str,
num_lines: usize,
activate: bool,
) -> Result<(), Box<dyn std::error::Error>> {
let file: File = File::open(LOCAL_CONNECTION_PATH)
.with_context(|| format!("could not read file `{}`", LOCAL_CONNECTION_PATH))?;
let reader: BufReader<File> = BufReader::new(file);
let mut modified_lines: Vec<String> = 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()
}

View File

@ -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<dyn std::error::Error>> {
start_docker_compose(&config.container_service, &config.container_dir, start)?;
set_local_db(start)?;
set_dot_env(start)?;
println!(
"Current directory: {}",