forked from faker-ruby/faker
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CLI - Integrate fakerbot 🤖 (faker-ruby#1507)
* feat(cli): Pull in fakerbot gem logic * chore(cli): Extract module outside of `faker` dir The module dir `faker` houses the main "faker" namespaces; and it should remain that way. Best to have the CLI live outside the directory; similar to helpers * feat(cli): Add `faker` excecutable * feat(cli): Add renderer; reflector unit tests * feat(cli): Add commands tests * feat(cli): Add integration tests * feat(cli): Add documentation 📖 * chore(cli): Extract constructor to base command * fix(cli): Amend faker version load path - Add integration test to cover that * fix(cli): Skip default deprecation warning output in the CLI * chore(cli): Amend var names * chore(reflectors): Distribute Reflector responsibilities The Reflector was doing too much; handling Search and List reflections which made it a tad harder to follow. This commit defines a Reflection interface and the correspoding subclasses. * chore: Don't specify bunlder version There's an erroneous 'null byte' error in the CI which could be caused by the bundler version. Further, As of Ruby 2.6.0, bundler is now built into Ruby, so no need to install it ourselves * chore: Address review comments * move cli doc to unreleased * Reinstate rubygems deprecation warnings * chore: Skip Gem::Deprecate warning output in CLI
- Loading branch information
Showing
23 changed files
with
918 additions
and
39 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#!/usr/bin/env ruby | ||
# frozen_string_literal: true | ||
|
||
lib_path = File.expand_path('../lib', __dir__) | ||
$LOAD_PATH.unshift(lib_path) unless $LOAD_PATH.include?(lib_path) | ||
|
||
require 'cli' | ||
|
||
Signal.trap('INT') do | ||
warn("\n#{caller.join("\n")}: interrupted") | ||
exit(1) | ||
end | ||
|
||
begin | ||
Faker::CLI::Base.start | ||
rescue Faker::CLI::Base::Error => err | ||
puts "ERROR: #{err.message}" | ||
exit 1 | ||
end |
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'thor' | ||
|
||
require 'cli/commands/list' | ||
require 'cli/commands/search' | ||
require 'faker/version' | ||
|
||
module Faker | ||
module CLI | ||
class Base < Thor | ||
Error = Class.new(StandardError) | ||
# Skip default deprecation warning output; the CLI will display that. | ||
Gem::Deprecate.skip_during do | ||
desc 'version', 'Faker version' | ||
def version | ||
puts "v#{Faker::VERSION}" | ||
end | ||
map %w[--version -v] => :version | ||
|
||
desc 'list', 'List all Faker constants' | ||
method_option :help, aliases: '-h', type: :boolean, | ||
desc: 'Display usage information' | ||
method_option :show_methods, aliases: '-m', type: :boolean, default: true, | ||
desc: 'Display Faker constants with methods' | ||
method_option :verbose, aliases: '-v', type: :boolean, | ||
desc: 'Include sample Faker output' | ||
def list(*) | ||
if options[:help] | ||
invoke :help, ['list'] | ||
else | ||
Faker::CLI::Commands::List.new(options).execute | ||
end | ||
end | ||
|
||
desc 'search [Faker]', 'Search Faker method(s)' | ||
method_option :help, aliases: '-h', type: :boolean, | ||
desc: 'Display usage information' | ||
method_option :show_methods, aliases: '-m', type: :boolean, default: true, | ||
desc: 'Display Faker constants with methods' | ||
method_option :verbose, aliases: '-v', type: :boolean, | ||
desc: 'Include sample Faker output' | ||
def search(query) | ||
if options[:help] | ||
invoke :help, ['search'] | ||
else | ||
Faker::CLI::Commands::Search.new(options).execute(query) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
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,55 @@ | ||
# faker (cli) | ||
|
||
## Usage | ||
|
||
1.) Run a quick lookup | ||
|
||
```bash | ||
$ faker search name | ||
# Faker::SwordArtOnline | ||
# └── real_name | ||
# └── game_name | ||
# Faker::Superhero | ||
# └── name | ||
# .... | ||
``` | ||
|
||
2.) List methods | ||
|
||
```sh | ||
$ faker list | ||
# Faker::BackToTheFuture | ||
# ├── quote | ||
# ├── date | ||
# └── character | ||
# Faker::Finance | ||
# └── credit_card | ||
# .... | ||
``` | ||
|
||
`faker` also includes an option to display sample output via the `--verbose` or `-v` flag. :wink: | ||
|
||
3.) | ||
|
||
```sh | ||
$ faker list -v | ||
# Faker::Appliance | ||
# ├── brand=> Whirlpool | ||
# └── equipment=> Sump pump | ||
# Faker::UmphreysMcgee | ||
# └── song=> Headphones & Snowcones | ||
$ faker search name -v | ||
# Faker::App | ||
# └── name=> Subin | ||
# Faker::Address | ||
# └── street_name=> Percy Landing | ||
# .... | ||
``` | ||
|
||
## Features! :sunglasses: :dancers: | ||
|
||
- [x] List classes with methods E.g. Faker::FunnyName is displayed with `.name`,`.first_name` e.t.c. | ||
- [x] Expand search to Faker::Base sub classes | ||
- [x] Paginate results :book: | ||
|
||
![screenshot 2019-01-05 at 03 02 08](https://user-images.githubusercontent.com/17295175/50717135-59d85780-1096-11e9-8d0d-eca95646644d.jpg) |
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,24 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'forwardable' | ||
|
||
require_relative 'renderer' | ||
|
||
module Faker | ||
module CLI | ||
class Command | ||
extend Forwardable | ||
|
||
def_delegators :command, :run | ||
attr_reader :options | ||
|
||
def initialize(options) | ||
@options = options | ||
end | ||
|
||
def render(result, output) | ||
Renderer.call(result, options, output) | ||
end | ||
end | ||
end | ||
end |
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,17 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative '../command' | ||
require_relative '../reflectors/list' | ||
|
||
module Faker | ||
module CLI | ||
module Commands | ||
class List < Command | ||
def execute(output: $stdout) | ||
result = Reflectors::List.call(options) | ||
render(result, output) | ||
end | ||
end | ||
end | ||
end | ||
end |
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,29 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative '../command' | ||
require_relative '../reflectors/search' | ||
|
||
module Faker | ||
module CLI | ||
module Commands | ||
class Search < Command | ||
def execute(input, output: $stdout) | ||
result = Reflectors::Search.call(input) | ||
render(result, output) | ||
end | ||
|
||
private | ||
|
||
def render(result, output) | ||
return not_found(output) if result.empty? | ||
|
||
super(result, output) | ||
end | ||
|
||
def not_found(output) | ||
output.puts "\nSorry, we couldn't find a match 😢", "\n" | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.