Skip to content

Commit

Permalink
project: don't create .hemttout when not needed (#840)
Browse files Browse the repository at this point in the history
  • Loading branch information
BrettMayson authored Nov 13, 2024
1 parent 7daf16d commit afce635
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
3 changes: 3 additions & 0 deletions bin/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ impl Context {
trace!("using temporary folder: {:?}", tmp.display());
let hemtt_folder = root.join(".hemtt");
trace!("using project folder: {:?}", root.display());
if !hemtt_folder.exists() {
return Err(Error::ConfigNotFound);
}
let out_folder = root.join(".hemttout");
trace!("using out folder: {:?}", out_folder.display());
create_dir_all(&out_folder)?;
Expand Down
7 changes: 5 additions & 2 deletions bin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,11 @@ pub fn execute(cli: &Cli) -> Result<(), Error> {
if !in_test && !matches!(cli.command, Some(Commands::Value(_))) {
logging::init(
cli.global.verbosity,
!matches!(cli.command, Some(Commands::Utils(_))),
);
!matches!(
cli.command,
Some(Commands::Utils(_) | Commands::Wiki(_) | Commands::New(_) | Commands::Book(_))
),
)?;
}

#[cfg(debug_assertions)]
Expand Down
15 changes: 14 additions & 1 deletion bin/src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ use tracing_subscriber::{
prelude::__tracing_subscriber_SubscriberExt, util::SubscriberInitExt, Layer,
};

use crate::Error;

/// Initialize the logger
///
/// # Errors
/// If `hemttout` is true, but no `.hemtt` folder is found
///
/// # Panics
/// If the log file could not be created
pub fn init(verbosity: u8, hemttout: bool) {
pub fn init(verbosity: u8, hemttout: bool) -> Result<(), Error> {
let format = tracing_subscriber::fmt::format()
.without_time()
.with_target(false)
Expand All @@ -31,6 +36,12 @@ pub fn init(verbosity: u8, hemttout: bool) {
};

if hemttout {
if !std::path::Path::new(".hemtt").exists() {
tracing_subscriber::registry()
.with(stdout.with_filter(filter))
.init();
return Err(Error::ConfigNotFound);
}
create_dir_all(".hemttout").expect("Unable to create `.hemttout`");
let out_file =
File::create(".hemttout/latest.log").expect("Unable to create `.hemttout/latest.log`");
Expand All @@ -48,4 +59,6 @@ pub fn init(verbosity: u8, hemttout: bool) {
.with(stdout.with_filter(filter))
.init();
}

Ok(())
}

0 comments on commit afce635

Please sign in to comment.