From 7899d3d24ac126e4fa974c04d38e40cf1a583a85 Mon Sep 17 00:00:00 2001 From: Liam Bigelow <40188355+bglw@users.noreply.github.com> Date: Tue, 17 Dec 2024 21:09:31 +1300 Subject: [PATCH] Add `--quiet` and `--silent` configuration options when indexing --- docs/content/docs/config-options.md | 18 +++++++++++-- .../_macros/run_failing.toolproof.macro.yml | 3 +++ ...run_failing_with_flags.toolproof.macro.yml | 3 +++ .../log_levels/error_logs.toolproof.yml | 15 +++++++++++ .../log_levels/success_logs.toolproof.yml | 26 +++++++++++++++++++ pagefind/src/fossick/mod.rs | 14 +++++----- pagefind/src/lib.rs | 4 +-- pagefind/src/logging.rs | 19 +++++++++----- pagefind/src/options.rs | 22 ++++++++++++++++ 9 files changed, 106 insertions(+), 18 deletions(-) create mode 100644 pagefind/integration_tests/_macros/run_failing.toolproof.macro.yml create mode 100644 pagefind/integration_tests/_macros/run_failing_with_flags.toolproof.macro.yml create mode 100644 pagefind/integration_tests/log_levels/error_logs.toolproof.yml create mode 100644 pagefind/integration_tests/log_levels/success_logs.toolproof.yml diff --git a/docs/content/docs/config-options.md b/docs/content/docs/config-options.md index 6e215646..e519f0a0 100644 --- a/docs/content/docs/config-options.md +++ b/docs/content/docs/config-options.md @@ -5,11 +5,11 @@ nav_section: References weight: 51 --- -The Pagefind CLI has the following options. +The Pagefind CLI has the following options. These can be set via any [configuration source](/docs/config-sources/). > These configuration options only apply when running the Pagefind indexing tool on your site. -> For configuring Pagefind search in the browser, see [Pagefind Search Config](/docs/search-config/). +> For configuring Pagefind search in the browser, see [Pagefind Search Config](/docs/search-config/). > For configuring the Pagefind Default UI, see [Pagefind UI](/docs/ui/). ## Required arguments @@ -102,6 +102,20 @@ Prints extra logging while indexing the site. Only affects the CLI, does not imp |-------------|--------------------|------------| | `--verbose` | `PAGEFIND_VERBOSE` | `verbose` | +### Quiet +Only logs errors and warnings while indexing the site. Only affects the CLI, does not impact web-facing search. + +| CLI Flag | ENV Variable | Config Key | +|-----------|------------------|------------| +| `--quiet` | `PAGEFIND_QUIET` | `quiet` | + +### Silent +Only logs errors while indexing the site. Only affects the CLI, does not impact web-facing search. + +| CLI Flag | ENV Variable | Config Key | +|-----------|------------------|------------| +| `--silent` | `PAGEFIND_SILENT` | `silent` | + ### Logfile Writes logs to the given logfile, in addition to the console. Replaces the file on each run. diff --git a/pagefind/integration_tests/_macros/run_failing.toolproof.macro.yml b/pagefind/integration_tests/_macros/run_failing.toolproof.macro.yml new file mode 100644 index 00000000..6c96163d --- /dev/null +++ b/pagefind/integration_tests/_macros/run_failing.toolproof.macro.yml @@ -0,0 +1,3 @@ +macro: I run a failing Pagefind +steps: + - I run "%toolproof_process_directory%/target/%pagefind_mode%/pagefind" and expect it to fail diff --git a/pagefind/integration_tests/_macros/run_failing_with_flags.toolproof.macro.yml b/pagefind/integration_tests/_macros/run_failing_with_flags.toolproof.macro.yml new file mode 100644 index 00000000..c2998896 --- /dev/null +++ b/pagefind/integration_tests/_macros/run_failing_with_flags.toolproof.macro.yml @@ -0,0 +1,3 @@ +macro: I run a failing Pagefind with {flags} +steps: + - I run "%toolproof_process_directory%/target/%pagefind_mode%/pagefind %flags%" and expect it to fail diff --git a/pagefind/integration_tests/log_levels/error_logs.toolproof.yml b/pagefind/integration_tests/log_levels/error_logs.toolproof.yml new file mode 100644 index 00000000..2bb8873f --- /dev/null +++ b/pagefind/integration_tests/log_levels/error_logs.toolproof.yml @@ -0,0 +1,15 @@ +name: Log level > error logs +steps: + - step: I have the environment variable "PAGEFIND_SITE" set to "public" + + - macro: I run a failing Pagefind with "--verbose" + - "stdout should contain 'Found 0 files'" + - "stderr should contain 'Error: Pagefind was not able to build an index'" + + - macro: I run a failing Pagefind with "--quiet" + - "stdout should be empty" + - "stderr should contain 'Error: Pagefind was not able to build an index'" + + - macro: I run a failing Pagefind with "--silent" + - "stdout should be empty" + - "stderr should contain 'Error: Pagefind was not able to build an index'" diff --git a/pagefind/integration_tests/log_levels/success_logs.toolproof.yml b/pagefind/integration_tests/log_levels/success_logs.toolproof.yml new file mode 100644 index 00000000..140f9678 --- /dev/null +++ b/pagefind/integration_tests/log_levels/success_logs.toolproof.yml @@ -0,0 +1,26 @@ +name: Log level > success logs +steps: + - step: I have the environment variable "PAGEFIND_SITE" set to "public" + - step: I have a "public/index.html" file with the content {html} + html: >- +

Nothing

+ - step: I have a "public/cat/index.html" file with the content {html} + html: >- +

world

Hello

+ - step: I have a "public/no-lang/index.html" file with the content {html} + html: >- +

world

+ + - macro: I run Pagefind with "--verbose" + - "stdout should contain 'unknown: 1 page'" + - 'stderr should contain ''data-pagefind-ignore value of "misc" is not valid''' + + - macro: I run Pagefind with "--quiet && echo done" + - "stdout should be exactly 'done\n'" + - 'stderr should contain ''data-pagefind-ignore value of "misc" is not valid''' + + - macro: I run Pagefind with "--silent && echo complete" + - "stdout should be exactly 'complete\n'" + - stderr should be empty diff --git a/pagefind/src/fossick/mod.rs b/pagefind/src/fossick/mod.rs index c23456d3..998b27ed 100644 --- a/pagefind/src/fossick/mod.rs +++ b/pagefind/src/fossick/mod.rs @@ -123,10 +123,10 @@ impl Fossicker { break; } if let Err(error) = rewriter.write(&buf[..read]) { - println!( + options.logger.error(format!( "Failed to parse file {} — skipping this file. Error:\n{error}", - file_path.to_str().unwrap_or("[unknown file]") - ); + file_path.to_str().unwrap_or("[unknown file]"), + )); return Ok(()); } } @@ -136,10 +136,10 @@ impl Fossicker { break; } if let Err(error) = rewriter.write(&buf[..read]) { - println!( + options.logger.error(format!( "Failed to parse file {} — skipping this file. Error:\n{error}", file_path.to_str().unwrap_or("[unknown file]") - ); + )); return Ok(()); } } @@ -170,7 +170,7 @@ impl Fossicker { break; } if let Err(error) = rewriter.write(&buf[..read]) { - println!( + options.logger.error(format!( "Failed to parse file {} — skipping this file. Error:\n{error}", &self .file_path @@ -179,7 +179,7 @@ impl Fossicker { .flatten() .or(self.page_url.as_ref().map(|u| u.as_str())) .unwrap_or("[unknown file]") - ); + )); return Ok(()); } } diff --git a/pagefind/src/lib.rs b/pagefind/src/lib.rs index 25af98d6..f6942d2a 100644 --- a/pagefind/src/lib.rs +++ b/pagefind/src/lib.rs @@ -269,12 +269,12 @@ impl SearchState { if stats.1 == 0 && !self.options.running_as_service { log.error( - "Error: Pagefind wasn't able to build an index. \n\ + "Error: Pagefind was not able to build an index. \n\ Most likely, the directory passed to Pagefind was empty \ or did not contain any html files.", ); bail!( - "Error: Pagefind wasn't able to build an index. \n\ + "Error: Pagefind was not able to build an index. \n\ Most likely, the directory passed to Pagefind was empty \ or did not contain any html files." ); diff --git a/pagefind/src/logging.rs b/pagefind/src/logging.rs index 670b2d24..91342888 100644 --- a/pagefind/src/logging.rs +++ b/pagefind/src/logging.rs @@ -10,6 +10,8 @@ use lazy_static::lazy_static; #[derive(Debug, Clone)] pub enum LogLevel { + Silent, + Quiet, Standard, Verbose, } @@ -98,25 +100,28 @@ impl Logger { } pub fn warn>(&self, msg: S) { - self.log(msg, LogLevel::Standard, LogStyle::Warning); + self.log(msg, LogLevel::Quiet, LogStyle::Warning); } pub fn v_warn>(&self, msg: S) { - self.log(msg, LogLevel::Verbose, LogStyle::Warning); + self.log(msg, LogLevel::Quiet, LogStyle::Warning); } pub fn error>(&self, msg: S) { - self.log(msg, LogLevel::Standard, LogStyle::Error); + self.log(msg, LogLevel::Silent, LogStyle::Error); } pub fn success>(&self, msg: S) { self.log(msg, LogLevel::Standard, LogStyle::Success); } - pub fn log>(&self, msg: S, log_level: LogLevel, log_style: LogStyle) { - let log = match log_level { - LogLevel::Standard => true, - LogLevel::Verbose => matches!(self.log_level, LogLevel::Verbose), + fn log>(&self, msg: S, min_log_level: LogLevel, log_style: LogStyle) { + use LogLevel::*; + let log = match min_log_level { + Silent => true, + Quiet => matches!(self.log_level, Quiet | Standard | Verbose), + Standard => matches!(self.log_level, Standard | Verbose), + Verbose => matches!(self.log_level, Verbose), }; if let Some(filename) = &self.logfile { diff --git a/pagefind/src/options.rs b/pagefind/src/options.rs index e1f889e0..7fb5f84c 100644 --- a/pagefind/src/options.rs +++ b/pagefind/src/options.rs @@ -103,6 +103,24 @@ pub(crate) struct PagefindInboundConfig { #[serde(default = "defaults::default_false")] pub(crate) verbose: bool, + #[clap( + long, + short, + help = "Only log errors and warnings while indexing the site. Does not impact the web-facing search." + )] + #[clap(required = false)] + #[serde(default = "defaults::default_false")] + pub(crate) quiet: bool, + + #[clap( + long, + short, + help = "Only log errors while indexing the site. Does not impact the web-facing search." + )] + #[clap(required = false)] + #[serde(default = "defaults::default_false")] + pub(crate) silent: bool, + #[clap( long, short, @@ -209,6 +227,10 @@ impl SearchOptions { } else { let log_level = if config.verbose { LogLevel::Verbose + } else if config.quiet { + LogLevel::Quiet + } else if config.silent { + LogLevel::Silent } else { LogLevel::Standard };