From 260bcb7ca5acb4561c86b48f5798e6e1a026f30a Mon Sep 17 00:00:00 2001 From: Bauke Date: Wed, 17 Jan 2024 18:43:17 +0100 Subject: [PATCH] Add a noise level configuration option. --- hooked-config/source/config/general.rs | 6 +++++ hooked-config/source/config/mod.rs | 2 ++ hooked-config/source/config/noise_level.rs | 24 +++++++++++++++++++ hooked-config/source/config/pre_commit.rs | 6 ++++- hooked-config/tests/parsing/with-hooks.toml | 3 +++ hooked-config/tests/serialize.rs | 4 +++- .../tests/snapshots/parsing__defaults.snap | 2 ++ .../tests/snapshots/parsing__with-hooks.snap | 3 +++ .../tests/snapshots/serialize__serialize.snap | 3 +++ 9 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 hooked-config/source/config/noise_level.rs diff --git a/hooked-config/source/config/general.rs b/hooked-config/source/config/general.rs index 4dcfee8..31f2a42 100644 --- a/hooked-config/source/config/general.rs +++ b/hooked-config/source/config/general.rs @@ -4,6 +4,8 @@ use std::path::PathBuf; use serde::{Deserialize, Serialize}; +use crate::NoiseLevel; + /// General Hooked configuration. #[derive(Debug, Deserialize, Serialize)] #[serde(default, deny_unknown_fields)] @@ -14,6 +16,9 @@ pub struct General { /// The directory to use for hooks. pub directory: PathBuf, + /// The noise level tasks should output logs with by default. + pub noise_level: NoiseLevel, + /// Path to a script template for use with the install subcommand. pub template: Option, } @@ -23,6 +28,7 @@ impl Default for General { Self { config: PathBuf::from("Hooked.toml"), directory: PathBuf::from("hooks"), + noise_level: NoiseLevel::default(), template: None, } } diff --git a/hooked-config/source/config/mod.rs b/hooked-config/source/config/mod.rs index d69d88d..21d5d38 100644 --- a/hooked-config/source/config/mod.rs +++ b/hooked-config/source/config/mod.rs @@ -9,11 +9,13 @@ use { mod exit_action; mod general; +mod noise_level; mod pre_commit; mod task; pub use exit_action::*; pub use general::*; +pub use noise_level::*; pub use pre_commit::*; pub use task::*; diff --git a/hooked-config/source/config/noise_level.rs b/hooked-config/source/config/noise_level.rs new file mode 100644 index 0000000..afc3b7f --- /dev/null +++ b/hooked-config/source/config/noise_level.rs @@ -0,0 +1,24 @@ +//! The noise level Hooked should output logs with. + +use serde::{Deserialize, Serialize}; + +/// The noise level Hooked should output logs with. +#[derive(Debug, Deserialize, Eq, PartialEq, Serialize)] +#[serde(rename_all = "snake_case")] +pub enum NoiseLevel { + /// Output only errors. + Quiet, + /// Output everything. + Loud, + /// Print a list of tasks and output warnings and errors, this is the default. + Standard, + /// The same as [`NoiseLevel::Standard`] except don't output task names or + /// warnings. + Minimal, +} + +impl Default for NoiseLevel { + fn default() -> Self { + Self::Standard + } +} diff --git a/hooked-config/source/config/pre_commit.rs b/hooked-config/source/config/pre_commit.rs index 01b0faf..b3573fe 100644 --- a/hooked-config/source/config/pre_commit.rs +++ b/hooked-config/source/config/pre_commit.rs @@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize}; -use crate::{ExitAction, Task}; +use crate::{ExitAction, NoiseLevel, Task}; /// A pre-commit hook. #[derive(Debug, Deserialize, Serialize)] @@ -11,6 +11,10 @@ pub struct PreCommit { /// Display name for this hook. pub name: Option, + /// The noise level this task should output with. + #[serde(default)] + pub noise_level: NoiseLevel, + /// What to do when the hook exits with a non-zero status code. #[serde(default)] pub on_failure: ExitAction, diff --git a/hooked-config/tests/parsing/with-hooks.toml b/hooked-config/tests/parsing/with-hooks.toml index 9afc804..18e6563 100644 --- a/hooked-config/tests/parsing/with-hooks.toml +++ b/hooked-config/tests/parsing/with-hooks.toml @@ -1,14 +1,17 @@ [general] directory = "hooked" +noise_level = "minimal" template = "test.sh" [[pre_commit]] name = "Pre Commit 1" +noise_level = "quiet" command = "exit 0" staged = ["*.txt"] on_failure = "continue" [[pre_commit]] name = "Pre Commit 2" +noise_level = "loud" script = "test.sh" on_failure = "stop" diff --git a/hooked-config/tests/serialize.rs b/hooked-config/tests/serialize.rs index 77ae6cd..c54e276 100644 --- a/hooked-config/tests/serialize.rs +++ b/hooked-config/tests/serialize.rs @@ -1,5 +1,5 @@ use { - hooked_config::{Config, ExitAction, PreCommit, Task}, + hooked_config::{Config, ExitAction, NoiseLevel, PreCommit, Task}, toml::to_string_pretty, }; @@ -9,6 +9,7 @@ use insta::assert_snapshot; fn test_serialize() { let pre_commit_command = PreCommit { name: Some("Command Test".to_string()), + noise_level: NoiseLevel::Quiet, on_failure: ExitAction::Continue, staged: vec!["*.txt".to_string()], task: Task { @@ -19,6 +20,7 @@ fn test_serialize() { let pre_commit_script = PreCommit { name: Some("Script Test".to_string()), + noise_level: NoiseLevel::Loud, on_failure: ExitAction::Stop, staged: vec![], task: Task { diff --git a/hooked-config/tests/snapshots/parsing__defaults.snap b/hooked-config/tests/snapshots/parsing__defaults.snap index e08526c..8e397cf 100644 --- a/hooked-config/tests/snapshots/parsing__defaults.snap +++ b/hooked-config/tests/snapshots/parsing__defaults.snap @@ -6,11 +6,13 @@ Config { general: General { config: "Hooked.toml", directory: "hooks", + noise_level: Standard, template: None, }, pre_commit: [ PreCommit { name: None, + noise_level: Standard, on_failure: Stop, staged: [], task: Task { diff --git a/hooked-config/tests/snapshots/parsing__with-hooks.snap b/hooked-config/tests/snapshots/parsing__with-hooks.snap index 990759b..ed6cdd5 100644 --- a/hooked-config/tests/snapshots/parsing__with-hooks.snap +++ b/hooked-config/tests/snapshots/parsing__with-hooks.snap @@ -6,6 +6,7 @@ Config { general: General { config: "Hooked.toml", directory: "hooked", + noise_level: Minimal, template: Some( "test.sh", ), @@ -15,6 +16,7 @@ Config { name: Some( "Pre Commit 1", ), + noise_level: Quiet, on_failure: Continue, staged: [ "*.txt", @@ -30,6 +32,7 @@ Config { name: Some( "Pre Commit 2", ), + noise_level: Loud, on_failure: Stop, staged: [], task: Task { diff --git a/hooked-config/tests/snapshots/serialize__serialize.snap b/hooked-config/tests/snapshots/serialize__serialize.snap index a6fafd1..b38a5bc 100644 --- a/hooked-config/tests/snapshots/serialize__serialize.snap +++ b/hooked-config/tests/snapshots/serialize__serialize.snap @@ -5,15 +5,18 @@ expression: to_string_pretty(&config).unwrap() [general] config = 'Hooked.toml' directory = 'hooks' +noise_level = 'standard' [[pre_commit]] name = 'Command Test' +noise_level = 'quiet' on_failure = 'continue' staged = ['*.txt'] command = 'exit 0' [[pre_commit]] name = 'Script Test' +noise_level = 'loud' on_failure = 'stop' staged = [] script = 'test.sh'