Skip to content

Commit

Permalink
Added config path argument #53 (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
WindowGenerator committed Nov 12, 2023
1 parent 1d987dc commit 74693dd
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 19 deletions.
4 changes: 4 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ pub struct Args {
#[arg(long)]
pub uppercase_ratio: Option<f64>,

// path to config file in json format
#[arg(long)]
pub config_path: Option<String>,

/// indicates if test results should be saved
#[arg(long)]
pub save_results: Option<bool>,
Expand Down
4 changes: 4 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ mod tests {
symbols_ratio: None,
uppercase: None,
uppercase_ratio: None,
config_path: None,
save_results: None,
results_path: None,
history: None,
Expand Down Expand Up @@ -329,6 +330,7 @@ mod tests {
dictionary_path: None,
uppercase: None,
uppercase_ratio: None,
config_path: None,
save_results: None,
results_path: None,
history: None,
Expand All @@ -352,6 +354,7 @@ mod tests {
dictionary_path: None,
uppercase: None,
uppercase_ratio: None,
config_path: None,
save_results: Some(false),
results_path: None,
history: None,
Expand Down Expand Up @@ -381,6 +384,7 @@ mod tests {
uppercase: None,
uppercase_ratio: None,
save_results: Some(true),
config_path: Some(String::from("/config.json")),
results_path: Some(String::from("/some-path")),
history: None,
};
Expand Down
51 changes: 32 additions & 19 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ use crossterm::{
};
use ratatui::{backend::CrosstermBackend, Terminal};
use std::io;
use std::path::PathBuf;
use test_results::{read_previous_results, render_results};

use args::Args;
Expand Down Expand Up @@ -131,15 +132,20 @@ fn handle_main_command(
terminal: &mut Terminal<CrosstermBackend<io::Stdout>>,
args: Args,
) -> Result<()> {
let config_file_path = if cfg!(target_os = "windows") {
dirs::config_local_dir().context("Unable to get local config directory")?
} else {
dirs::home_dir()
.context("Unable to get home directory")?
.join(".config")
}
.join("donkeytype")
.join("donkeytype-config.json");
let config_file_path = args
.config_path
.clone()
.map(PathBuf::from)
.unwrap_or_else(|| {
let base_dir = if cfg!(target_os = "windows") {
dirs::config_local_dir().expect("Unable to get local config directory")
} else {
dirs::home_dir()
.expect("Unable to get home directory")
.join(".config")
};
base_dir.join("donkeytype").join("donkeytype-config.json")
});

let config = Config::new(args, config_file_path).context("Unable to create config")?;
let expected_input = ExpectedInput::new(&config).context("Unable to create expected input")?;
Expand Down Expand Up @@ -205,7 +211,7 @@ fn restore_terminal(

#[cfg(test)]
mod tests {
use std::{io::Write, time::Instant};
use std::{io::Write, path::PathBuf, time::Instant};

use anyhow::{Context, Result};
use predicates::Predicate;
Expand Down Expand Up @@ -240,15 +246,20 @@ mod tests {
}

fn setup_terminal(args: Args) -> Result<(Config, ExpectedInput, Terminal<TestBackend>)> {
let config_file_path = if cfg!(target_os = "windows") {
dirs::config_local_dir().context("Unable to get local config directory")?
} else {
dirs::home_dir()
.context("Unable to get home directory")?
.join(".config")
}
.join("donkeytype")
.join("donkeytype-config.json");
let config_file_path = args
.config_path
.clone()
.map(PathBuf::from)
.unwrap_or_else(|| {
let base_dir = if cfg!(target_os = "windows") {
dirs::config_local_dir().expect("Unable to get local config directory")
} else {
dirs::home_dir()
.expect("Unable to get home directory")
.join(".config")
};
base_dir.join("donkeytype").join("donkeytype-config.json")
});

let config = Config::new(args, config_file_path).context("Unable to create config")?;
let expected_input =
Expand All @@ -275,6 +286,7 @@ mod tests {
numbers_ratio: None,
symbols: None,
symbols_ratio: None,
config_path: None,
save_results: None,
results_path: None,
history: None,
Expand Down Expand Up @@ -312,6 +324,7 @@ mod tests {
numbers_ratio: None,
symbols: None,
symbols_ratio: None,
config_path: None,
save_results: None,
results_path: None,
history: None,
Expand Down

0 comments on commit 74693dd

Please sign in to comment.