#[macro_use]
extern crate clap;
extern crate colored;
extern crate liboskar;
use clap::{App, AppSettings};
use colored::*;
use liboskar::prelude::*;
use std::env;
use std::path::PathBuf;
use std::process::Command;
#[allow(unknown_lints)]
#[allow(clippy::cognitive_complexity)]
#[allow(clippy::unreadable_literal)]
fn main() {
#[cfg(feature = "english")]
let yaml = load_yaml!("cli/options-en.yml");
#[cfg(feature = "francais")]
let yaml = load_yaml!("cli/options-fr.yml");
#[cfg(feature = "deutsch")]
let yaml = load_yaml!("cli/options-de.yml");
let matches = App::from_yaml(yaml)
.version(crate_version!())
.set_term_width(90)
.setting(AppSettings::SubcommandRequired)
.get_matches();
if let Some(x) = matches.subcommand_matches("update") {
let force = x.is_present("force");
println!("current version: {}", crate_version!());
let s = if force {
"curl -LSfs https://japaric.github.io/trust/install.sh | sh -s -- --git vmchale/tin-summer --force"
} else {
"curl -LSfs https://japaric.github.io/trust/install.sh | sh -s -- --git vmchale/tin-summer"
};
let script = Command::new("bash")
.arg("-c")
.arg(s)
.output()
.expect("failed to execute update script.");
let script_string = String::from_utf8(script.stderr).unwrap();
println!("{}", script_string);
} else if let Some(command) = matches.subcommand_matches("clean") {
let home_dir_str = match env::var("HOME") {
Ok(val) => val,
_ => "/home".to_string(),
};
let home_dir = PathBuf::from(home_dir_str);
let regex = command.value_of("excludes").map(|r| check_regex(r));
let vimtags = command.is_present("tags");
let dirs = get_dirs(command.values_of("dir"));
let force = command.is_present("force");
for dir in dirs {
if (dir != home_dir) && !force {
clean_project_dirs(dir, ®ex, vimtags);
} else {
eprintln!(
"{}: not cleaning directory '{}', as it is your home directory. To clean your home directory, rerun with --force.",
"Warning".yellow(),
dir.display()
)
}
}
}
else if let Some(command) = matches.subcommand_matches("parallel") {
let print_all = command.is_present("all");
let print_files = command.is_present("files");
let nproc = get_threads(command.value_of("threads"));
let min_bytes = threshold(command.value_of("threshold"));
let dirs = get_dirs(command.values_of("dir"));
let regex = command.value_of("excludes").map(|r| check_regex(r));
for dir in dirs {
let mut w = Walk::new(dir, nproc);
if let Some(b) = min_bytes {
w.set_threshold(b);
}
if !print_all {
let depth = get_depth(command.value_of("depth"));
w.set_depth(depth);
} else if command.is_present("depth") {
eprintln!(
"{}: flag --all is not compatible with --depth",
"Warning".yellow()
);
}
if print_files {
w.with_files();
}
if let Some(e) = regex.clone() {
w.set_regex(e);
}
print_parallel(w);
}
}
else if let Some(command) = matches.subcommand_matches("fat") {
let min_bytes = match threshold(command.value_of("threshold")) {
Some(t) => t,
_ => 31457280, };
let depth = if !command.is_present("all") {
Some(get_depth(command.value_of("depth")))
} else if command.is_present("depth") {
eprintln!(
"{}: flag --all is not compatible with --depth",
"Warning".yellow()
);
None
} else {
None
};
let print_files = command.is_present("files");
let regex = command.value_of("excludes");
let dirs = get_dirs(command.values_of("dir"));
for dir in dirs {
let v = match regex {
Some(r) => read_all(&dir, 0, depth, Some(&check_regex(r)), &None, false, false),
_ => read_all(&dir, 0, depth, None, &None, false, false),
};
let mut v_filtered = v.filtered(Some(min_bytes), !print_files, depth);
v_filtered.display_tree(&dir);
}
}
else if let Some(command) = matches.subcommand_matches("directories") {
let min_bytes = threshold(command.value_of("threshold"));
let depth = if !command.is_present("all") {
Some(get_depth(command.value_of("depth")))
} else if command.is_present("depth") {
eprintln!(
"{}: flag --all is not compatible with --depth",
"Warning".yellow()
);
None
} else {
None
};
let regex = command.value_of("excludes");
let print_files = command.is_present("files");
let dirs = get_dirs(command.values_of("dir"));
for dir in dirs {
let v = match regex {
Some(r) => read_all(&dir, 0, depth, Some(&check_regex(r)), &None, false, false),
_ => read_all_fast(&dir, 0, depth),
};
let mut v_filtered = v.filtered(min_bytes, !print_files, depth);
v_filtered.display_tree(&dir);
}
} else if let Some(command) = matches.subcommand_matches("files") {
let min_bytes = threshold(command.value_of("threshold"));
let depth = if !command.is_present("all") {
Some(get_depth(command.value_of("depth")))
} else if command.is_present("depth") {
eprintln!(
"{}: flag --all is not compatible with --depth",
"Warning".yellow()
);
None
} else {
None
};
let regex = command.value_of("excludes");
let print_files = true;
let dirs = get_dirs(command.values_of("dir"));
for dir in dirs {
let v = match regex {
Some(r) => read_all(&dir, 0, depth, Some(&check_regex(r)), &None, false, false),
_ => read_all(&dir, 0, depth, None, &None, false, false),
};
let mut v_filtered = v.filtered(min_bytes, !print_files, depth);
v_filtered.display_tree(&dir);
}
} else if let Some(command) = matches.subcommand_matches("artifacts") {
let min_bytes = threshold(command.value_of("threshold"));
let depth = if !command.is_present("all") {
Some(get_depth(command.value_of("depth")))
} else if command.is_present("depth") {
eprintln!(
"{}: flag --all is not compatible with --depth",
"Warning".yellow()
);
None
} else {
None
};
let num_int = if command.is_present("count") {
Some(get_num(command.value_of("count")))
} else {
None
};
let vimtags = command.is_present("tags");
let should_sort = command.is_present("sort");
let print_files = command.is_present("files");
let dirs = get_dirs(command.values_of("dir"));
for dir in dirs {
let excludes = get_excludes(command.value_of("excludes"));
let v = read_all(&dir, 0, depth, Some(&excludes), &None, vimtags, true);
let mut v_processed = if should_sort {
v.sort(num_int, min_bytes, !print_files, depth)
} else {
v.filtered(min_bytes, !print_files, depth)
};
v_processed.display_tree(&dir);
}
}
else if let Some(command) = matches.subcommand_matches("sort") {
let min_bytes = threshold(command.value_of("threshold"));
let depth = if !command.is_present("all") {
Some(get_depth(command.value_of("depth")))
} else if command.is_present("depth") {
eprintln!(
"{}: flag --all is not compatible with --depth",
"Warning".yellow()
);
None
} else {
None
};
let num_int = if command.is_present("count") {
Some(get_num(command.value_of("count")))
} else {
None
};
let print_files = command.is_present("files");
let dirs = get_dirs(command.values_of("dir"));
for dir in dirs {
let regex = if let Some(n) = command.value_of("excludes") {
Some(n)
} else {
None
};
let v = match regex {
Some(r) => read_all(&dir, 0, depth, Some(&check_regex(r)), &None, false, false),
_ => read_all(&dir, 0, depth, None, &None, false, false),
};
let mut v_sorted = v.sort(num_int, min_bytes, !print_files, depth);
v_sorted.display_tree(&dir);
}
}
}