Skip to content

Commit

Permalink
decent config formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
BrettMayson committed Sep 10, 2024
1 parent 4187933 commit ab488aa
Show file tree
Hide file tree
Showing 6 changed files with 609 additions and 1 deletion.
2 changes: 2 additions & 0 deletions bin/src/commands/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub fn cli() -> Command {
.about("Use HEMTT standalone utils")
.subcommand_required(false)
.arg_required_else_help(true)
.subcommand(utils::config::cli())
.subcommand(utils::inspect::cli())
.subcommand(utils::paa::cli())
.subcommand(utils::pbo::cli())
Expand All @@ -21,6 +22,7 @@ pub fn cli() -> Command {
/// [`Error`] depending on the modules
pub fn execute(matches: &ArgMatches) -> Result<Report, Error> {
match matches.subcommand() {
Some(("config", matches)) => utils::config::execute(matches),
Some(("inspect", matches)) => utils::inspect::execute(matches),
Some(("paa", matches)) => utils::paa::execute(matches),
Some(("pbo", matches)) => utils::pbo::execute(matches),
Expand Down
65 changes: 65 additions & 0 deletions bin/src/utils/config/fmt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use std::{
path::{Path, PathBuf},
sync::{atomic::AtomicUsize, Arc},
};

use clap::{ArgMatches, Command};
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};

use crate::Error;

#[must_use]
pub fn cli() -> Command {
Command::new("fmt").about("Format the config files").arg(
clap::Arg::new("path")
.help("Path to the config file or a folder to recursively fix")
.required(true),
)
}

/// Execute the convert command
///
/// # Errors
/// [`Error`] depending on the modules
pub fn execute(matches: &ArgMatches) -> Result<(), Error> {
let path = PathBuf::from(matches.get_one::<String>("path").expect("required"));
if path.is_dir() {
let count = Arc::new(AtomicUsize::new(0));
let entries = walkdir::WalkDir::new(&path)
.into_iter()
.collect::<Result<Vec<_>, _>>()?;
entries
.par_iter()
.map(|entry| {
if entry.file_type().is_file()
&& entry.path().extension().unwrap_or_default() == "cpp"
&& entry.path().extension().unwrap_or_default() == "hpp"
&& file(entry.path())?
{
count.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
info!("Format `{}`", entry.path().display());
}
Ok(())
})
.collect::<Result<Vec<_>, Error>>()?;
info!(
"Format {} files",
count.load(std::sync::atomic::Ordering::Relaxed)
);
} else if file(&path)? {
info!("Format `{}`", path.display());
} else {
info!("No changes in `{}`", path.display());
}
Ok(())
}

fn file(path: &Path) -> Result<bool, Error> {
// TODO do not release this lmao
let content = std::fs::read_to_string(path)?;
println!(
"{}",
hemtt_config::fmt::format(&content).expect("errors later")
);
Ok(true)
}
28 changes: 28 additions & 0 deletions bin/src/utils/config/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
mod fmt;

use clap::{ArgMatches, Command};

use crate::Error;

#[must_use]
pub fn cli() -> Command {
Command::new("config")
.about("Commands for Config files")
.arg_required_else_help(true)
.subcommand(fmt::cli())
}

/// Execute the paa command
///
/// # Errors
/// [`Error`] depending on the modules
///
/// # Panics
/// If the args are not present from clap
pub fn execute(matches: &ArgMatches) -> Result<(), Error> {
match matches.subcommand() {
Some(("fmt", matches)) => fmt::execute(matches),

_ => unreachable!(),
}
}
1 change: 1 addition & 0 deletions bin/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod config;
pub mod inspect;
pub mod paa;
pub mod pbo;
Expand Down
Loading

0 comments on commit ab488aa

Please sign in to comment.