support initializing with specified config (supports testing)

This commit is contained in:
Trevor Bentley
2025-01-13 23:57:10 +01:00
parent fc99b9180d
commit b5c7f89959
+21 -16
View File
@@ -32,7 +32,7 @@ use std::hash::{Hash, Hasher};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::sync::atomic::Ordering; use std::sync::atomic::Ordering;
#[derive(Parser, Debug)] #[derive(Parser, Debug, Default)]
#[command(author = "Trevor Bentley", version, about, long_about = None)] #[command(author = "Trevor Bentley", version, about, long_about = None)]
#[command(help_template = "\ #[command(help_template = "\
{name} v{version}, by {author-with-newline} {name} v{version}, by {author-with-newline}
@@ -41,7 +41,7 @@ use std::sync::atomic::Ordering;
{all-args}{after-help} {all-args}{after-help}
")] ")]
struct CliArgs { pub struct CliArgs {
/// Path to TOML configuration file /// Path to TOML configuration file
#[arg(short, long, value_name = "FILE")] #[arg(short, long, value_name = "FILE")]
config: Option<PathBuf>, config: Option<PathBuf>,
@@ -62,9 +62,10 @@ struct CliArgs {
quiet: bool, quiet: bool,
/// Increase verbosity of output. Specify up to 4 times. /// Increase verbosity of output. Specify up to 4 times.
#[arg(short, long, action = clap::ArgAction::Count)] #[arg(short, long, action = clap::ArgAction::Count)]
verbose: u8, pub verbose: u8,
} }
#[derive(Default)]
pub struct GitsyCli { pub struct GitsyCli {
pub path: PathBuf, pub path: PathBuf,
pub dir: PathBuf, pub dir: PathBuf,
@@ -75,10 +76,8 @@ pub struct GitsyCli {
} }
impl GitsyCli { impl GitsyCli {
pub fn new() -> Self { pub fn new_with_config<P: AsRef<Path>>(config_path: P, cli: CliArgs) -> Self {
let cli = CliArgs::parse(); let config_dir = config_path.as_ref()
let config_path = cli.config.as_deref().unwrap_or(Path::new("config.toml")).to_owned();
let config_dir = config_path
.parent() .parent()
.expect("Config file not in valid directory.") .expect("Config file not in valid directory.")
.to_owned(); .to_owned();
@@ -86,21 +85,14 @@ impl GitsyCli {
true => config_dir, true => config_dir,
false => PathBuf::from("."), false => PathBuf::from("."),
}; };
let config_path = match config_path.canonicalize() { let config_path = match config_path.as_ref().canonicalize() {
Ok(d) => d, Ok(d) => d,
_ => config_path.clone(), _ => config_path.as_ref().to_owned(),
}; };
let config_dir = match config_dir.canonicalize() { let config_dir = match config_dir.canonicalize() {
Ok(d) => d, Ok(d) => d,
_ => config_dir.clone(), _ => config_dir.clone(),
}; };
crate::util::VERBOSITY.store(
match cli.quiet {
true => 0,
false => (cli.verbose + 1).into(),
},
Ordering::Relaxed,
);
GitsyCli { GitsyCli {
path: config_path, path: config_path,
dir: config_dir, dir: config_dir,
@@ -110,6 +102,19 @@ impl GitsyCli {
repos: cli.repo, repos: cli.repo,
} }
} }
pub fn new() -> Self {
let cli = CliArgs::parse();
let config_path = cli.config.as_deref().unwrap_or(Path::new("config.toml")).to_owned();
crate::util::VERBOSITY.store(
match cli.quiet {
true => 0,
false => (cli.verbose + 1).into(),
},
Ordering::Relaxed,
);
GitsyCli::new_with_config(config_path.as_path(), cli)
}
} }
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]