command-line argument to clean output

This commit is contained in:
Trevor Bentley
2023-01-14 20:30:33 +01:00
parent 6d301831b9
commit 0899d5146c
2 changed files with 28 additions and 4 deletions
+7 -2
View File
@@ -9,7 +9,7 @@ use crate::{
use git2::{Error, Repository}; use git2::{Error, Repository};
use rayon::prelude::*; use rayon::prelude::*;
use std::cmp; use std::cmp;
use std::fs::{create_dir, File}; use std::fs::File;
use std::io::Write; use std::io::Write;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::atomic::{AtomicUsize, Ordering};
@@ -182,7 +182,12 @@ impl GitsyGenerator {
let tera = self.tera_init()?; let tera = self.tera_init()?;
// Create output directory // Create output directory
let _ = create_dir(self.settings.outputs.path.to_str().expect("Output path invalid.")); if self.cli.should_clean {
louder!("Cleaning output directory: {}", self.settings.outputs.path.display());
self.settings.outputs.clean();
}
louder!("Creating output directory: {}", self.settings.outputs.path.display());
self.settings.outputs.create();
let generated_dt = chrono::offset::Local::now(); let generated_dt = chrono::offset::Local::now();
let mut global_bytes = 0; let mut global_bytes = 0;
+21 -2
View File
@@ -3,7 +3,7 @@ use crate::git::{GitFile, GitObject, GitRepo};
use clap::Parser; use clap::Parser;
use serde::Deserialize; use serde::Deserialize;
use std::collections::{BTreeMap, HashMap, HashSet}; use std::collections::{BTreeMap, HashMap, HashSet};
use std::fs::{create_dir_all, read_dir, read_to_string}; use std::fs::{create_dir, create_dir_all, read_dir, remove_dir_all, read_to_string};
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::sync::atomic::Ordering; use std::sync::atomic::Ordering;
@@ -28,8 +28,11 @@ struct CliArgs {
#[arg(short, long)] #[arg(short, long)]
local: bool, local: bool,
/// Open browser to repository listing after generation. /// Open browser to repository listing after generation.
#[arg(short, long)] #[arg(long)]
open: bool, open: bool,
/// Remove output directory before generating.
#[arg(long)]
clean: bool,
/// Don't show any output, except errors and warnings /// Don't show any output, except errors and warnings
#[arg(short, long)] #[arg(short, long)]
quiet: bool, quiet: bool,
@@ -43,6 +46,7 @@ pub struct GitsyCli {
pub dir: PathBuf, pub dir: PathBuf,
pub is_local: bool, pub is_local: bool,
pub should_open: bool, pub should_open: bool,
pub should_clean: bool,
pub repos: Vec<PathBuf>, pub repos: Vec<PathBuf>,
} }
@@ -78,6 +82,7 @@ impl GitsyCli {
dir: config_dir, dir: config_dir,
is_local: cli.local, is_local: cli.local,
should_open: cli.open, should_open: cli.open,
should_clean: cli.clean,
repos: cli.repo, repos: cli.repo,
} }
} }
@@ -194,6 +199,20 @@ impl GitsySettingsOutputs {
.to_string() .to_string()
} }
pub fn create(&self) {
let _ = create_dir(self.path.to_str().expect(&format!("ERROR: output path invalid: {}", self.path.display())));
}
pub fn clean(&self) {
if !self.path.exists() {
return;
}
let dir: PathBuf = PathBuf::from(&self.output_dir());
assert!(dir.is_dir(), "ERROR: Output directory is... not a directory? {}", dir.display());
remove_dir_all(&dir)
.expect(&format!("ERROR: failed to clean output directory: {}", dir.display()));
}
pub fn to_relative(&self, path: &str) -> String { pub fn to_relative(&self, path: &str) -> String {
let path_buf = PathBuf::from(path); let path_buf = PathBuf::from(path);
path_buf.strip_prefix(self.output_dir()) path_buf.strip_prefix(self.output_dir())