Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

project: don't create .hemttout when not needed #840

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(())
}
Loading