Skip to content

Commit

Permalink
Merge pull request #759 from CloudCannon/feat/quiet
Browse files Browse the repository at this point in the history
Add `--quiet` and `--silent` configuration options when indexing
  • Loading branch information
bglw authored Dec 17, 2024
2 parents 74c4ed8 + 7899d3d commit c45609d
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 18 deletions.
18 changes: 16 additions & 2 deletions docs/content/docs/config-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions pagefind/integration_tests/log_levels/error_logs.toolproof.yml
Original file line number Diff line number Diff line change
@@ -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'"
26 changes: 26 additions & 0 deletions pagefind/integration_tests/log_levels/success_logs.toolproof.yml
Original file line number Diff line number Diff line change
@@ -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: >-
<!DOCTYPE html><html lang="en"><head></head><body><p
data-url>Nothing</p></body></html>
- step: I have a "public/cat/index.html" file with the content {html}
html: >-
<!DOCTYPE html><html
lang="en"><head></head><body><h1>world</h1><p data-pagefind-ignore="misc">Hello</p></body></html>
- step: I have a "public/no-lang/index.html" file with the content {html}
html: >-
<!DOCTYPE html><html><head></head><body><h1>world</h1></body></html>
- 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
14 changes: 7 additions & 7 deletions pagefind/src/fossick/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(());
}
}
Expand All @@ -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(());
}
}
Expand Down Expand Up @@ -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
Expand All @@ -179,7 +179,7 @@ impl Fossicker {
.flatten()
.or(self.page_url.as_ref().map(|u| u.as_str()))
.unwrap_or("[unknown file]")
);
));
return Ok(());
}
}
Expand Down
4 changes: 2 additions & 2 deletions pagefind/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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."
);
Expand Down
19 changes: 12 additions & 7 deletions pagefind/src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use lazy_static::lazy_static;

#[derive(Debug, Clone)]
pub enum LogLevel {
Silent,
Quiet,
Standard,
Verbose,
}
Expand Down Expand Up @@ -98,25 +100,28 @@ impl Logger {
}

pub fn warn<S: AsRef<str>>(&self, msg: S) {
self.log(msg, LogLevel::Standard, LogStyle::Warning);
self.log(msg, LogLevel::Quiet, LogStyle::Warning);
}

pub fn v_warn<S: AsRef<str>>(&self, msg: S) {
self.log(msg, LogLevel::Verbose, LogStyle::Warning);
self.log(msg, LogLevel::Quiet, LogStyle::Warning);
}

pub fn error<S: AsRef<str>>(&self, msg: S) {
self.log(msg, LogLevel::Standard, LogStyle::Error);
self.log(msg, LogLevel::Silent, LogStyle::Error);
}

pub fn success<S: AsRef<str>>(&self, msg: S) {
self.log(msg, LogLevel::Standard, LogStyle::Success);
}

pub fn log<S: AsRef<str>>(&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<S: AsRef<str>>(&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 {
Expand Down
22 changes: 22 additions & 0 deletions pagefind/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
};
Expand Down

0 comments on commit c45609d

Please sign in to comment.