summary history branches tags files
commit:0899d5146cccceb375e04e87a0261dbc9377a9d3
author:Trevor Bentley
committer:Trevor Bentley
date:Sat Jan 14 20:30:33 2023 +0100
parents:6d301831b96d7e228703ac7a6093687e0862a137
command-line argument to clean output
diff --git a/src/generate.rs b/src/generate.rs
line changes: +7/-2
index 9be5c16..16019c1
--- a/src/generate.rs
+++ b/src/generate.rs
@@ -9,7 +9,7 @@ use crate::{
 use git2::{Error, Repository};
 use rayon::prelude::*;
 use std::cmp;
-use std::fs::{create_dir, File};
+use std::fs::File;
 use std::io::Write;
 use std::path::{Path, PathBuf};
 use std::sync::atomic::{AtomicUsize, Ordering};
@@ -182,7 +182,12 @@ impl GitsyGenerator {
         let tera = self.tera_init()?;
 
         // 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 mut global_bytes = 0;

diff --git a/src/settings.rs b/src/settings.rs
line changes: +21/-2
index a8ef62e..5991b17
--- a/src/settings.rs
+++ b/src/settings.rs
@@ -3,7 +3,7 @@ use crate::git::{GitFile, GitObject, GitRepo};
 use clap::Parser;
 use serde::Deserialize;
 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::path::{Path, PathBuf};
 use std::sync::atomic::Ordering;
@@ -28,8 +28,11 @@ struct CliArgs {
     #[arg(short, long)]
     local: bool,
     /// Open browser to repository listing after generation.
-    #[arg(short, long)]
+    #[arg(long)]
     open: bool,
+    /// Remove output directory before generating.
+    #[arg(long)]
+    clean: bool,
     /// Don't show any output, except errors and warnings
     #[arg(short, long)]
     quiet: bool,
@@ -43,6 +46,7 @@ pub struct GitsyCli {
     pub dir: PathBuf,
     pub is_local: bool,
     pub should_open: bool,
+    pub should_clean: bool,
     pub repos: Vec<PathBuf>,
 }
 
@@ -78,6 +82,7 @@ impl GitsyCli {
             dir: config_dir,
             is_local: cli.local,
             should_open: cli.open,
+            should_clean: cli.clean,
             repos: cli.repo,
         }
     }
@@ -194,6 +199,20 @@ impl GitsySettingsOutputs {
             .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 {
         let path_buf = PathBuf::from(path);
         path_buf.strip_prefix(self.output_dir())