extract pty writer

This commit is contained in:
Nikita Galaiko 2023-06-05 10:21:52 +02:00
parent de30d69128
commit 6167ee0a31
4 changed files with 42 additions and 25 deletions

View File

@ -496,10 +496,7 @@ impl App {
) )
.context("failed to open repository")?; .context("failed to open repository")?;
let session = gb_repository let pty_writer = pty::Writer::new(&gb_repository)?;
.get_or_create_current_session()
.context("failed to get session")?;
let writer = sessions::Writer::open(&gb_repository, &session)?;
let timestamp = std::time::SystemTime::now() let timestamp = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH) .duration_since(std::time::UNIX_EPOCH)
@ -512,7 +509,7 @@ impl App {
bytes: bytes.to_vec(), bytes: bytes.to_vec(),
}; };
writer.append_pty(&record).context("failed to append pty")?; pty_writer.write(&record).context("failed to append pty")?;
Ok(()) Ok(())
} }

View File

@ -1,6 +1,8 @@
mod connection; mod connection;
mod recorder; mod recorder;
mod server; mod server;
mod writer;
pub use recorder::{Record, Type}; pub use recorder::{Record, Type};
pub use server::start_server; pub use server::start_server;
pub use writer::PtyWriter as Writer;

View File

@ -0,0 +1,37 @@
use anyhow::{Context, Result};
use crate::gb_repository;
use super::Record;
pub struct PtyWriter<'writer> {
repository: &'writer gb_repository::Repository,
}
impl<'writer> PtyWriter<'writer> {
pub fn new(repository: &'writer gb_repository::Repository) -> Result<Self> {
repository
.get_or_create_current_session()
.context("failed to create session")?;
Ok(Self { repository })
}
pub fn write(&self, record: &Record) -> Result<()> {
self.repository.lock()?;
defer! {
self.repository.unlock().expect("failed to unlock");
}
serde_jsonlines::append_json_lines(
self.repository.session_path().join("pty.jsonl"),
[record],
)?;
log::info!(
"{}: appended pty record to session",
self.repository.project_id
);
Ok(())
}
}

View File

@ -3,7 +3,7 @@ use std::time;
use anyhow::{anyhow, Context, Result}; use anyhow::{anyhow, Context, Result};
use crate::{ use crate::{
bookmarks, gb_repository, pty, bookmarks, gb_repository,
reader::{self, Reader}, reader::{self, Reader},
writer::{self, Writer}, writer::{self, Writer},
}; };
@ -127,25 +127,6 @@ impl<'writer> SessionWriter<'writer> {
Ok(writer) Ok(writer)
} }
pub fn append_pty(&self, record: &pty::Record) -> Result<()> {
self.repository.lock()?;
defer! {
self.repository.unlock().expect("failed to unlock");
}
serde_jsonlines::append_json_lines(
self.repository.session_path().join("pty.jsonl"),
[record],
)?;
log::info!(
"{}: appended pty record to session",
self.repository.project_id
);
Ok(())
}
pub fn write_session_wd_file<P: AsRef<std::path::Path>>( pub fn write_session_wd_file<P: AsRef<std::path::Path>>(
&self, &self,
path: P, path: P,