Added Parser

main
Mathias Rothenhaeusler 2024-01-21 18:45:34 +01:00
parent 114a1accde
commit 708ae57d1f
6 changed files with 103 additions and 6 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
/target
/conf

7
Cargo.lock generated
View File

@ -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",

View File

@ -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"

View File

@ -0,0 +1 @@
pub mod parse;

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

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: {}",