--reinitialize param, logging

feat/vaults
Tomáš Mládek 2020-09-12 15:02:03 +02:00
parent 7baa91f179
commit fc7635bf70
2 changed files with 17 additions and 7 deletions

View File

@ -15,6 +15,7 @@ use crate::addressing::Address;
use crate::hash::{decode, encode, hash, Hash, Hashable};
use crate::models;
use serde::export::Formatter;
use std::fs;
#[derive(Debug, Clone)]
pub struct Entry {
@ -289,7 +290,7 @@ impl Handler<InsertEntry> for DbExecutor {
let connection = &self.0.get()?;
debug!("Inserting {:?}...", msg.entry);
debug!("Inserting: {}", msg.entry);
let insert_entry = models::Entry {
identity: msg.entry.hash()?.0,
@ -375,10 +376,15 @@ pub struct OpenResult {
pub new: bool,
}
pub fn open_upend<P: AsRef<Path>>(dirpath: P) -> Result<OpenResult> {
const DATABASE_FILENAME: &str = "upend.sqlite3";
pub fn open_upend<P: AsRef<Path>>(dirpath: P, reinitialize: bool) -> Result<OpenResult> {
embed_migrations!("./migrations/upend/");
let database_path: PathBuf = dirpath.as_ref().join("upend.sqlite3");
let database_path: PathBuf = dirpath.as_ref().join(DATABASE_FILENAME);
if reinitialize {
let _ = fs::remove_file(&database_path);
}
let new = !database_path.exists();
let manager = ConnectionManager::<SqliteConnection>::new(database_path.to_str().unwrap());

View File

@ -3,9 +3,9 @@ extern crate diesel;
#[macro_use]
extern crate diesel_migrations;
use std::env;
use std::net::SocketAddr;
use std::path::PathBuf;
use std::{env, fs};
use actix::prelude::*;
use actix_web::{middleware, App, HttpServer};
@ -41,6 +41,11 @@ fn main() -> std::io::Result<()> {
Arg::with_name("NO_BROWSER")
.long("no-browser")
.help("Do not open web browser with the UI."),
)
.arg(
Arg::with_name("REINITIALIZE")
.long("reinitialize")
.help("Delete and initialize database, if it exists already."),
);
let matches = app.get_matches();
@ -50,8 +55,8 @@ fn main() -> std::io::Result<()> {
let vault_path = PathBuf::from(matches.value_of("DIRECTORY").unwrap());
let _ = fs::remove_file(&vault_path.join("upend.sqlite3")); // TODO REMOVE!!!
let open_result = database::open_upend(&vault_path).expect("failed to open database!");
let open_result = database::open_upend(&vault_path, matches.is_present("REINITIALIZE"))
.expect("failed to open database!");
let pool = open_result.pool;
let db_addr = SyncArbiter::start(3, move || database::DbExecutor(pool.clone()));
@ -96,7 +101,6 @@ fn main() -> std::io::Result<()> {
));
}
// TODO REMOVE
if !matches.is_present("NO_BROWSER") {
let ui_result = webbrowser::open(&format!("http://localhost:{}", bind.port()));
if ui_result.is_err() {