Skip to content

Commit

Permalink
set default path to current work directory
Browse files Browse the repository at this point in the history
  • Loading branch information
chanmaoganda committed Jul 18, 2024
1 parent e275729 commit 246316c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
29 changes: 27 additions & 2 deletions command_binary/src/command.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{fs::create_dir, path::PathBuf};

use std::path::PathBuf;

use anyhow::Error;
use clap::Parser;
use glob::glob;

Expand Down Expand Up @@ -29,4 +29,29 @@ impl Command {
}
Ok(items)
}

pub fn get_output_path(&self) -> anyhow::Result<PathBuf> {
if let Some(path) = &self.output_path {
return Ok(PathBuf::from(path));
}
let default_paths = vec![
PathBuf::from("output"),
PathBuf::from("out"),
PathBuf::from("dump"),
];
for path in default_paths {
if dir_check_and_create(&path) {
return Ok(path);
}
}
Err(Error::msg("No output path provided and default paths are occupied"))
}
}

fn dir_check_and_create(path: &PathBuf) -> bool {
if !path.exists() {
create_dir(path).unwrap();
return true;
}
path.is_dir()
}
5 changes: 4 additions & 1 deletion command_binary/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ use music_dump_lib::NcmDumper;
fn main() -> anyhow::Result<()> {
let command = Command::parse();
let music_list = command.get_items()?;
let output_directory = PathBuf::from(command.output_path.unwrap());
let output_directory = PathBuf::from(command.get_output_path()?);
if !output_directory.exists() {
return Err(anyhow::Error::msg("Output directory does not exist"))
}
let ncm_dumper = NcmDumper::new(music_list, output_directory);
ncm_dumper.dump_all()?;
Ok(())
Expand Down

0 comments on commit 246316c

Please sign in to comment.