Skip to content

CLI utility for fake data generation

License

Notifications You must be signed in to change notification settings

lucapette/fakedata

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

fakedata

fakedata is a small program that generates test data on the command line.

How to install

If you use Homebrew:

brew install lucapette/tap/fakedata

If you have Go installed:

go install github.com/lucapette/fakedata@latest

Or you can download the latest compiled binary and put it anywhere in your executable path.

If you want to build it yourself, refer to our contributing guidelines.

Quick Start

fakedata helps you generate random data in a number of ways.

By default, it 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 specify a SQL formatter:

$ fakedata --format=sql --limit 1 email domain
INSERT INTO TABLE (email,domain) values ('[email protected]','example.me');

Or a ndjson one:

$ fakedata --format=ndjson --limit 1 noun country.code
{"country.code":"PY","noun":"mainframe"}

You can change the name of the field column using a field with the syntax column_name=generator. It works with the SQL formatter as well the ndjson one:

$ fakedata --format=sql --limit 1 login=email referral=domain
INSERT INTO TABLE (login,referral) values ('[email protected]','test.me');

$ fakedata --format=ndjson --limit 1 login=email referral=domain
{"login":"[email protected]","referral":"example.ventures"}

fakedata can also stream rows of test data for you:

$ fakedata --stream animal
horse
koala
chameleon
## and so on...

If you need more control over the output, 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.

It 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.

You 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]>

Or you ask fakedata to read templates from disk:

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 ProductName).

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:

$ echo '{{ Enum "feature" "bug" "documentation" }}' | fakedata -l5
feature
bug
documentation
feature
documentation

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.

$ echo "uno\ndue\ntre" > example.txt
$ echo '{{ File "./example.txt" }}' | fakedata -l5
tre
uno
due
due
due
foo

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 iterations 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.

For example, this is helpful when creating HTML tables:

{{ range $i := 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" NameLast NameFirst }}

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 contribute

We love every form of contribution! Good entry points to the project are:

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