natter/src/command/init/runner.rs

26 lines
803 B
Rust
Raw Normal View History

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)?;
}
let existing_entries = std::fs::read_dir(&args.path)?;
if existing_entries.count() != 0 {
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())?;
Ok(())
}