2023-10-18 20:21:28 -04:00
|
|
|
use crate::cli::parameters::InitArgs;
|
|
|
|
use crate::config::Config;
|
2023-10-22 13:44:03 -04:00
|
|
|
use crate::error::CustomError;
|
2023-10-18 20:21:28 -04:00
|
|
|
|
2023-12-21 19:28:31 -05:00
|
|
|
pub(crate) async fn init_natter_folder(args: InitArgs) -> Result<(), CustomError> {
|
2023-10-18 20:21:28 -04:00
|
|
|
if args.path.exists() && !args.path.is_dir() {
|
|
|
|
return Err("The supplied path exists but is not a directory. Aborting.".into());
|
|
|
|
}
|
|
|
|
|
|
|
|
if !args.path.exists() {
|
2023-10-18 21:25:37 -04:00
|
|
|
tokio::fs::create_dir_all(&args.path).await?;
|
2023-10-18 20:21:28 -04:00
|
|
|
}
|
|
|
|
|
2023-10-18 21:25:37 -04:00
|
|
|
let mut existing_entries = tokio::fs::read_dir(&args.path).await?;
|
|
|
|
let first_entry = existing_entries.next_entry().await?;
|
|
|
|
if let Some(_) = first_entry {
|
2023-10-18 20:21:28 -04:00
|
|
|
return Err("The directory is not empty. Aborting.".into());
|
|
|
|
}
|
|
|
|
|
2023-10-20 19:13:22 -04:00
|
|
|
let new_config = Config::new(args.path)?;
|
2023-10-18 21:25:37 -04:00
|
|
|
new_config.write_to_disk().await?;
|
2023-10-18 20:21:28 -04:00
|
|
|
Ok(())
|
|
|
|
}
|