Skip to content

Commit

Permalink
fix: 🚧 WIP fixing configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
pvshvp-oss committed May 5, 2024
1 parent 1be564e commit f3a8a9c
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 89 deletions.
11 changes: 9 additions & 2 deletions paxy-cli/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
//! Has the [`run_cli`] function and the commandline interface template
//! [`cli_template::CliTemplate`]
/// Calls the [`ui::run_common::<C>`] function supplying it with the commandline
/// interface template as a type. Any errors are thrown back to the calling
/// function. A debug message is then displayed conveying that the program is
/// being run in the CLI mode.
pub fn run_cli() -> Result<(), paxy::Error> {
let (_cli_input, _worker_guards) = ui::run_common::<CliTemplate>()?;
let (_cli_input, _logging_worker_guards) = ui::run_common::<CliTemplate>()?;

tracing::debug!(
"Running in {} mode... {}",
Expand Down Expand Up @@ -408,6 +415,6 @@ mod cli_template {

// region: RE-EXPORTS

pub use cli_template::*;
pub use cli_template::*; // Flatten the module heirarchy for easier access

// endregion: RE-EXPORTS
7 changes: 7 additions & 0 deletions paxy-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
//! Starts execution at the [`main`] function. Offloads the implemenation
//! details to its corresponding library crate.
/// Calls the [`paxy_cli::run_cli`] function and captures the returned
/// [`Result`]. If there was an error, the error message chain is printed to the
/// standard error stream (`stderr`). The program then returns an `0` or `1`
/// corresponding to "no error" or "error" based on the result.
fn main() -> process::ExitCode {
let return_value = paxy_cli::run_cli();
match return_value {
Expand Down
19 changes: 16 additions & 3 deletions paxy-gui/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
//! Has the [`run_gui`] function and the commandline interface template
//! [`gui_cli_template::CliTemplate`]
/// Calls the [`ui::run_common::<C>`] function supplying it with the commandline
/// interface template as a type. Any errors are thrown back to the calling
/// function. A debug message is then displayed conveying that the program is
/// being run in the GUI mode.
pub fn run_gui() -> Result<(), paxy::Error> {
let (_cli_input, _worker_guards) = ui::run_common::<CliTemplate>()?;
let (_cli_input, _logging_worker_guards) = ui::run_common::<CliTemplate>()?;

tracing::debug!(
"Running in {} mode... {}",
Expand All @@ -15,7 +22,7 @@ pub fn run_gui() -> Result<(), paxy::Error> {
pub enum Error {
#[non_exhaustive]
#[snafu(display(""), visibility(pub))]
GuiDummy {},
GuiDummy {}, // No errors implemented yet
}

// region: IMPORTS
Expand All @@ -28,14 +35,20 @@ use snafu::Snafu;

// region: MODULES

/// The commandline interface for the GUI program. Allows one to specify flags
/// that control output on a console.
mod gui_cli_template {

/// The base commandline template consists of global arguments
#[derive(Parser, Debug)]
#[command(version, author, about, args_conflicts_with_subcommands = true)]
pub struct CliTemplate {
#[clap(flatten)]
pub global_args: ui::cli_template::GlobalArgs<clap_verbosity_flag::InfoLevel>,
}

/// Implement a trait that can extract standard global arguments from our
/// own CLI template
impl ui::GlobalArguments for CliTemplate {
type L = clap_verbosity_flag::InfoLevel;

Expand Down Expand Up @@ -91,6 +104,6 @@ mod gui_cli_template {

// region: RE-EXPORTS

pub use gui_cli_template::*;
pub use gui_cli_template::*; // Flatten the module heirarchy for easier access

// endregion: RE-EXPORTS
7 changes: 7 additions & 0 deletions paxy-gui/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
//! Starts execution at the [`main`] function. Offloads the implemenation
//! details to its corresponding library crate.
/// Calls the [`paxy_gui::run_gui`] function and captures the returned
/// [`Result`]. If there was an error, the error message chain is printed to the
/// standard error stream (`stderr`). The program then returns an `0` or `1`
/// corresponding to "no error" or "error" based on the result.
fn main() -> process::ExitCode {
let return_value = paxy_gui::run_gui();
match return_value {
Expand Down
2 changes: 1 addition & 1 deletion paxy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ console = { workspace = true }
home = "0.5.9"
toml = "0.8.10"
pollster = "0.3"
reqwest = "0.11"
reqwest = "0.12"
url = { version = "2.3", features = ["serde"] }
extism = "1.2.0"
bson = "2.9.0"
Expand Down
29 changes: 14 additions & 15 deletions paxy/src/app/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/// local paths. overridden by environment variables starting with `PAXY_`,
/// overridden by the configuration file specified by the commandline.
/// Values from only files with supported file extensions would be merged.
pub fn init_config<C>(cli_modifier: &C) -> Result<(Config, Vec<PathBuf>), Error>
pub fn init_config<G>(cli_global_arguments: G) -> Result<(Config, Vec<PathBuf>), Error>
where
C: ui::CliModifier,
<C as ui::GlobalArguments>::L: clap_verbosity_flag::LogLevel,
G: ui::GlobalArguments,
<G as ui::GlobalArguments>::L: clap_verbosity_flag::LogLevel,
{
let mut candidate_config_filepath_stubs: Vec<PathBuf> = Vec::new();

Expand Down Expand Up @@ -44,7 +44,7 @@ where

// Merge configuration values from additional config filepaths (usually
// specified through CLI)
if let Some(additional_config_filepath) = cli_modifier.config_file() {
if let Some(additional_config_filepath) = preferred_config_filepath {
if let Some(parent) = additional_config_filepath.parent() {
if let Some(stem) = additional_config_filepath.file_stem() {
let mut stub = PathBuf::from(parent);
Expand All @@ -58,10 +58,10 @@ where
// Merge configuration values from the CLI
// These are not set to be optional, so only action-required states are
// merged with the configuration
if cli_modifier.is_uncolored() {
if cli_global_arguments.is_uncolored() {
figment = figment.admerge(("no_color", true));
}
if let Some(log_level_filter) = cli_modifier.verbosity_filter() {
if let Some(log_level_filter) = cli_global_arguments.verbosity_filter() {
figment = figment.admerge(("log_level_filter", log_level_filter));
}

Expand Down Expand Up @@ -91,13 +91,11 @@ fn admerge_from_stub(candidate_config_filepath_stub: &PathBuf, mut figment: Figm

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Config {
pub paths: app::Paths,
pub config_dirpaths: Vec<PathBuf>,

pub log_directory: Option<String>,
pub log_dirpath: PathBuf,

pub log_level_filter: Option<LevelFilter>,

pub no_color: Option<bool>,
pub cli_output_format: ui::CliOutputFormat,
}

impl Config {
Expand All @@ -108,10 +106,11 @@ impl Config {

impl Default for Config {
fn default() -> Self {
Config {
log_directory: None,
log_level_filter: Some(LevelFilter::Info),
no_color: Some(false),
Self {
config_dirpaths: None,
log_dirpath: None,
log_directory: todo!(),
cli_output_format: todo!(),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion paxy/src/app/i18n.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ pub enum Error {
I18nDummy {},
}

// endregion: ERRORS
// endregion: ERRORS
9 changes: 2 additions & 7 deletions paxy/src/app/logging.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
pub fn init_log(
preferred_log_dirpath: Option<PathBuf>,
preferred_log_level_filter: Option<log::LevelFilter>,
) -> Result<(Handle, PathBuf), Error> {
pub fn init_log(max_output_verbosity: log::LevelFilter) -> Result<(Handle, PathBuf), Error> {
let log_filename = format!("{}.log", *app::APP_NAME);
let log_dirpath = obtain_log_dirpath(preferred_log_dirpath)?;
let log_file_appender =
tracing_appender::rolling::daily(log_dirpath.clone(), log_filename.clone());
let log_level_filter = tracing_level_filter_from_log_level_filter(
preferred_log_level_filter.unwrap_or(log::LevelFilter::Info),
);
let log_level_filter = tracing_level_filter_from_log_level_filter(max_output_verbosity);

// Obtain writers to various logging destinations and worker guards (for
// keeping the streams alive)
Expand Down
6 changes: 0 additions & 6 deletions paxy/src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@ lazy_static! {
pub static ref APP_NAME: &'static str = "paxy";
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Paths {
pub config_dirpaths: Vec<PathBuf>,
pub log_dirpath: PathBuf,
}

// region: IMPORTS

use std::path::PathBuf;
Expand Down
Loading

0 comments on commit f3a8a9c

Please sign in to comment.