diff --git a/src/args_parser.rs b/src/args_parser.rs index d743397..9d3fa8d 100644 --- a/src/args_parser.rs +++ b/src/args_parser.rs @@ -102,6 +102,12 @@ pub enum Command { #[command(flatten)] server_reload: ServerReloadOptions, }, + /// Create a blank database + Init { + /// Force creating a new database even if one exists already. WILL DELETE DATA! + #[arg(long, short = 'f')] + force: bool + } } #[derive(ValueEnum, Debug, Eq, PartialEq, Copy, Clone)] diff --git a/src/editor/commands.rs b/src/editor/commands.rs index f83e586..1a82c5f 100644 --- a/src/editor/commands.rs +++ b/src/editor/commands.rs @@ -1,3 +1,4 @@ pub mod editor; pub mod fetch; pub mod query; +pub mod init; diff --git a/src/editor/commands/init.rs b/src/editor/commands/init.rs new file mode 100644 index 0000000..4305fd7 --- /dev/null +++ b/src/editor/commands/init.rs @@ -0,0 +1,18 @@ +use std::io::Write; +use std::path::PathBuf; +use tracing::{error, info, warn}; + +pub fn init(database_path: PathBuf, force: bool) { + if database_path.exists() { + warn!("Database already exists!"); + if force { + warn!("OVERWRITING DATABASE!"); + } else { + error!("Refusing to overwrite database"); + return; + } + } + let mut file = std::fs::File::create(database_path).unwrap(); + file.write_all(b"[]").unwrap(); + info!("Initialised empty database"); +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 623dca8..9c01e9a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ use clap::Parser; use editor::commands::fetch::fetch; use editor::commands::query::query; use tracing_subscriber::util::SubscriberInitExt; +use crate::editor::commands::init::init; mod args_parser; #[cfg(feature = "editor")] @@ -62,5 +63,8 @@ fn main() { reload(data_paths.pid_file_path, server_reload); } + Command::Init { force } => { + init(data_paths.database_path, force); + } } }