-
Notifications
You must be signed in to change notification settings - Fork 141
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
29 changed files
with
731 additions
and
588 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,78 +1,35 @@ | ||
use crate::lib::{ | ||
error::{JdcError, JdcResult}, | ||
error::JdcResult, | ||
jdc_config::JdcConfig, | ||
}; | ||
use std::path::PathBuf; | ||
use tracing::error; | ||
|
||
#[derive(Debug)] | ||
pub struct Args { | ||
pub config_path: PathBuf, | ||
use clap::Parser; | ||
|
||
#[derive(Parser, Debug)] | ||
#[command(version, about, long_about = None)] | ||
struct Args { | ||
#[arg( | ||
short, | ||
long, | ||
help = "Path to TOML configuration file" | ||
)] | ||
config_path: String, | ||
} | ||
|
||
enum ArgsState { | ||
Next, | ||
ExpectPath, | ||
Done, | ||
} | ||
|
||
enum ArgsResult { | ||
Config(PathBuf), | ||
None, | ||
Help(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); | ||
} | ||
|
||
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; | ||
|
||
Some(ArgsResult::None) | ||
} | ||
}, | ||
ArgsState::ExpectPath => Some(ArgsResult::Config(PathBuf::from(item))), | ||
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 }) | ||
} | ||
} | ||
|
||
/// Process CLI args, if any. | ||
#[allow(clippy::result_large_err)] | ||
pub fn process_cli_args<'a>() -> JdcResult<'a, JdcConfig> { | ||
let args = match Args::from_args() { | ||
let args = Args::parse(); | ||
let config = match config::Config::builder() | ||
.add_source(config::File::with_name(&args.config_path)) | ||
.build() { | ||
Ok(cfg) => cfg, | ||
Err(help) => { | ||
error!("{}", help); | ||
return Err(JdcError::BadCliArgs); | ||
Err(e) => { | ||
tracing::error!("{:?}", e); | ||
std::process::exit(1) | ||
} | ||
}; | ||
let config_file = std::fs::read_to_string(args.config_path)?; | ||
Ok(toml::from_str::<JdcConfig>(&config_file)?) | ||
} | ||
|
||
let jdc_config: JdcConfig = config.try_deserialize()?; | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,34 @@ | ||
use crate::lib::{ | ||
error::{JdsError, JdsResult}, | ||
error::JdsResult, | ||
jds_config::JdsConfig, | ||
}; | ||
use std::path::PathBuf; | ||
use tracing::error; | ||
|
||
#[derive(Debug)] | ||
pub struct Args { | ||
pub config_path: PathBuf, | ||
} | ||
|
||
enum ArgsState { | ||
Next, | ||
ExpectPath, | ||
Done, | ||
} | ||
|
||
enum ArgsResult { | ||
Config(PathBuf), | ||
None, | ||
Help(String), | ||
use clap::Parser; | ||
|
||
#[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 = "jds-config.toml"; | ||
const HELP_MSG: &'static str = "Usage: -h/--help, -c/--config <path|default jds-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); | ||
} | ||
|
||
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; | ||
|
||
Some(ArgsResult::None) | ||
} | ||
}, | ||
ArgsState::ExpectPath => Some(ArgsResult::Config(PathBuf::from(item))), | ||
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 }) | ||
} | ||
} | ||
|
||
/// Process CLI args, if any. | ||
#[allow(clippy::result_large_err)] | ||
pub fn process_cli_args() -> JdsResult<JdsConfig> { | ||
let args = match Args::from_args() { | ||
let args = Args::parse(); | ||
let config = match config::Config::builder() | ||
.add_source(config::File::with_name(&args.config_path)) | ||
.build() { | ||
Ok(cfg) => cfg, | ||
Err(help) => { | ||
error!("{}", help); | ||
return Err(JdsError::BadCliArgs); | ||
Err(e) => { | ||
tracing::error!("{:?}", e); | ||
std::process::exit(1) | ||
} | ||
}; | ||
let config_file = std::fs::read_to_string(args.config_path)?; | ||
Ok(toml::from_str::<JdsConfig>(&config_file)?) | ||
} | ||
|
||
let jds_config: JdsConfig = config.try_deserialize()?; | ||
|
||
Ok(jds_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
Oops, something went wrong.