feat: update hyperlog
All checks were successful
continuous-integration/drone/push Build is passing

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
Kasper Juul Hermansen 2024-11-23 14:26:11 +01:00
parent ee0680194b
commit 34fba9754c
Signed by: kjuulh
GPG Key ID: D85D7535F18F35FA
6 changed files with 77 additions and 25 deletions

View File

@ -221,6 +221,7 @@ impl<'a> App<'a> {
} }
fn open_editor(&self, item: &GraphItem) -> Option<Command> { fn open_editor(&self, item: &GraphItem) -> Option<Command> {
tracing::info!("entering editor for session");
match editor::EditorSession::new(item).execute() { match editor::EditorSession::new(item).execute() {
Ok(None) => { Ok(None) => {
tracing::info!("editor returned without changes, skipping"); tracing::info!("editor returned without changes, skipping");

View File

@ -7,12 +7,10 @@ use crate::{
#[allow(dead_code)] #[allow(dead_code)]
pub struct State { pub struct State {
engine: SharedEngine,
pub storage: Storage,
events: Events,
pub commander: Commander, pub commander: Commander,
pub querier: Querier, pub querier: Querier,
backend: Backend,
} }
pub enum Backend { pub enum Backend {
@ -22,19 +20,21 @@ pub enum Backend {
impl State { impl State {
pub async fn new(backend: Backend) -> anyhow::Result<Self> { pub async fn new(backend: Backend) -> anyhow::Result<Self> {
let storage = Storage::new(); let (querier, commander) = match &backend {
let engine = storage.load()?; Backend::Local => {
let events = Events::default(); let storage = Storage::new();
let engine = SharedEngine::from(engine); let engine = storage.load()?;
let events = Events::default();
let (querier, commander) = match backend { let engine = SharedEngine::from(engine);
Backend::Local => ( (
Querier::local(&engine), Querier::local(&engine),
Commander::local(engine.clone(), storage.clone(), events.clone())?, Commander::local(engine.clone(), storage.clone(), events.clone())?,
), )
}
Backend::Remote { url } => { Backend::Remote { url } => {
let channel = Channel::from_shared(url)? let tls = ClientTlsConfig::new();
.tls_config(ClientTlsConfig::new())? let channel = Channel::from_shared(url.clone())?
.tls_config(tls.with_native_roots())?
.connect() .connect()
.await?; .await?;
@ -46,12 +46,25 @@ impl State {
}; };
Ok(Self { Ok(Self {
engine: engine.clone(),
storage: storage.clone(),
events: events.clone(),
commander, commander,
querier, querier,
backend,
}) })
} }
pub fn unlock(&self) {
if let Backend::Local = &self.backend {
let storage = Storage::new();
storage.clear_lock_file();
}
}
pub fn info(&self) -> Option<anyhow::Result<String>> {
if let Backend::Local = &self.backend {
let storage = Storage::new();
return Some(storage.info());
}
None
}
} }

View File

@ -5,6 +5,10 @@ use std::{
}; };
use anyhow::{anyhow, Context}; use anyhow::{anyhow, Context};
use crossterm::{
terminal::{disable_raw_mode, enable_raw_mode},
ExecutableCommand,
};
use hyperlog_core::log::{GraphItem, ItemState}; use hyperlog_core::log::{GraphItem, ItemState};
use itertools::Itertools; use itertools::Itertools;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -127,6 +131,11 @@ impl SessionFile {
impl Drop for SessionFile { impl Drop for SessionFile {
fn drop(&mut self) { fn drop(&mut self) {
// std::io::stdout()
// .execute(crossterm::terminal::EnterAlternateScreen)
// .expect("to be able to restore alternative mode");
// enable_raw_mode().expect("to be able to restore raw mode");
if self.path.exists() { if self.path.exists() {
tracing::debug!("cleaning up file: {}", self.path.display()); tracing::debug!("cleaning up file: {}", self.path.display());
@ -180,6 +189,7 @@ impl<'a> EditorSession<'a> {
.as_bytes(), .as_bytes(),
) )
.context("failed to write to file")?; .context("failed to write to file")?;
file.flush().context("failed to flush to disk")?;
let modified_time = file.metadata()?.modified()?; let modified_time = file.metadata()?.modified()?;
@ -209,7 +219,26 @@ impl<'a> EditorSession<'a> {
editor, editor,
session_file.get_path().display() session_file.get_path().display()
); );
if let Err(e) = std::process::Command::new(editor)
std::io::stdout().flush()?;
// disable_raw_mode()?;
// std::io::stdout().execute(crossterm::terminal::LeaveAlternateScreen)?;
let path = session_file.get_path();
if let Some(parent) = path.parent() {
if let Err(e) = std::process::Command::new(editor)
.arg(
path.file_name()
.ok_or(anyhow::anyhow!("failed to find file in the given path"))?,
)
.current_dir(parent)
.status()
{
tracing::error!("failed command with: {}", e);
return Ok(None);
}
} else if let Err(e) = std::process::Command::new(editor)
.arg(session_file.get_path()) .arg(session_file.get_path())
.status() .status()
{ {
@ -238,7 +267,7 @@ trait ItemExt {
fn get_digest(&self) -> Option<String>; fn get_digest(&self) -> Option<String>;
} }
impl<'a> ItemExt for &'a GraphItem { impl ItemExt for &GraphItem {
fn get_digest(&self) -> Option<String> { fn get_digest(&self) -> Option<String> {
if let GraphItem::Item { title, .. } = self { if let GraphItem::Item { title, .. } = self {
let digest = sha2::Sha256::digest(title.as_bytes()); let digest = sha2::Sha256::digest(title.as_bytes());

View File

@ -31,7 +31,7 @@ pub mod state;
mod engine; mod engine;
mod events; mod events;
mod querier; mod querier;
mod storage; pub mod storage;
mod editor; mod editor;
mod logging; mod logging;

View File

@ -80,6 +80,13 @@ impl Storage {
pub fn clear_lock_file(self) { pub fn clear_lock_file(self) {
let mut lock_file = self.lock_file.lock().unwrap(); let mut lock_file = self.lock_file.lock().unwrap();
if let Ok(lock) = self.state_lock() {
if lock.exists() {
tracing::info!("clearing lock file");
std::fs::remove_file(&lock).expect("to be able to remove lockfile");
}
}
if lock_file.is_some() { if lock_file.is_some() {
*lock_file = None; *lock_file = None;
} }

View File

@ -164,11 +164,13 @@ pub async fn execute() -> anyhow::Result<()> {
} }
Some(Commands::Info {}) => { Some(Commands::Info {}) => {
let state = State::new(backend).await?; let state = State::new(backend).await?;
println!("graph stored at: {}", state.storage.info()?) if let Some(info) = state.info() {
println!("graph stored at: {}", info?);
}
} }
Some(Commands::ClearLock {}) => { Some(Commands::ClearLock {}) => {
let state = State::new(backend).await?; let state = State::new(backend).await?;
state.storage.clear_lock_file(); state.unlock();
println!("cleared lock file"); println!("cleared lock file");
} }
None => { None => {