feat: add command line options
This commit is contained in:
parent
1e811d61ed
commit
140a7cc71f
1 changed files with 36 additions and 4 deletions
38
src/main.rs
38
src/main.rs
|
@ -11,9 +11,40 @@ use filesorters::Config;
|
||||||
|
|
||||||
static CONFIG: OnceLock<Config> = OnceLock::new();
|
static CONFIG: OnceLock<Config> = OnceLock::new();
|
||||||
|
|
||||||
|
const VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let config_path = get_config_path();
|
let config_path = get_config_path();
|
||||||
|
|
||||||
|
let args: Vec<String> = env::args().collect();
|
||||||
|
|
||||||
|
if args.len() > 1
|
||||||
|
&& (args.contains(&"-v".to_string()) || args.contains(&"--version".to_string()))
|
||||||
|
{
|
||||||
|
println!("Filesorters version {}", VERSION);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if args.len() > 1
|
||||||
|
&& (args.contains(&"-c".to_string()) || args.contains(&"--config".to_string()))
|
||||||
|
{
|
||||||
|
println!("Config location: {}", config_path.display());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if args.len() > 1 && (args.contains(&"-h".to_string()) || args.contains(&"--help".to_string()))
|
||||||
|
{
|
||||||
|
print!(
|
||||||
|
"Options:
|
||||||
|
-h, --help - show this help message
|
||||||
|
-v, --version - show the version
|
||||||
|
-c, --config-path - config file location
|
||||||
|
"
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if !config_path.exists() {
|
if !config_path.exists() {
|
||||||
println!("Config file does not exist, creating...");
|
println!("Config file does not exist, creating...");
|
||||||
filesorters::Config::create(&config_path).unwrap();
|
filesorters::Config::create(&config_path).unwrap();
|
||||||
|
@ -195,8 +226,8 @@ fn move_file_to_directory(path: &Path, dir: &Path) {
|
||||||
|
|
||||||
if !dir.exists() {
|
if !dir.exists() {
|
||||||
match fs::create_dir(dir) {
|
match fs::create_dir(dir) {
|
||||||
Ok(_) => {},
|
Ok(_) => {}
|
||||||
Err(err) => eprintln!("Error creating destination directory: {}", err)
|
Err(err) => eprintln!("Error creating destination directory: {}", err),
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -204,11 +235,12 @@ fn move_file_to_directory(path: &Path, dir: &Path) {
|
||||||
let destination = dir.join(path.file_name().unwrap_or_default());
|
let destination = dir.join(path.file_name().unwrap_or_default());
|
||||||
|
|
||||||
match fs::rename(path, &destination) {
|
match fs::rename(path, &destination) {
|
||||||
Ok(()) => {},
|
Ok(()) => {}
|
||||||
Err(err) => eprintln!("Error moving file: {}", err),
|
Err(err) => eprintln!("Error moving file: {}", err),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
fn get_config_path() -> PathBuf {
|
fn get_config_path() -> PathBuf {
|
||||||
if cfg!(target_os = "windows") {
|
if cfg!(target_os = "windows") {
|
||||||
let username = env::var("USERNAME").unwrap_or_else(|_| "Default".to_string());
|
let username = env::var("USERNAME").unwrap_or_else(|_| "Default".to_string());
|
||||||
|
|
Loading…
Reference in a new issue