-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
46 changed files
with
1,032 additions
and
821 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,29 @@ | ||
use std::path::PathBuf; | ||
use crate::lib::{error::JdcResult, jdc_config::JdcConfig}; | ||
|
||
#[derive(Debug)] | ||
pub struct Args { | ||
pub config_path: PathBuf, | ||
} | ||
|
||
enum ArgsState { | ||
Next, | ||
ExpectPath, | ||
Done, | ||
} | ||
use clap::Parser; | ||
|
||
enum ArgsResult { | ||
Config(PathBuf), | ||
None, | ||
Help(String), | ||
#[derive(Parser, Debug)] | ||
#[command(version, about, long_about = None)] | ||
struct Args { | ||
#[arg(short, long, help = "Path to TOML configuration file")] | ||
config_path: String, | ||
} | ||
|
||
impl Args { | ||
const DEFAULT_CONFIG_PATH: &'static str = "jdc-config.toml"; | ||
const HELP_MSG: &'static str = "Usage: -h/--help, -c/--config <path|default jdc-config.toml>"; | ||
|
||
pub fn from_args() -> Result<Self, String> { | ||
let cli_args = std::env::args(); | ||
|
||
if cli_args.len() == 1 { | ||
println!("Using default config path: {}", Self::DEFAULT_CONFIG_PATH); | ||
println!("{}\n", Self::HELP_MSG); | ||
#[allow(clippy::result_large_err)] | ||
pub fn process_cli_args<'a>() -> JdcResult<'a, JdcConfig> { | ||
let args = Args::parse(); | ||
let config = match config::Config::builder() | ||
.add_source(config::File::with_name(&args.config_path)) | ||
.build() | ||
{ | ||
Ok(cfg) => cfg, | ||
Err(e) => { | ||
tracing::error!("{:?}", e); | ||
std::process::exit(1) | ||
} | ||
}; | ||
|
||
let config_path = cli_args | ||
.scan(ArgsState::Next, |state, item| { | ||
match std::mem::replace(state, ArgsState::Done) { | ||
ArgsState::Next => match item.as_str() { | ||
"-c" | "--config" => { | ||
*state = ArgsState::ExpectPath; | ||
Some(ArgsResult::None) | ||
} | ||
"-h" | "--help" => Some(ArgsResult::Help(Self::HELP_MSG.to_string())), | ||
_ => { | ||
*state = ArgsState::Next; | ||
let jdc_config: JdcConfig = config.try_deserialize()?; | ||
|
||
Some(ArgsResult::None) | ||
} | ||
}, | ||
ArgsState::ExpectPath => { | ||
let path = PathBuf::from(item.clone()); | ||
if !path.exists() { | ||
return Some(ArgsResult::Help(format!( | ||
"Error: File '{}' does not exist!", | ||
path.display() | ||
))); | ||
} | ||
Some(ArgsResult::Config(path)) | ||
} | ||
ArgsState::Done => None, | ||
} | ||
}) | ||
.last(); | ||
let config_path = match config_path { | ||
Some(ArgsResult::Config(p)) => p, | ||
Some(ArgsResult::Help(h)) => return Err(h), | ||
_ => PathBuf::from(Self::DEFAULT_CONFIG_PATH), | ||
}; | ||
Ok(Self { config_path }) | ||
} | ||
Ok(jdc_config) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.