Introduce a new config struct the lives above the raw disk implementation.
This should let us include values that would not be written to disk like the folder containing the config.
This commit is contained in:
@@ -1,25 +1,22 @@
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
|
||||
use crate::cli::parameters::InitArgs;
|
||||
use crate::config::Config;
|
||||
|
||||
pub(crate) async fn init_writer_folder(args: &InitArgs) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let new_config = Config::default();
|
||||
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() {
|
||||
std::fs::create_dir_all(&args.path)?;
|
||||
tokio::fs::create_dir_all(&args.path).await?;
|
||||
}
|
||||
|
||||
let existing_entries = std::fs::read_dir(&args.path)?;
|
||||
if existing_entries.count() != 0 {
|
||||
let mut existing_entries = tokio::fs::read_dir(&args.path).await?;
|
||||
let first_entry = existing_entries.next_entry().await?;
|
||||
if let Some(_) = first_entry {
|
||||
return Err("The directory is not empty. Aborting.".into());
|
||||
}
|
||||
|
||||
let mut config_file = File::create(args.path.join("writer.toml"))?;
|
||||
config_file.write_all(toml::to_string(&new_config)?.as_bytes())?;
|
||||
let new_config = Config::new(&args.path)?;
|
||||
new_config.write_to_disk().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user