Writing the writer.toml to a folder when running init.

This commit is contained in:
Tom Alexander 2023-10-18 20:21:28 -04:00
parent 5f82d7a469
commit 933387eb6f
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
4 changed files with 37 additions and 1 deletions

View File

@ -0,0 +1,3 @@
mod runner;
pub(crate) use runner::init_writer_folder;

View File

@ -0,0 +1,25 @@
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(())
}

View File

@ -6,3 +6,8 @@ use serde::Serialize;
pub(crate) struct Config {
foo: String,
}
impl Default for Config {
fn default() -> Self {
Config { foo: "".to_owned() }
}
}

View File

@ -4,6 +4,7 @@ use clap::Parser;
use self::cli::parameters::Cli;
use self::cli::parameters::Commands;
use self::command::init::init_writer_folder;
mod cli;
mod command;
mod config;
@ -19,7 +20,9 @@ fn main() -> Result<ExitCode, Box<dyn std::error::Error>> {
async fn main_body() -> Result<ExitCode, Box<dyn std::error::Error>> {
let args = Cli::parse();
match args.command {
Commands::Init(_args) => {}
Commands::Init(args) => {
init_writer_folder(&args).await?;
}
Commands::Build(_args) => {}
Commands::AddPost(_args) => {}
};