Skip to content

Commit

Permalink
feat(fix-cmd): add support to walk through file repository
Browse files Browse the repository at this point in the history
  • Loading branch information
Joxit committed Oct 3, 2023
1 parent db79e1c commit edbabb1
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions src/commands/fix.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::commands::export::Export;
use crate::fix::Fix;
use crate::repo::Walk;
use crate::utils::ResultExit;
use log::error;
use std::io::{Read, Write};
use std::fs::File;
use std::io::{stdin, stdout, Read};
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
Expand All @@ -20,7 +20,7 @@ impl FixCommand {
if crate::commands::input_pipe() {
loop {
let mut input = String::new();
match std::io::stdin().read_line(&mut input) {
match stdin().read_line(&mut input) {
Ok(0) => break,
Ok(_) => {
input = input.trim().to_string();
Expand All @@ -30,12 +30,35 @@ impl FixCommand {
let mut json_value =
crate::parse_string_to_json(&input).expect_exit("Malformed json object");
fix.fix(&mut json_value);
crate::json_to_writer(&json_value, &mut std::io::stdout()).exit_silently();
crate::json_to_writer(&json_value, &mut stdout()).exit_silently();
println!();
}
Err(_) => break,
}
}
}

for directory in &self.directories {
self.walk_directory(&fix, directory);
}
}

fn walk_directory(&self, fix: &Fix, directory: &String) -> Result<(), String> {
for entry in Walk::new(directory.to_string(), true, true) {
if let Ok(path) = entry {
let mut file =
File::open(path.path()).expect_exit(format!("Cannot open file {:?}", path).as_str());
let mut buffer = String::new();
file.read_to_string(&mut buffer).exit_silently();
let mut json = crate::parse_string_to_json(&buffer).exit_silently();
if fix.fix(&mut json)? {
let mut file = File::create(path.path())
.expect_exit(format!("Cannot create file {:?}", path).as_str());
crate::ser::json_to_writer(&json, &mut file)
.expect_exit(format!("Cannot write to file {:?}", path).as_str());
}
}
}
Ok(())
}
}

0 comments on commit edbabb1

Please sign in to comment.