Don't initially create a tmpfile until it's needed
This commit is contained in:
parent
69094cb161
commit
51b18edb1c
|
@ -5,7 +5,7 @@ use std::{
|
||||||
time::{SystemTime, UNIX_EPOCH},
|
time::{SystemTime, UNIX_EPOCH},
|
||||||
};
|
};
|
||||||
|
|
||||||
use jaq_interpret::{ParseCtx, Error, Native, Val};
|
use jaq_interpret::{Error, Native, ParseCtx, Val};
|
||||||
use serde_json::{json, Value};
|
use serde_json::{json, Value};
|
||||||
use tracing::{debug, error, trace};
|
use tracing::{debug, error, trace};
|
||||||
|
|
||||||
|
|
|
@ -101,7 +101,7 @@ async fn init() -> Result<Args> {
|
||||||
async fn run_watchexec(args: Args) -> Result<()> {
|
async fn run_watchexec(args: Args) -> Result<()> {
|
||||||
info!(version=%env!("CARGO_PKG_VERSION"), "constructing Watchexec from CLI");
|
info!(version=%env!("CARGO_PKG_VERSION"), "constructing Watchexec from CLI");
|
||||||
|
|
||||||
let state = state::State::new()?;
|
let state = state::State::default();
|
||||||
let config = config::make_config(&args, &state)?;
|
let config = config::make_config(&args, &state)?;
|
||||||
config.filterer(WatchexecFilterer::new(&args).await?);
|
config.filterer(WatchexecFilterer::new(&args).await?);
|
||||||
|
|
||||||
|
|
|
@ -7,38 +7,34 @@ use std::{
|
||||||
use miette::{IntoDiagnostic, Result};
|
use miette::{IntoDiagnostic, Result};
|
||||||
use tempfile::NamedTempFile;
|
use tempfile::NamedTempFile;
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug, Default)]
|
||||||
pub struct State {
|
pub struct State {
|
||||||
pub emit_file: RotatingTempFile,
|
pub emit_file: RotatingTempFile,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl State {
|
#[derive(Clone, Debug, Default)]
|
||||||
pub fn new() -> Result<Self> {
|
pub struct RotatingTempFile(Arc<Mutex<Option<NamedTempFile>>>);
|
||||||
let emit_file = RotatingTempFile::new()?;
|
|
||||||
Ok(Self { emit_file })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
|
||||||
pub struct RotatingTempFile(Arc<Mutex<NamedTempFile>>);
|
|
||||||
|
|
||||||
impl RotatingTempFile {
|
impl RotatingTempFile {
|
||||||
pub fn new() -> Result<Self> {
|
|
||||||
let file = Arc::new(Mutex::new(NamedTempFile::new().into_diagnostic()?));
|
|
||||||
Ok(Self(file))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn rotate(&self) -> Result<()> {
|
pub fn rotate(&self) -> Result<()> {
|
||||||
// implicitly drops the old file
|
// implicitly drops the old file
|
||||||
*self.0.lock().unwrap() = NamedTempFile::new().into_diagnostic()?;
|
*self.0.lock().unwrap() = Some(NamedTempFile::new().into_diagnostic()?);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write(&self, data: &[u8]) -> Result<()> {
|
pub fn write(&self, data: &[u8]) -> Result<()> {
|
||||||
self.0.lock().unwrap().write_all(data).into_diagnostic()
|
if let Some(file) = self.0.lock().unwrap().as_mut() {
|
||||||
|
file.write_all(data).into_diagnostic()?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn path(&self) -> PathBuf {
|
pub fn path(&self) -> PathBuf {
|
||||||
self.0.lock().unwrap().path().to_owned()
|
if let Some(file) = self.0.lock().unwrap().as_ref() {
|
||||||
|
file.path().to_owned()
|
||||||
|
} else {
|
||||||
|
PathBuf::new()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue