-
-
Notifications
You must be signed in to change notification settings - Fork 96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CLI Interface for fake-rs #209
Open
akhildevelops
wants to merge
6
commits into
cksac:master
Choose a base branch
from
akhildevelops:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0ebb79e
cli interface
akhildevelops d29038e
github actions filters
akhildevelops c063a30
fix workdflows
akhildevelops b528d50
fake as cli
akhildevelops de71a3b
fetch from the path
akhildevelops 5456189
fix binary name
akhildevelops File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
name = "fake" | ||
version = "3.0.1" | ||
authors = ["cksac <[email protected]>"] | ||
description = "An easy to use library for generating fake data like name, number, address, lorem, dates, etc." | ||
description = "An easy to use library and command line for generating fake data like name, number, address, lorem, dates, etc." | ||
keywords = ["faker", "data", "generator", "random"] | ||
license = "MIT OR Apache-2.0" | ||
readme = "README.md" | ||
|
@@ -40,6 +40,7 @@ url-escape = { version = "0.1", optional = true } | |
bson = { version = "2", optional = true } | ||
url = { version = "2", optional = true } | ||
indexmap = { version = "2", optional = true} | ||
clap = { version = "4.0.32", optional = true, features=["cargo"] } | ||
|
||
[dev-dependencies] | ||
chrono = { version = "0.4", features = ["clock"], default-features = false } | ||
|
@@ -59,6 +60,7 @@ bigdecimal = ["bigdecimal-rs", "rust_decimal"] | |
geo = ["geo-types", "num-traits"] | ||
http = ["dep:http", "url-escape"] | ||
bson_oid = ["bson"] | ||
clap = ["dep:clap"] | ||
|
||
[[example]] | ||
name = "basic" | ||
|
@@ -81,3 +83,10 @@ required-features = [ | |
name = "usage" | ||
path = "examples/usage.rs" | ||
required-features = ["derive"] | ||
|
||
[[bin]] | ||
name = "fake" | ||
path = "src/bin/cli/main.rs" | ||
required-features = [ | ||
"clap" | ||
] |
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 |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use clap::{builder::StyledStr, ArgMatches}; | ||
use fake::{faker, Fake}; | ||
use rand::Rng; | ||
#[derive(Clone, Copy, Debug)] | ||
#[allow(non_camel_case_types)] | ||
pub enum AVAILABLE_LOCALES { | ||
EN, | ||
FR_FR, | ||
ZH_TW, | ||
ZH_CN, | ||
JA_JP, | ||
AR_SA, | ||
PT_BR, | ||
} | ||
|
||
macro_rules! some_rules { | ||
($locale:expr, $module:ident, $fake:ident($($arg:ident)?)) => { | ||
|
||
match $locale { | ||
AVAILABLE_LOCALES::EN => { | ||
let s = faker::$module::en::$fake($($arg)?); | ||
Box::new(move |rng: &mut R| s.fake_with_rng(rng)) | ||
|
||
} | ||
AVAILABLE_LOCALES::FR_FR => { | ||
let s = faker::$module::fr_fr::$fake($($arg)?); | ||
Box::new(move |rng: &mut R| s.fake_with_rng(rng)) | ||
|
||
} | ||
AVAILABLE_LOCALES::ZH_TW => { | ||
let s = faker::$module::zh_tw::$fake($($arg)?); | ||
Box::new(move |rng: &mut R| s.fake_with_rng(rng)) | ||
|
||
} | ||
AVAILABLE_LOCALES::ZH_CN => { | ||
let s = faker::$module::zh_cn::$fake($($arg)?); | ||
Box::new(move |rng: &mut R| s.fake_with_rng(rng)) | ||
|
||
} | ||
AVAILABLE_LOCALES::AR_SA => { | ||
let s = faker::$module::ar_sa::$fake($($arg)?); | ||
Box::new(move |rng: &mut R| s.fake_with_rng(rng)) | ||
|
||
} | ||
AVAILABLE_LOCALES::JA_JP => { | ||
let s = faker::$module::ja_jp::$fake($($arg)?); | ||
Box::new(move |rng: &mut R| s.fake_with_rng(rng)) | ||
|
||
} | ||
AVAILABLE_LOCALES::PT_BR => { | ||
let s = faker::$module::pt_br::$fake($($arg)?); | ||
Box::new(move |rng: &mut R| s.fake_with_rng(rng)) | ||
|
||
} | ||
} | ||
}; | ||
} | ||
|
||
pub fn fake_generator<R>( | ||
matches: ArgMatches, | ||
locale: AVAILABLE_LOCALES, | ||
help_message: StyledStr, | ||
) -> Box<dyn Fn(&mut R) -> String> | ||
where | ||
R: Rng, | ||
{ | ||
match matches.subcommand() { | ||
Some(("FirstName", _)) => { | ||
some_rules!(locale, name, FirstName()) | ||
} | ||
Some(("Name", _)) => { | ||
some_rules!(locale, name, Name()) | ||
} | ||
Some(("CityPrefix", _)) => { | ||
some_rules!(locale, address, CityPrefix()) | ||
} | ||
Some(("Password", matches)) => { | ||
let min = *matches.get_one::<usize>("min").unwrap(); | ||
let max = *matches.get_one::<usize>("max").unwrap(); | ||
let range = min..max; | ||
some_rules!(locale, internet, Password(range)) | ||
} | ||
_ => { | ||
println!("Didn't receive subcommand\n {}", help_message); | ||
std::process::exit(0) | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,81 @@ | ||
use clap::{command, value_parser, Arg}; | ||
use rand::Rng; | ||
use std::io::{self, Write}; | ||
|
||
mod fake_gen; | ||
#[allow(non_upper_case_globals)] | ||
mod names; | ||
mod subcommands; | ||
|
||
const AVAILABLE_LOCALES: [&str; 7] = ["en", "fr_fr", "zh_tw", "zh_cn", "ja_jp", "ar_sa", "pt_br"]; | ||
|
||
pub use fake_gen::{fake_generator, AVAILABLE_LOCALES}; | ||
pub fn main() { | ||
let stdout = io::stdout(); | ||
let mut buf_stdout = io::BufWriter::new(stdout); | ||
|
||
let mut thread_rng = rand::thread_rng(); | ||
let args = cli_parser(); | ||
|
||
writeln!( | ||
buf_stdout, | ||
"Generating {} fakes for {:?} locale", | ||
args.0.repeats, args.0.locale | ||
) | ||
.unwrap(); | ||
|
||
(0..args.0.repeats).for_each(|_| writeln!(buf_stdout, "{}", args.1(&mut thread_rng)).unwrap()); | ||
} | ||
|
||
impl TryFrom<&str> for AVAILABLE_LOCALES { | ||
type Error = String; | ||
fn try_from(str_val: &str) -> Result<Self, Self::Error> { | ||
let str_val = str_val.to_lowercase(); | ||
let variant = match str_val.as_str(){ | ||
"en" => AVAILABLE_LOCALES::EN, | ||
"fr_fr" => AVAILABLE_LOCALES::FR_FR, | ||
"zh_tw" => AVAILABLE_LOCALES::ZH_TW, | ||
"zh_cn" => AVAILABLE_LOCALES::ZH_CN, | ||
"ja_jp" => AVAILABLE_LOCALES::JA_JP, | ||
"ar_sa" => AVAILABLE_LOCALES::AR_SA, | ||
"pt_br" => AVAILABLE_LOCALES::PT_BR, | ||
_=> return Err(format!("{} is either an invalid locale or not yet supported.\n The supported locales are: {:?}",str_val,AVAILABLE_LOCALES)) | ||
}; | ||
Ok(variant) | ||
} | ||
} | ||
|
||
fn cli_parser<R: Rng>() -> (Args, impl Fn(&mut R) -> String) { | ||
let mut command = command!() | ||
.arg( | ||
Arg::new("repeat") | ||
.long("repeat") | ||
.short('r') | ||
.default_value("1") | ||
.value_parser(value_parser!(u32)), | ||
) | ||
.arg( | ||
Arg::new("locale") | ||
.short('l') | ||
.long("locale") | ||
.default_value("EN") | ||
.value_parser(|value: &str| AVAILABLE_LOCALES::try_from(value)), | ||
) | ||
.subcommands(subcommands::all_fakegen_commands()) | ||
.arg_required_else_help(true); | ||
let help_message = command.render_help(); | ||
let matches = command.get_matches(); | ||
let repeats = *matches.get_one::<u32>("repeat").unwrap(); | ||
let locale = matches | ||
.get_one::<AVAILABLE_LOCALES>("locale") | ||
.unwrap() | ||
.to_owned(); | ||
|
||
let fake_gen = fake_generator::<R>(matches, locale, help_message); | ||
(Args { repeats, locale }, fake_gen) | ||
} | ||
|
||
struct Args { | ||
repeats: u32, | ||
locale: AVAILABLE_LOCALES, | ||
} |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
|
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use clap::{value_parser, Arg, Command}; | ||
macro_rules! generate_command { | ||
($fake:literal) => { | ||
Command::new($fake) | ||
}; | ||
($fake:literal, range, $min:literal, $max:literal) => { | ||
Command::new($fake) | ||
.arg( | ||
Arg::new("max") | ||
.long("max") | ||
.default_value($max) | ||
.value_parser(value_parser!(usize)), | ||
) | ||
.arg( | ||
Arg::new("min") | ||
.long("min") | ||
.default_value($min) | ||
.value_parser(value_parser!(usize)), | ||
) | ||
}; | ||
} | ||
|
||
pub fn all_fakegen_commands() -> Vec<Command> { | ||
vec![ | ||
generate_command!("Name"), | ||
generate_command!("FirstName"), | ||
generate_command!("CityPrefix"), | ||
generate_command!("Password", range, "10", "20"), | ||
] | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the feature name should be "cli"?,
cli = ["dep:clap"]