Add init command
This commit is contained in:
parent
5ce8b6b2ec
commit
77f24f3f54
4 changed files with 29 additions and 0 deletions
|
@ -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)]
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
pub mod editor;
|
||||
pub mod fetch;
|
||||
pub mod query;
|
||||
pub mod init;
|
||||
|
|
18
src/editor/commands/init.rs
Normal file
18
src/editor/commands/init.rs
Normal 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");
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue