Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
pvshvp-oss committed May 7, 2024
1 parent dcd64e0 commit d4cfd3b
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 51 deletions.
2 changes: 1 addition & 1 deletion paxy-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ mod cli_template {
.is_no_color()
}

fn verbosity_filter(&self) -> &log::LevelFilter {
fn verbosity_filter(&self) -> log::LevelFilter {
self.global_args
.verbosity_filter()
}
Expand Down
2 changes: 1 addition & 1 deletion paxy-gui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ mod gui_cli_template {
.is_no_color()
}

fn verbosity_filter(&self) -> &log::LevelFilter {
fn verbosity_filter(&self) -> log::LevelFilter {
self.global_args
.verbosity_filter()
}
Expand Down
89 changes: 61 additions & 28 deletions paxy/src/app/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,23 @@ fn admerge_from_stub(candidate_config_filepath_stub: &PathBuf, mut figment: Figm
figment
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ConfigTemplate {
pub config_filepaths: Vec<PathBuf>,
pub log_filepath_stub: PathBuf,
pub console_output_format: ConsoleOutputFormat,
}

impl Default for ConfigTemplate {
fn default() -> Self {
Self {
config_filepaths: Vec::new(),
log_filepath_stub: PathBuf::default(),
console_output_format: ConsoleOutputFormat::default(),
}
}
}

pub struct Config {
pub figment: Figment,
}
Expand Down Expand Up @@ -147,18 +164,51 @@ impl Config {
self
}

pub fn with_overriding_env_var<S: AsRef<str>>(env_var_name: S) -> &mut Self {
let env_var_name = env_var_name.as_ref();
self.figment = self
.figment
.admerge(Env::raw().only(&[env_var_name]));
pub fn with_overriding_args<A: ui::GlobalArguments>(&mut self, cli_arguments: A) -> &mut Self {
if let Some(path) = cli_arguments.config_filepath() {
self.figment = self
.figment
.admerge(("config_filepaths", path));
}

self
}
let console_output_mode = cli_arguments.console_output_mode();
if console_output_mode != ConsoleOutputMode::Regular {
self.figment = self
.figment
.admerge(("console_output_format.mode", console_output_mode));
}

pub fn with_overriding_args<A: ui::GlobalArguments>(&mut self, arguments: A) -> &mut Self {
if let Some(path) = arguments.config_filepath() {
self.figment = self.figment.admerge(("config_filepaths", path));
let current_max_verbosity = self
.figment
.extract_inner::<log::LevelFilter>("console_output_format.max_verbosity");
let requested_max_verbosity = cli_arguments.max_output_verbosity();
if let Ok(current_max_verbosity) = current_max_verbosity {
if cli_requested_max_verbosity > current_max_verbosity {
self.figment = self
.figment
.admerge((
"console_output_format.max_verbosity",
requested_max_verbosity,
))
}
}

let current_no_color = self
.figment
.extract_inner::<log::LevelFilter>("console_output_format.no_color");
let requested_no_color =
cli_arguments.is_no_color() || cli_arguments.is_plain() || cli_arguments.is_json();
let env_no_color = env::var("NO_COLOR").is_ok()
|| env::var(format!(
"{}_NO_COLOR",
String::from(*app::APP_NAME).to_uppercase()
))
.is_ok()
|| env::var("TERM").is_ok_and(|env_term_value| env_term_value.to_lowercase == "dumb");
if !current_no_color && (requested_no_color || env_no_color) {
self.figment = self
.figment
.admerge(("console_output_format.no_color", true));
}

self
Expand All @@ -171,23 +221,6 @@ impl Config {
}
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ConfigTemplate {
pub config_filepaths: Vec<PathBuf>,
pub log_filepath_stub: PathBuf,
pub console_output_format: ConsoleOutputFormat,
}

impl Default for ConfigTemplate {
fn default() -> Self {
Self {
config_filepaths: Vec::new(),
log_filepath_stub: PathBuf::default(),
console_output_format: ConsoleOutputFormat::default(),
}
}
}

// region: IMPORTS

use std::path::PathBuf;
Expand All @@ -204,7 +237,7 @@ use log::LevelFilter;
use serde::{Deserialize, Serialize};
use snafu::{OptionExt, ResultExt, Snafu};

use super::ui::GlobalArguments;
use super::ui::{ConsoleOutputMode, GlobalArguments};
use crate::app;
use crate::app::ui;

Expand Down
33 changes: 12 additions & 21 deletions paxy/src/app/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn resolve_max_output_verbosity<G: GlobalArguments>(
let verbosity_flag_filter = cli_output_format.requested_verbosity;

if matches!(
cli_output_format.output_mode,
cli_output_format.mode,
CliOutputMode::Plain | CliOutputMode::Json
) {
return Some(LevelFilter::Info);
Expand All @@ -62,7 +62,7 @@ fn adjust_output_formatting(
) {
// Turn off colors if requested
if matches!(
cli_output_format.output_mode,
cli_output_format.mode,
CliOutputMode::Plain | CliOutputMode::Json
) || cli_output_format.no_color
|| is_env_variable_set("NO_COLOR")
Expand All @@ -76,7 +76,7 @@ fn adjust_output_formatting(
}

// Change output mode if requested
match cli_output_format.output_mode {
match cli_output_format.mode {
CliOutputMode::Plain => logging_handle
.switch_to_plain()
.context(app::LoggingSnafu {})
Expand Down Expand Up @@ -195,7 +195,7 @@ fn emit_test_messages() {

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConsoleOutputFormat {
pub output_mode: ConsoleOutputMode,
pub mode: ConsoleOutputMode,

pub max_verbosity: log::LevelFilter,

Expand All @@ -205,7 +205,7 @@ pub struct ConsoleOutputFormat {
impl Default for ConsoleOutputFormat {
fn default() -> Self {
Self {
output_mode: ConsoleOutputMode::default(),
mode: ConsoleOutputMode::default(),
max_verbosity: log::LevelFilter::Info,
no_color: false,
}
Expand Down Expand Up @@ -277,7 +277,7 @@ pub trait GlobalArguments {

fn is_test(&self) -> bool;

fn verbosity_filter(&self) -> &log::LevelFilter;
fn verbosity_filter(&self) -> log::LevelFilter;

fn console_output_mode(&self) -> ConsoleOutputMode {
if self.is_json() {
Expand All @@ -292,21 +292,12 @@ pub trait GlobalArguments {
}

fn max_output_verbosity(&self) -> log::LevelFilter {
let verbosity_flag_filter = cli_output_format.requested_verbosity;
if matches!(
cli_output_format.output_mode,
CliOutputMode::Plain | CliOutputMode::Json
) {
return Some(LevelFilter::Info);
} else if verbosity_flag_filter < clap_verbosity_flag::LevelFilter::Debug
&& cli_global_arguments.is_debug()
{
return Some(LevelFilter::Debug);
if self.is_plain() || self.is_json() {
log::LevelFilter::Info
} else if self.is_debug() && log::LevelFilter::Debug > self.verbosity_filter() {
log::LevelFilter::Debug
} else {
return verbosity_flag_filter
.as_str()
.parse()
.ok();
self.verbosity_filter()
}
}
}
Expand Down Expand Up @@ -424,7 +415,7 @@ pub mod cli_template {
self.no_color_flag
}

fn verbosity_filter(&self) -> &log::LevelFilter {
fn verbosity_filter(&self) -> log::LevelFilter {
self.verbosity
.log_level_filter()
.and_then(|log_level_filter| {
Expand Down

0 comments on commit d4cfd3b

Please sign in to comment.