fakedata
fakedata copied to clipboard
CLI utility for fake data generation
fakedata
is a small program that generates test data on the command line.
Quick Start
fakedata
helps you generate random data in a number of ways. By default
fakedata
uses a column formatter with space separator:
$ fakedata email country
[email protected] Afghanistan
[email protected] Turkey
[email protected] Saint Helena
[email protected] Montenegro
[email protected] Croatia
[email protected] Vietnam
[email protected] Lithuania
[email protected] Haiti
[email protected] Malaysia
[email protected] Virgin Islands, British
You can choose a different separator:
$ fakedata --separator=, product.category product.name
Shoes,Rankfix
Automotive,Namis
Movies,Matquadfax
Tools,Damlight
Computers,Silverlux
Industrial,Matquadfax
Home,Sil-Home
Health,Toughwarm
Shoes,Freetop
Tools,Domnix
# tab is a little tricky to type, but works
$ fakedata emoji industry -s=$'\t'
👦 Electrical & Electronic Manufacturing
🆘 Investment Banking/Venture
📦 Computer Hardware
♐ Computer & Network Security
🔠 Religious Institutions
💷 Automotive
🇱 Capital Markets
㊙ Public Relations
☺ Alternative Dispute Resoluti
You can also specify a SQL formatter:
$ fakedata --format=sql --limit 1 email domain
INSERT INTO TABLE (email,domain) values ('[email protected]','example.me');
You can change the name of the table column using a field with the following syntax
column_name=generator
:
$ fakedata --format=sql --limit 1 login=email referral=domain
INSERT INTO TABLE (login,referral) values ('[email protected]','test.me');
If you need more control over the output, you can use templates.
Generators
fakedata
provides a number of generators. You can see the full list running
the following command:
$ fakedata --generators # or -G
color one word color
country Full country name
country.code 2-digit country code
date date
domain domain
domain.tld example|test
#...
#...
#It's a long list :)
You can use the -g
(or --generator
) option to see an example:
$ fakedata -g sentence
Description: sentence
Example:
Jerk the dart from the cork target.
Drop the ashes on the worn old rug.
The sense of smell is better than that of touch.
Tin cans are absent from store shelves.
Shut the hatch before the waves push it in.
Constraints
Some generators allow you to pass in a range to constraint the output to a subset of values. To find out which generators support constraints:
$ fakedata -c # or fakedata --generators-with-constraints
Int
Here is how you can use constraints with the int
generator:
$ fakedata int:1,100 # will generate only integers between 1 and 100
$ fakedata int:50, # specifying only min number works too
$ fakedata int:50 # also works
Enum
The enum
generator allows you to specify a set of values. It comes handy when
you need random data from a small set of values:
$ fakedata --limit 5 enum
foo
baz
foo
foo
baz
$ fakedata --limit 5 enum:bug,feature,question,duplicate
question
duplicate
duplicate
bug
feature
When passing a single value enum
can be used to repeat a value in every line:
$ fakedata --limit 5 enum:one,two enum,repeat
two repeat
one repeat
two repeat
one repeat
one repeat
File
The file
generator can be use to read custom values from a file:
$ printf "one\ntwo\nthree" > values.txt
$ fakedata -l5 file:values.txt
three
two
two
one
two
Templates
fakedata
supports parsing and executing template files for generating
customized output formats. fakedata
executes the provided template a number of
times based on the limit flag (-l
, --limit
) and writes the output to
stdout
, exactly like using inline generators.
fakedata
can read templates from disk:
$ echo "{{Email}}--{{Int}}" > /tmp/template.tmpl
$ fakedata --template /tmp/template.tmpl
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
You can also pipe a template into fakedata
:
$ echo "#{{ Int 0 100}} {{ Name }} <{{ Email }}>" | fakedata
#56 Dannie Martin <[email protected]>
#89 Moshe Walsh <[email protected]>
#48 Buck Reid <[email protected]>
#55 Rico Powell <[email protected]>
#92 Luise Wood <[email protected]>
#30 Isreal Henderson <[email protected]>
#96 Josphine Patton <[email protected]>
#95 Jetta Blair <[email protected]>
#10 Clorinda Parsons <[email protected]>
#0 Dionna Bates <[email protected]>
The generators listed under fakedata -g
are available as functions into the
templates. If the generator name is a single word, then it's available as a
function with the same name capitalized (example: int
becomes Int
). If the
generator name is composed by multiple words joined by dots, then the function
name is again capitalized by the first letter of the word and joined together
(example: product.name
becomes Product.Name
).
Each generator with constraints is available in templates as a function that takes arguments.
Enum
Enum takes one or more strings and returns a random string on each run. Strings are passed to Enum like so:
{{ Enum "feature" "bug" "documentation" }}
This Enum will return either the string feature
, bug
, or documentation
for
each run.
File
File reads a file from disk and returns a random line on each run. It takes one parameter which is the path to the file on disk.
{{ File "/var/data/dummy/dummy.txt" }}
Int
Int takes one or two integer values and returns a number within this range. By
default it returns a number between 0
and 1000
.
echo "{{ Int 15 20 }}" | fakedata -l5
15
20
15
15
17
Date
Date takes one or two dates and returns a date within this range. By default, it returns a date between one year ago and today.
Helpers
Beside the generator functions, fakedata
templates provide a number of helper
functions:
-
Loop
-
Odd
-
Even
If you need to create your own loop for advanced templates you can use the {{ Loop }}
function. This function takes a single integer as parameter which is
the number of iterations. Loop
must be used with range
e.g.
{{ range Loop 10 }} You're going to see this 10 times! {{ end }}
Loop
can take a second argument, so that you can specify a range and
fakedata
will generate a random number of interations in that range. For
example:
{{ range Loop 1 5 }}42{{ end }}
In combination with Loop
and range
you can use Odd
and Even
to determine
if the current iteration is odd or even. This is especially helpful when
creating HTML tables:
{{ range $i, $j := Loop 5 }}
<tr>
{{ if Odd $i -}}
<td class="odd">{{- else -}}</td>
<td class="even">{{- end -}} {{ Name }}</td>
</tr>
{{ end }}
Odd
takes an integer as parameter which is why we need to assign the return
values of Loop 5
to the variables $i
and $j
.
Templates also support string manipulation via the printf
function. Using
printf
we can create custom output. For example to display a full name in the
format Lastname Firstname
instead of Firstname Lastname
.
{{ printf "%s %s" Name.Last Name.First }}
Completion
fakedata
supports basic shell tab completion for bash, zsh, and fish shells:
$ eval "$(fakedata --completion zsh)"
$ eval "$(fakedata --completion bash)"
$ eval (fakedata --completion fish)
How to install
Homebrew
fakedata
can be installed through Homebrew:
$ brew install lucapette/tap/fakedata
Standalone
fakedata
can be installed as an executable. Download the latest compiled
binary and put it anywhere in
your executable path.
Source
Please refer to our contributing guidelines to build and
install fakedata
from the source.
How to contribute
We love every form of contribution! Good entry points to the project are:
- Our contributing guidelines document
- Issues with the tag gardening
- Issues with the tag good first patch
If you're not sure where to start, please open a new issue and we'll gladly help you get started.
Code of Conduct
You are expected to follow our code of conduct when interacting with the project via issues, pull requests, or in any other form. Many thanks to the awesome contributor covenant initiative!
License
MIT License Copyright (c) [2022] Luca Pette