Add init command

This commit is contained in:
Karcsesz 2024-02-14 20:55:28 +01:00
parent 5ce8b6b2ec
commit 77f24f3f54
4 changed files with 29 additions and 0 deletions

View file

@ -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)]

View file

@ -1,3 +1,4 @@
pub mod editor;
pub mod fetch;
pub mod query;
pub mod init;

View file

@ -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");
}

View file

@ -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);
}
}
}