Skip to content

Commit

Permalink
Only delete old projects (skip active projects) (#66)
Browse files Browse the repository at this point in the history
* add: age parsing function

* add: older option to Opt, skip directory if newer than value set in opt.older

* fix: return to previous state

* fix: moved code to main.rs as it adds to the cli not the lib, and made the sugested changes in code

* fix: linting corrections
  • Loading branch information
gabrielztk authored Nov 26, 2022
1 parent 725f7ec commit f3924f8
Showing 1 changed file with 70 additions and 7 deletions.
77 changes: 70 additions & 7 deletions kondo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use structopt::StructOpt;
use std::{
env::current_dir,
error::Error,
fmt,
io::{stdin, stdout, BufRead, Write},
num::ParseIntError,
path::PathBuf,
};

Expand Down Expand Up @@ -34,6 +36,10 @@ struct Opt {
/// Restrict directory traversal to the root filesystem
#[structopt(short, long)]
same_filesystem: bool,

/// Only directories with a file last modified n units of time ago will be looked at. Ex: 20d. Units are m: minutes, h: hours, d: days, w: weeks, M: months and y: years.
#[structopt(short, long, parse(try_from_str = parse_age_filter), default_value = "0d")]
older: u64,
}

fn prepare_directories(dirs: Vec<PathBuf>) -> Result<Vec<PathBuf>, Box<dyn Error>> {
Expand Down Expand Up @@ -68,6 +74,57 @@ fn prepare_directories(dirs: Vec<PathBuf>) -> Result<Vec<PathBuf>, Box<dyn Error
Ok(dirs)
}

#[derive(Debug)]
pub enum ParseAgeFilterError {
ParseIntError(ParseIntError),
InvalidUnit,
}

impl fmt::Display for ParseAgeFilterError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ParseAgeFilterError::ParseIntError(e) => e.fmt(f),
ParseAgeFilterError::InvalidUnit => {
"invalid age unit, must be one of m, h, d, w, M, y".fmt(f)
}
}
}
}

impl From<ParseIntError> for ParseAgeFilterError {
fn from(e: ParseIntError) -> Self {
Self::ParseIntError(e)
}
}

pub fn parse_age_filter(age_filter: &str) -> Result<u64, ParseAgeFilterError> {
const MINUTE: u64 = 60;
const HOUR: u64 = MINUTE * 60;
const DAY: u64 = HOUR * 24;
const WEEK: u64 = DAY * 7;
const MONTH: u64 = WEEK * 4;
const YEAR: u64 = MONTH * 12;

let (digit_end, unit) = age_filter
.char_indices()
.last()
.ok_or(ParseAgeFilterError::InvalidUnit)?;

let multiplier = match unit {
'm' => MINUTE,
'h' => HOUR,
'd' => DAY,
'w' => WEEK,
'M' => MONTH,
'y' => YEAR,
_ => return Err(ParseAgeFilterError::InvalidUnit),
};

let count = age_filter[..digit_end].parse::<u64>()?;
let seconds = count * multiplier;
Ok(seconds)
}

fn main() -> Result<(), Box<dyn Error>> {
let opt = Opt::from_args();

Expand Down Expand Up @@ -125,16 +182,22 @@ fn main() -> Result<(), Box<dyn Error>> {
continue;
}

if opt.quiet == 0 {
let mut last_modified_str = String::new();
let mut last_modified_str = String::new();
let mut last_modified_int: u64 = 0;

if let Ok(last_modified) = project.last_modified(&scan_options) {
if let Ok(elapsed) = last_modified.elapsed() {
let elapsed = print_elapsed(elapsed.as_secs());
last_modified_str = format!("({elapsed})");
}
if let Ok(last_modified) = project.last_modified(&scan_options) {
if let Ok(elapsed) = last_modified.elapsed() {
last_modified_int = elapsed.as_secs();
let elapsed = print_elapsed(last_modified_int);
last_modified_str = format!("({elapsed})");
}
}

if last_modified_int < opt.older {
continue;
}

if opt.quiet == 0 {
writeln!(
&mut write_handle,
"{} {} project {last_modified_str}{}",
Expand Down

0 comments on commit f3924f8

Please sign in to comment.