hanami-cli 1.0.0.alpha1 → 2.0.0.alpha2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (101) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ci.yml +42 -0
  3. data/.gitignore +4 -2
  4. data/.rspec +1 -0
  5. data/.rubocop.yml +24 -0
  6. data/CHANGELOG.md +14 -1
  7. data/CODE_OF_CONDUCT.md +84 -0
  8. data/Gemfile +8 -2
  9. data/LICENSE.txt +21 -0
  10. data/README.md +12 -605
  11. data/Rakefile +5 -10
  12. data/exe/hanami +10 -0
  13. data/hanami-cli.gemspec +21 -17
  14. data/lib/hanami/cli.rb +9 -122
  15. data/lib/hanami/cli/bundler.rb +73 -0
  16. data/lib/hanami/cli/command.rb +14 -386
  17. data/lib/hanami/cli/command_line.rb +17 -0
  18. data/lib/hanami/cli/commands.rb +26 -0
  19. data/lib/hanami/cli/commands/application.rb +63 -0
  20. data/lib/hanami/cli/commands/db/utils/database.rb +122 -0
  21. data/lib/hanami/cli/commands/db/utils/database_config.rb +48 -0
  22. data/lib/hanami/cli/commands/db/utils/mysql.rb +27 -0
  23. data/lib/hanami/cli/commands/db/utils/postgres.rb +49 -0
  24. data/lib/hanami/cli/commands/db/utils/sqlite.rb +37 -0
  25. data/lib/hanami/cli/commands/gem.rb +21 -0
  26. data/lib/hanami/cli/commands/gem/new.rb +77 -0
  27. data/lib/hanami/cli/commands/gem/version.rb +18 -0
  28. data/lib/hanami/cli/commands/monolith.rb +55 -0
  29. data/lib/hanami/cli/commands/monolith/console.rb +50 -0
  30. data/lib/hanami/cli/commands/monolith/db/create.rb +25 -0
  31. data/lib/hanami/cli/commands/monolith/db/create_migration.rb +29 -0
  32. data/lib/hanami/cli/commands/monolith/db/drop.rb +25 -0
  33. data/lib/hanami/cli/commands/monolith/db/migrate.rb +40 -0
  34. data/lib/hanami/cli/commands/monolith/db/reset.rb +26 -0
  35. data/lib/hanami/cli/commands/monolith/db/rollback.rb +55 -0
  36. data/lib/hanami/cli/commands/monolith/db/sample_data.rb +40 -0
  37. data/lib/hanami/cli/commands/monolith/db/seed.rb +40 -0
  38. data/lib/hanami/cli/commands/monolith/db/setup.rb +24 -0
  39. data/lib/hanami/cli/commands/monolith/db/structure/dump.rb +25 -0
  40. data/lib/hanami/cli/commands/monolith/db/version.rb +26 -0
  41. data/lib/hanami/cli/commands/monolith/generate.rb +14 -0
  42. data/lib/hanami/cli/commands/monolith/generate/action.rb +62 -0
  43. data/lib/hanami/cli/commands/monolith/generate/slice.rb +62 -0
  44. data/lib/hanami/cli/commands/monolith/install.rb +16 -0
  45. data/lib/hanami/cli/commands/monolith/version.rb +18 -0
  46. data/lib/hanami/cli/error.rb +8 -0
  47. data/lib/hanami/cli/generators/context.rb +38 -0
  48. data/lib/hanami/cli/generators/gem/application.rb +21 -0
  49. data/lib/hanami/cli/generators/gem/application/monolith.rb +83 -0
  50. data/lib/hanami/cli/generators/gem/application/monolith/action.erb +21 -0
  51. data/lib/hanami/cli/generators/gem/application/monolith/application.erb +8 -0
  52. data/lib/hanami/cli/generators/gem/application/monolith/config_ru.erb +5 -0
  53. data/lib/hanami/cli/generators/gem/application/monolith/entities.erb +9 -0
  54. data/lib/hanami/cli/generators/gem/application/monolith/env.erb +0 -0
  55. data/lib/hanami/cli/generators/gem/application/monolith/functions.erb +13 -0
  56. data/lib/hanami/cli/generators/gem/application/monolith/gemfile.erb +19 -0
  57. data/lib/hanami/cli/generators/gem/application/monolith/keep.erb +0 -0
  58. data/lib/hanami/cli/generators/gem/application/monolith/operation.erb +18 -0
  59. data/lib/hanami/cli/generators/gem/application/monolith/rakefile.erb +3 -0
  60. data/lib/hanami/cli/generators/gem/application/monolith/readme.erb +1 -0
  61. data/lib/hanami/cli/generators/gem/application/monolith/repository.erb +13 -0
  62. data/lib/hanami/cli/generators/gem/application/monolith/routes.erb +4 -0
  63. data/lib/hanami/cli/generators/gem/application/monolith/settings.erb +6 -0
  64. data/lib/hanami/cli/generators/gem/application/monolith/types.erb +10 -0
  65. data/lib/hanami/cli/generators/gem/application/monolith/validation_contract.erb +14 -0
  66. data/lib/hanami/cli/generators/gem/application/monolith/view_context.erb +15 -0
  67. data/lib/hanami/cli/generators/monolith/action.rb +123 -0
  68. data/lib/hanami/cli/generators/monolith/action/action.erb +13 -0
  69. data/lib/hanami/cli/generators/monolith/action/template.erb +0 -0
  70. data/lib/hanami/cli/generators/monolith/action/template.html.erb +2 -0
  71. data/lib/hanami/cli/generators/monolith/action/view.erb +13 -0
  72. data/lib/hanami/cli/generators/monolith/action_context.rb +76 -0
  73. data/lib/hanami/cli/generators/monolith/slice.rb +56 -0
  74. data/lib/hanami/cli/generators/monolith/slice/action.erb +9 -0
  75. data/lib/hanami/cli/generators/monolith/slice/entities.erb +9 -0
  76. data/lib/hanami/cli/generators/monolith/slice/keep.erb +0 -0
  77. data/lib/hanami/cli/generators/monolith/slice/repository.erb +10 -0
  78. data/lib/hanami/cli/generators/monolith/slice/routes.erb +2 -0
  79. data/lib/hanami/cli/generators/monolith/slice/view.erb +9 -0
  80. data/lib/hanami/cli/generators/monolith/slice_context.rb +33 -0
  81. data/lib/hanami/cli/repl/core.rb +55 -0
  82. data/lib/hanami/cli/repl/irb.rb +41 -0
  83. data/lib/hanami/cli/repl/pry.rb +29 -0
  84. data/lib/hanami/cli/system_call.rb +51 -0
  85. data/lib/hanami/cli/url.rb +34 -0
  86. data/lib/hanami/cli/version.rb +2 -3
  87. data/lib/hanami/console/context.rb +39 -0
  88. data/lib/hanami/console/plugins/slice_readers.rb +42 -0
  89. data/lib/hanami/rake_tasks.rb +52 -0
  90. metadata +134 -43
  91. data/.circleci/config.yml +0 -63
  92. data/.travis.yml +0 -22
  93. data/lib/hanami/cli/banner.rb +0 -129
  94. data/lib/hanami/cli/command_registry.rb +0 -215
  95. data/lib/hanami/cli/errors.rb +0 -46
  96. data/lib/hanami/cli/option.rb +0 -134
  97. data/lib/hanami/cli/parser.rb +0 -144
  98. data/lib/hanami/cli/program_name.rb +0 -21
  99. data/lib/hanami/cli/registry.rb +0 -330
  100. data/lib/hanami/cli/usage.rb +0 -91
  101. data/script/ci +0 -61
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0fe57714d6c897c21dd896eac31b8075fc52a7d747a2b9d7745bddebc4233818
4
- data.tar.gz: 76217ba7ae795a50d4c37c92f837d826f009b178cf8f2d169985cfde35abff8f
3
+ metadata.gz: 9de5812f665993e5c2ef2df77e3bee3f78c0917b3089f2bb822b0eab04e2d8e7
4
+ data.tar.gz: 1cc451a367b2ecd1959b30604808d99efb39a819bba9726e7f0f3ef0dc98dd6d
5
5
  SHA512:
6
- metadata.gz: 3e61b952023ef34ce48c2c24a3108e3b9087c7712e961a4af0c6eaee42aa707f661aba35f733468aec27ee02924578c9e65a12f499fc5a00e288666561c584fb
7
- data.tar.gz: 9d04ca91f9372567f72d852222e82caf639019df436c9c58b31fd828ad841ec3b75f93bdce08c585ac55345e71478c27f05cf4b88fdf8e3e88ba50bcbfcdd279
6
+ metadata.gz: 8ff9f2bf4104febfee8cdcd230fcd56b6bb54c18651cd0c1e8acfa58ebb216ec70060e7e4ea331b11bd56c67f8236b6f48ad212a514c28489a0cfa19acc5e761
7
+ data.tar.gz: 304bf695102705acb0fbc1f64729c6111ffd3aa464770854826c6554e2e8a48ed8b12028f353b123e05951570e871468f98db3b4547fe7440f20381503e551ca
@@ -0,0 +1,42 @@
1
+ name: ci
2
+
3
+ "on":
4
+ push:
5
+ paths:
6
+ - ".github/workflows/ci.yml"
7
+ - "lib/**"
8
+ - "*.gemspec"
9
+ - "spec/**"
10
+ - "Rakefile"
11
+ - "Gemfile"
12
+ - ".rubocop.yml"
13
+ pull_request:
14
+ branches:
15
+ - master
16
+ create:
17
+
18
+ jobs:
19
+ tests:
20
+ runs-on: ubuntu-latest
21
+ strategy:
22
+ fail-fast: false
23
+ matrix:
24
+ ruby:
25
+ - "3.0"
26
+ - "2.7"
27
+ - "2.6"
28
+ steps:
29
+ - uses: actions/checkout@v1
30
+ - name: Install package dependencies
31
+ run: "[ -e $APT_DEPS ] || sudo apt-get install -y --no-install-recommends $APT_DEPS"
32
+ - name: Set up Ruby
33
+ uses: ruby/setup-ruby@v1
34
+ with:
35
+ ruby-version: ${{matrix.ruby}}
36
+ - name: Install latest bundler
37
+ run: |
38
+ gem install bundler --no-document
39
+ - name: Bundle install
40
+ run: bundle install --jobs 4 --retry 3
41
+ - name: Run all tests
42
+ run: bundle exec rake spec
data/.gitignore CHANGED
@@ -1,11 +1,13 @@
1
1
  /.bundle/
2
2
  /.yardoc
3
- /Gemfile.lock
4
3
  /_yardoc/
5
4
  /coverage/
6
5
  /doc/
7
6
  /pkg/
8
7
  /spec/reports/
9
8
  /tmp/
10
- .byebug_history
11
9
  .rubocop-*
10
+ Gemfile.lock
11
+
12
+ # rspec failure tracking
13
+ .rspec_status
data/.rspec CHANGED
@@ -1,2 +1,3 @@
1
+ --format documentation
1
2
  --color
2
3
  --require spec_helper
data/.rubocop.yml CHANGED
@@ -1,4 +1,28 @@
1
1
  # Please keep AllCops, Bundler, Style, Metrics groups and then order cops
2
2
  # alphabetically
3
+ AllCops:
4
+ SuggestExtensions: false
3
5
  inherit_from:
4
6
  - https://raw.githubusercontent.com/hanami/devtools/master/.rubocop-unstable.yml
7
+ Layout/LineLength:
8
+ Exclude:
9
+ - Gemfile
10
+ Naming/HeredocDelimiterNaming:
11
+ Enabled: false
12
+ Naming/MethodParameterName:
13
+ AllowedNames:
14
+ - fs
15
+ Style/AccessorGrouping:
16
+ Enabled: false
17
+ Style/BlockDelimiters:
18
+ Enabled: false
19
+ Style/CommentedKeyword:
20
+ Enabled: false
21
+ Style/LambdaCall:
22
+ Enabled: false
23
+ Style/TrailingCommaInArguments:
24
+ Enabled: false
25
+ Style/TrailingCommaInArrayLiteral:
26
+ Enabled: false
27
+ Style/TrailingCommaInHashLiteral:
28
+ Enabled: false
data/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # Hanami::CLI
2
- General purpose Command Line Interface (CLI) framework for Ruby
2
+ Hanami Command Line Interface
3
+
4
+ ## v2.0.0.alpha2 - 2021-05-04
5
+ ### Added
6
+ - [Luca Guidi] Official support for Ruby: MRI 3.0
7
+ - [Luca Guidi] Dynamically change the set of available commands depending on the context (outside or inside an Hanami app)
8
+ - [Luca Guidi] Dynamically change the set of available commands depending on Hanami app architecture
9
+ - [Luca Guidi] Implemented `hanami version` (available both outside and inside an Hanami app)
10
+ - [Piotr Solnica] Implemented `db *` commands (available both outside and inside an Hanami app) (sqlite and postgres only for now)
11
+ - [Piotr Solnica] Implemented `console` command with support for `IRB` and `Pry` (`pry` is auto-detected)
12
+
13
+ ### Changed
14
+ - [Luca Guidi] Changed the purpose of this gem: the CLI Ruby framework has been extracted into `dry-cli` gem. `hanami-cli` is now the `hanami` command line.
15
+ - [Luca Guidi] Drop support for Ruby: MRI 2.5.
3
16
 
4
17
  ## v1.0.0.alpha1 - 2019-01-30
5
18
  ### Added
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at me@lucaguidi.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile CHANGED
@@ -8,5 +8,11 @@ unless ENV["CI"]
8
8
  gem "yard", require: false
9
9
  end
10
10
 
11
- gem "hanami-utils", "~> 2.0.alpha", require: false, git: "https://github.com/hanami/utils.git", branch: "unstable"
12
- gem "hanami-devtools", require: false, git: "https://github.com/hanami/devtools.git"
11
+ gem "hanami", require: false, git: "https://github.com/hanami/hanami.git", branch: "unstable"
12
+ gem "hanami-view", require: false, git: "https://github.com/hanami/view.git", branch: "unstable"
13
+
14
+ gem "rack"
15
+
16
+ group :test do
17
+ gem "pry"
18
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Luca Guidi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md CHANGED
@@ -1,30 +1,6 @@
1
1
  # Hanami::CLI
2
2
 
3
- General purpose Command Line Interface (CLI) framework for Ruby.
4
-
5
- :warning: **This is a general framework for Ruby (aka `thor` gem replacement), NOT the implementation of the `hanami` CLI commands** :warning:
6
-
7
- ## Status
8
-
9
- [![Gem Version](https://badge.fury.io/rb/hanami-cli.svg)](https://badge.fury.io/rb/hanami-cli)
10
- [![TravisCI](https://travis-ci.org/hanami/cli.svg?branch=master)](https://travis-ci.org/hanami/cli)
11
- [![CircleCI](https://circleci.com/gh/hanami/cli/tree/master.svg?style=svg)](https://circleci.com/gh/hanami/cli/tree/master)
12
- [![Test Coverage](https://codecov.io/gh/hanami/cli/branch/master/graph/badge.svg)](https://codecov.io/gh/hanami/cli)
13
- [![Depfu](https://badges.depfu.com/badges/2c1bc076f16c6b5508334c44b5800362/overview.svg)](https://depfu.com/github/hanami/cli?project=Bundler)
14
- [![Inline Docs](http://inch-ci.org/github/hanami/cli.svg)](http://inch-ci.org/github/hanami/cli)
15
-
16
- ## Contact
17
-
18
- * Home page: http://hanamirb.org
19
- * Mailing List: http://hanamirb.org/mailing-list
20
- * API Doc: http://rdoc.info/gems/hanami-cli
21
- * Bugs/Issues: https://github.com/hanami/cli/issues
22
- * Support: http://stackoverflow.com/questions/tagged/hanami
23
- * Chat: http://chat.hanamirb.org
24
-
25
- ## Rubies
26
-
27
- __Hanami::CLI__ supports Ruby (MRI) 2.5+
3
+ [Hanami](http://hanamirb.org) command line
28
4
 
29
5
  ## Installation
30
6
 
@@ -36,269 +12,7 @@ gem 'hanami-cli'
36
12
 
37
13
  And then execute:
38
14
 
39
- $ bundle
40
-
41
- Or install it yourself as:
42
-
43
- $ gem install hanami-cli
44
-
45
- <!-- Tocer[start]: Auto-generated, don't remove. -->
46
-
47
- ## Table of Contents
48
-
49
- - [Features](#features)
50
- - [Registration](#registration)
51
- - [Commands as objects](#commands-as-objects)
52
- - [Subcommands](#subcommands)
53
- - [Arguments](#arguments)
54
- - [Option](#option)
55
- - [Variadic arguments](#variadic-arguments)
56
- - [Installation](#installation)
57
- - [Usage](#usage)
58
- - [Available commands](#available-commands)
59
- - [Help](#help)
60
- - [Optional arguments](#optional-arguments)
61
- - [Required arguments](#required-arguments)
62
- - [Options](#options)
63
- - [Boolean options](#boolean-options)
64
- - [Array options](#array-options)
65
- - [Subcommands](#subcommands-1)
66
- - [Aliases](#aliases)
67
- - [Subcommand aliases](#subcommand-aliases)
68
- - [Callbacks](#callbacks)
69
- - [Development](#development)
70
- - [Contributing](#contributing)
71
- - [Alternatives](#alternatives)
72
- - [Copyright](#copyright)
73
-
74
- <!-- Tocer[finish]: Auto-generated, don't remove. -->
75
-
76
- ## Features
77
-
78
- ### Registration
79
-
80
- For a given _command name_, you can register a corresponding _command object_ (aka command).
81
-
82
- Example: for `foo hi` _command name_ there is the corresponding `Foo::CLI::Hello` _command object_.
83
-
84
- ```ruby
85
- #!/usr/bin/env ruby
86
- require "bundler/setup"
87
- require "hanami/cli"
88
-
89
- module Foo
90
- module CLI
91
- module Commands
92
- extend Hanami::CLI::Registry
93
-
94
- class Hello < Hanami::CLI::Command
95
- def call(*)
96
- end
97
- end
98
- end
99
- end
100
- end
101
-
102
- class Version < Hanami::CLI::Command
103
- def call(*)
104
- end
105
- end
106
-
107
- Foo::CLI::Commands.register "hi", Foo::CLI::Commands::Hello
108
- Foo::CLI::Commands.register "v", Version
109
-
110
- Hanami::CLI.new(Foo::CLI::Commands).call
111
- ```
112
-
113
- **Please note:** there is NOT a convention between the _command name_ and the _command object_ class.
114
- The manual _registration_ assigns a _command object_ to a _command name_.
115
-
116
- ### Commands as objects
117
-
118
- A command is a subclass of `Hanami::CLI::Command` and it MUST respond to `#call(*)`.
119
-
120
- ### Subcommands
121
-
122
- There is nothing special in subcommands: they are just _command objects_ registered under a **nested** _command name_.
123
-
124
- ```ruby
125
- #!/usr/bin/env ruby
126
- require "bundler/setup"
127
- require "hanami/cli"
128
-
129
- module Foo
130
- module CLI
131
- module Commands
132
- extend Hanami::CLI::Registry
133
-
134
- module Generate
135
- class Configuration < Hanami::CLI::Command
136
- def call(*)
137
- end
138
- end
139
- end
140
- end
141
- end
142
- end
143
-
144
- Foo::CLI::Commands.register "generate configuration", Foo::CLI::Commands::Generate::Configuration
145
-
146
- Hanami::CLI.new(Foo::CLI::Commands).call
147
- ```
148
-
149
- ### Arguments
150
-
151
- An argument is a token passed after the _command name_.
152
- For instance, given the `foo greet` command, when an user types `foo greet Luca`, then `Luca` is considered an argument.
153
- A command can accept none or many arguments.
154
- An argument can be declared as _required_.
155
-
156
- ```ruby
157
- #!/usr/bin/env ruby
158
- require "bundler/setup"
159
- require "hanami/cli"
160
-
161
- module Foo
162
- module CLI
163
- module Commands
164
- extend Hanami::CLI::Registry
165
-
166
- class Greet < Hanami::CLI::Command
167
- argument :name, required: true, desc: "The name of the person to greet"
168
- argument :age, desc: "The age of the person to greet"
169
-
170
- def call(name:, age: nil, **)
171
- result = "Hello, #{name}."
172
- result = "#{result} You are #{age} years old." unless age.nil?
173
-
174
- puts result
175
- end
176
- end
177
-
178
- register "greet", Greet
179
- end
180
- end
181
- end
182
-
183
- Hanami::CLI.new(Foo::CLI::Commands).call
184
- ```
185
-
186
- ```shell
187
- % foo greet Luca
188
- Hello, Luca.
189
- ```
190
-
191
- ```shell
192
- % foo greet Luca 35
193
- Hello, Luca. You are 35 years old.
194
- ```
195
-
196
- ```shell
197
- % foo greet
198
- ERROR: "foo greet" was called with no arguments
199
- Usage: "foo greet NAME"
200
- ```
201
-
202
- ### Option
203
-
204
- An option is a named argument that is passed after the _command name_ **and** the arguments.
205
-
206
- For instance, given the `foo request` command, when an user types `foo request --mode=http2`, then `--mode=http2` is considered an option.
207
- A command can accept none or many options.
208
-
209
- ```ruby
210
- #!/usr/bin/env ruby
211
- require "bundler/setup"
212
- require "hanami/cli"
213
-
214
- module Foo
215
- module CLI
216
- module Commands
217
- extend Hanami::CLI::Registry
218
-
219
- class Request < Hanami::CLI::Command
220
- option :mode, default: "http", values: %w[http http2], desc: "The request mode"
221
-
222
- def call(**options)
223
- puts "Performing a request (mode: #{options.fetch(:mode)})"
224
- end
225
- end
226
-
227
- register "request", Request
228
- end
229
- end
230
- end
231
-
232
- Hanami::CLI.new(Foo::CLI::Commands).call
233
- ```
234
-
235
- ```shell
236
- % foo request
237
- Performing a request (mode: http)
238
- ```
239
-
240
- ```shell
241
- % foo request --mode=http2
242
- Performing a request (mode: http2)
243
- ```
244
-
245
- ```shell
246
- % foo request --mode=unknown
247
- Error: "request" was called with arguments "--mode=unknown"
248
- ```
249
-
250
- ### Variadic arguments
251
-
252
- Sometimes we need extra arguments because those will be forwarded to a sub-command like `ssh`, `docker` or `cat`.
253
-
254
- By using `--` (double dash, aka hypen), the user indicates the end of the arguments and options belonging to the main command, and the beginning of the variadic arguments that can be forwarded to the sub-command.
255
- These extra arguments are included as `:args` in the keyword arguments available for each command.
256
-
257
- ```ruby
258
- #!/usr/bin/env ruby
259
- require "bundler/setup"
260
- require "hanami/cli"
261
-
262
- module Foo
263
- module CLI
264
- module Commands
265
- extend Hanami::CLI::Registry
266
-
267
- class Runner < Hanami::CLI::Command
268
- argument :image, required: true, desc: "Docker image"
269
-
270
- def call(image:, args: [], **)
271
- puts `docker run -it --rm #{image} #{args.join(" ")}`
272
- end
273
- end
274
-
275
- register "run", Runner
276
- end
277
- end
278
- end
279
-
280
- Hanami::CLI.new(Foo::CLI::Commands).call
281
- ```
282
-
283
- ```shell
284
- % foo run ruby:latest -- ruby -v
285
- ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]
286
- ```
287
-
288
- The user separates via `--` the arguments for `foo` and the command has to be run by the Docker container.
289
- In this specific case, `ruby:latest` corresponds to the `image` mandatory argument for `foo`, whereas `ruby -v` is the variadic argument that is passed to Docker via `args`.
290
-
291
- ## Installation
292
-
293
- Add this line to your application's Gemfile:
294
-
295
- ```ruby
296
- gem "hanami-cli"
297
- ```
298
-
299
- And then execute:
300
-
301
- $ bundle
15
+ $ bundle install
302
16
 
303
17
  Or install it yourself as:
304
18
 
@@ -306,331 +20,24 @@ Or install it yourself as:
306
20
 
307
21
  ## Usage
308
22
 
309
- Imagine to build a CLI executable `foo` for your Ruby project.
310
-
311
- ```ruby
312
- #!/usr/bin/env ruby
313
- require "bundler/setup"
314
- require "hanami/cli"
315
-
316
- module Foo
317
- module CLI
318
- module Commands
319
- extend Hanami::CLI::Registry
320
-
321
- class Version < Hanami::CLI::Command
322
- desc "Print version"
323
-
324
- def call(*)
325
- puts "1.0.0"
326
- end
327
- end
328
-
329
- class Echo < Hanami::CLI::Command
330
- desc "Print input"
331
-
332
- argument :input, desc: "Input to print"
333
-
334
- example [
335
- " # Prints 'wuh?'",
336
- "hello, folks # Prints 'hello, folks'"
337
- ]
338
-
339
- def call(input: nil, **)
340
- if input.nil?
341
- puts "wuh?"
342
- else
343
- puts input
344
- end
345
- end
346
- end
347
-
348
- class Start < Hanami::CLI::Command
349
- desc "Start Foo machinery"
350
-
351
- argument :root, required: true, desc: "Root directory"
352
-
353
- example [
354
- "path/to/root # Start Foo at root directory"
355
- ]
356
-
357
- def call(root:, **)
358
- puts "started - root: #{root}"
359
- end
360
- end
361
-
362
- class Stop < Hanami::CLI::Command
363
- desc "Stop Foo machinery"
364
-
365
- option :graceful, type: :boolean, default: true, desc: "Graceful stop"
366
-
367
- def call(**options)
368
- puts "stopped - graceful: #{options.fetch(:graceful)}"
369
- end
370
- end
371
-
372
- class Exec < Hanami::CLI::Command
373
- desc "Execute a task"
374
-
375
- argument :task, type: :string, required: true, desc: "Task to be executed"
376
- argument :dirs, type: :array, required: false, desc: "Optional directories"
377
-
378
- def call(task:, dirs: [], **)
379
- puts "exec - task: #{task}, dirs: #{dirs.inspect}"
380
- end
381
- end
382
-
383
- module Generate
384
- class Configuration < Hanami::CLI::Command
385
- desc "Generate configuration"
386
-
387
- option :apps, type: :array, default: [], desc: "Generate configuration for specific apps"
388
-
389
- def call(apps:, **)
390
- puts "generated configuration for apps: #{apps.inspect}"
391
- end
392
- end
393
-
394
- class Test < Hanami::CLI::Command
395
- desc "Generate tests"
396
-
397
- option :framework, default: "minitest", values: %w[minitest rspec]
398
-
399
- def call(framework:, **)
400
- puts "generated tests - framework: #{framework}"
401
- end
402
- end
403
- end
404
-
405
- register "version", Version, aliases: ["v", "-v", "--version"]
406
- register "echo", Echo
407
- register "start", Start
408
- register "stop", Stop
409
- register "exec", Exec
410
-
411
- register "generate", aliases: ["g"] do |prefix|
412
- prefix.register "config", Generate::Configuration
413
- prefix.register "test", Generate::Test
414
- end
415
- end
416
- end
417
- end
418
-
419
- Hanami::CLI.new(Foo::CLI::Commands).call
420
- ```
421
-
422
- Let's have a look at the command line usage.
423
-
424
- ### Available commands
425
-
426
- ```shell
427
- % foo
428
- Commands:
429
- foo echo [INPUT] # Print input
430
- foo exec TASK [DIRS] # Execute a task
431
- foo generate [SUBCOMMAND]
432
- foo start ROOT # Start Foo machinery
433
- foo stop # Stop Foo machinery
434
- foo version # Print version
435
- ```
436
-
437
- ### Help
438
-
439
- ```shell
440
- % foo echo --help
441
- Command:
442
- foo echo
443
-
444
- Usage:
445
- foo echo [INPUT]
446
-
447
- Description:
448
- Print input
449
-
450
- Arguments:
451
- INPUT # Input to print
452
-
453
- Options:
454
- --help, -h # Print this help
455
-
456
- Examples:
457
- foo echo # Prints 'wuh?'
458
- foo echo hello, folks # Prints 'hello, folks'
459
- ```
460
-
461
- ### Optional arguments
462
-
463
- ```shell
464
- % foo echo
465
- wuh?
466
-
467
- % foo echo hello
468
- hello
469
- ```
470
-
471
- ### Required arguments
472
-
473
- ```shell
474
- % foo start .
475
- started - root: .
476
- ```
477
-
478
- ```shell
479
- % foo start
480
- ERROR: "foo start" was called with no arguments
481
- Usage: "foo start ROOT"
482
- ```
483
-
484
- ### Array arguments
485
-
486
- Captures all the remaining arguments in a single array.
487
- Please note that `array` argument must be used as last argument as it works as a _"catch-all"_.
488
-
489
- ```shell
490
- % foo exec test
491
- exec - task: test, dirs: []
492
- ```
493
-
494
- ```shell
495
- % foo exec test spec/bookshelf/entities spec/bookshelf/repositories
496
- exec - task: test, dirs: ["spec/bookshelf/entities", "spec/bookshelf/repositories"]
497
- ```
498
-
499
- ### Options
500
-
501
- ```shell
502
- % foo generate test
503
- generated tests - framework: minitest
504
- ```
505
-
506
- ```shell
507
- % foo generate test --framework=rspec
508
- generated tests - framework: rspec
509
- ```
510
-
511
- ```shell
512
- % foo generate test --framework=unknown
513
- Error: "test" was called with arguments "--framework=unknown"
514
- ```
515
-
516
- ### Boolean options
517
-
518
- ```shell
519
- % foo stop
520
- stopped - graceful: true
521
- ```
522
-
523
- ```shell
524
- % foo stop --no-graceful
525
- stopped - graceful: false
526
- ```
527
-
528
- ### Array options
529
-
530
- ```shell
531
- % foo generate config --apps=web,api
532
- generated configuration for apps: ["web", "api"]
533
- ```
534
-
535
- ### Subcommands
536
-
537
- ```shell
538
- % foo generate
539
- Commands:
540
- foo generate config # Generate configuration
541
- foo generate test # Generate tests
542
- ```
543
-
544
- ### Aliases
545
-
546
- ```shell
547
- % foo version
548
- 1.0.0
549
- ```
550
-
551
- ```shell
552
- % foo v
553
- 1.0.0
554
- ```
555
-
556
- ```shell
557
- % foo -v
558
- 1.0.0
559
- ```
560
-
561
- ```shell
562
- % foo --version
563
- 1.0.0
564
- ```
565
-
566
- ### Subcommand aliases
567
-
568
- ```shell
569
- % foo g config
570
- generated configuration for apps: []
571
- ```
572
-
573
- ### Callbacks
574
-
575
- Third party gems can register _before_ and _after_ callbacks to enhance a command.
576
-
577
- From the `foo` gem we have a command `hello`.
578
-
579
- ```ruby
580
- #!/usr/bin/env ruby
581
- require "hanami/cli"
582
-
583
- module Foo
584
- module CLI
585
- module Commands
586
- extend Hanami::CLI::Registry
587
-
588
- class Hello < Hanami::CLI::Command
589
- argument :name, required: true
590
-
591
- def call(name:, **)
592
- puts "hello #{name}"
593
- end
594
- end
595
- end
596
- end
597
- end
598
-
599
- Foo::CLI::Commands.register "hello", Foo::CLI::Commands::Hello
600
-
601
- cli = Hanami::CLI.new(Foo::CLI::Commands)
602
- cli.call
603
- ```
604
-
605
- The `foo-bar` gem enhances `hello` command with callbacks:
606
-
607
- ```
608
- Foo::CLI::Commands.before("hello") { |args| puts "debug: #{args.inspect}" } # syntax 1
609
- Foo::CLI::Commands.after "hello", &->(args) { puts "bye, #{args.fetch(:name)}" } # syntax 2
610
- ```
611
-
612
- ```shell
613
- % foo hello Anton
614
- debug: {:name=>"Anton"}
615
- hello Anton
616
- bye, Anton
617
- ```
618
-
619
23
  ## Development
620
24
 
621
- After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
25
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
622
26
 
623
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
27
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
624
28
 
625
29
  ## Contributing
626
30
 
627
- Bug reports and pull requests are welcome on GitHub at https://github.com/hanami/cli.
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/hanami/cli. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/hanami/cli/blob/main/CODE_OF_CONDUCT.md).
32
+
33
+ ## License
34
+
35
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
628
36
 
629
- ## Alternatives
37
+ ## Code of Conduct
630
38
 
631
- * [thor](http://whatisthor.com/)
632
- * [clamp](https://github.com/mdub/clamp)
39
+ Everyone interacting in the Hanami::Cli project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/hanami/cli/blob/main/CODE_OF_CONDUCT.md).
633
40
 
634
41
  ## Copyright
635
42
 
636
- Copyright © 2017-2019 Luca Guidi – Released under MIT License
43
+ Copyright © 2014-2021 Luca Guidi – Released under MIT License