tomo 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (135) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +21 -0
  3. data/README.md +221 -0
  4. data/exe/tomo +4 -0
  5. data/lib/tomo/cli/command.rb +36 -0
  6. data/lib/tomo/cli/common_options.rb +48 -0
  7. data/lib/tomo/cli/completions.rb +70 -0
  8. data/lib/tomo/cli/deploy_options.rb +59 -0
  9. data/lib/tomo/cli/error.rb +16 -0
  10. data/lib/tomo/cli/interrupted_error.rb +9 -0
  11. data/lib/tomo/cli/options.rb +38 -0
  12. data/lib/tomo/cli/parser.rb +92 -0
  13. data/lib/tomo/cli/project_options.rb +47 -0
  14. data/lib/tomo/cli/rules/argument.rb +42 -0
  15. data/lib/tomo/cli/rules/switch.rb +43 -0
  16. data/lib/tomo/cli/rules/value_switch.rb +58 -0
  17. data/lib/tomo/cli/rules.rb +98 -0
  18. data/lib/tomo/cli/rules_evaluator.rb +71 -0
  19. data/lib/tomo/cli/state.rb +29 -0
  20. data/lib/tomo/cli/usage.rb +42 -0
  21. data/lib/tomo/cli.rb +75 -0
  22. data/lib/tomo/colors.rb +46 -0
  23. data/lib/tomo/commands/completion_script.rb +46 -0
  24. data/lib/tomo/commands/default.rb +72 -0
  25. data/lib/tomo/commands/deploy.rb +67 -0
  26. data/lib/tomo/commands/help.rb +9 -0
  27. data/lib/tomo/commands/init.rb +92 -0
  28. data/lib/tomo/commands/run.rb +76 -0
  29. data/lib/tomo/commands/setup.rb +54 -0
  30. data/lib/tomo/commands/tasks.rb +32 -0
  31. data/lib/tomo/commands/version.rb +23 -0
  32. data/lib/tomo/commands.rb +13 -0
  33. data/lib/tomo/configuration/dsl/batch_block.rb +17 -0
  34. data/lib/tomo/configuration/dsl/config_file.rb +39 -0
  35. data/lib/tomo/configuration/dsl/environment_block.rb +13 -0
  36. data/lib/tomo/configuration/dsl/error_formatter.rb +75 -0
  37. data/lib/tomo/configuration/dsl/hosts_and_settings.rb +24 -0
  38. data/lib/tomo/configuration/dsl/tasks_block.rb +24 -0
  39. data/lib/tomo/configuration/dsl.rb +12 -0
  40. data/lib/tomo/configuration/environment.rb +12 -0
  41. data/lib/tomo/configuration/glob.rb +26 -0
  42. data/lib/tomo/configuration/plugin_file_not_found_error.rb +14 -0
  43. data/lib/tomo/configuration/plugin_resolver.rb +63 -0
  44. data/lib/tomo/configuration/plugins_registry/file_resolver.rb +43 -0
  45. data/lib/tomo/configuration/plugins_registry/gem_resolver.rb +63 -0
  46. data/lib/tomo/configuration/plugins_registry.rb +67 -0
  47. data/lib/tomo/configuration/project_not_found_error.rb +28 -0
  48. data/lib/tomo/configuration/role_based_task_filter.rb +42 -0
  49. data/lib/tomo/configuration/unknown_environment_error.rb +46 -0
  50. data/lib/tomo/configuration/unknown_plugin_error.rb +28 -0
  51. data/lib/tomo/configuration/unspecified_environment_error.rb +28 -0
  52. data/lib/tomo/configuration.rb +124 -0
  53. data/lib/tomo/console/key_reader.rb +51 -0
  54. data/lib/tomo/console/menu.rb +109 -0
  55. data/lib/tomo/console.rb +33 -0
  56. data/lib/tomo/error/suggestions.rb +44 -0
  57. data/lib/tomo/error.rb +22 -0
  58. data/lib/tomo/host.rb +57 -0
  59. data/lib/tomo/logger/tagged_io.rb +38 -0
  60. data/lib/tomo/logger.rb +70 -0
  61. data/lib/tomo/path.rb +19 -0
  62. data/lib/tomo/paths.rb +36 -0
  63. data/lib/tomo/plugin/bundler/helpers.rb +14 -0
  64. data/lib/tomo/plugin/bundler/tasks.rb +57 -0
  65. data/lib/tomo/plugin/bundler.rb +17 -0
  66. data/lib/tomo/plugin/core/helpers.rb +65 -0
  67. data/lib/tomo/plugin/core/tasks.rb +138 -0
  68. data/lib/tomo/plugin/core.rb +31 -0
  69. data/lib/tomo/plugin/env/tasks.rb +113 -0
  70. data/lib/tomo/plugin/env.rb +13 -0
  71. data/lib/tomo/plugin/git/helpers.rb +11 -0
  72. data/lib/tomo/plugin/git/tasks.rb +78 -0
  73. data/lib/tomo/plugin/git.rb +19 -0
  74. data/lib/tomo/plugin/nvm/tasks.rb +61 -0
  75. data/lib/tomo/plugin/nvm.rb +14 -0
  76. data/lib/tomo/plugin/puma/tasks.rb +38 -0
  77. data/lib/tomo/plugin/puma.rb +12 -0
  78. data/lib/tomo/plugin/rails/helpers.rb +20 -0
  79. data/lib/tomo/plugin/rails/tasks.rb +79 -0
  80. data/lib/tomo/plugin/rails.rb +11 -0
  81. data/lib/tomo/plugin/rbenv/tasks.rb +55 -0
  82. data/lib/tomo/plugin/rbenv.rb +12 -0
  83. data/lib/tomo/plugin/testing.rb +16 -0
  84. data/lib/tomo/plugin.rb +4 -0
  85. data/lib/tomo/plugin_dsl.rb +23 -0
  86. data/lib/tomo/remote.rb +55 -0
  87. data/lib/tomo/result.rb +28 -0
  88. data/lib/tomo/runtime/concurrent_ruby_load_error.rb +26 -0
  89. data/lib/tomo/runtime/concurrent_ruby_thread_pool.rb +50 -0
  90. data/lib/tomo/runtime/context.rb +21 -0
  91. data/lib/tomo/runtime/current.rb +41 -0
  92. data/lib/tomo/runtime/execution_plan.rb +107 -0
  93. data/lib/tomo/runtime/host_execution_step.rb +49 -0
  94. data/lib/tomo/runtime/inline_thread_pool.rb +27 -0
  95. data/lib/tomo/runtime/privileged_task.rb +6 -0
  96. data/lib/tomo/runtime/settings_interpolation.rb +55 -0
  97. data/lib/tomo/runtime/settings_required_error.rb +33 -0
  98. data/lib/tomo/runtime/task_aborted_error.rb +15 -0
  99. data/lib/tomo/runtime/task_runner.rb +56 -0
  100. data/lib/tomo/runtime/unknown_task_error.rb +23 -0
  101. data/lib/tomo/runtime.rb +82 -0
  102. data/lib/tomo/script.rb +44 -0
  103. data/lib/tomo/shell_builder.rb +108 -0
  104. data/lib/tomo/ssh/child_process.rb +64 -0
  105. data/lib/tomo/ssh/connection.rb +82 -0
  106. data/lib/tomo/ssh/connection_error.rb +16 -0
  107. data/lib/tomo/ssh/connection_validator.rb +87 -0
  108. data/lib/tomo/ssh/error.rb +11 -0
  109. data/lib/tomo/ssh/executable_error.rb +21 -0
  110. data/lib/tomo/ssh/options.rb +67 -0
  111. data/lib/tomo/ssh/permission_error.rb +18 -0
  112. data/lib/tomo/ssh/script_error.rb +23 -0
  113. data/lib/tomo/ssh/unknown_error.rb +13 -0
  114. data/lib/tomo/ssh/unsupported_version_error.rb +15 -0
  115. data/lib/tomo/ssh.rb +36 -0
  116. data/lib/tomo/task_library.rb +51 -0
  117. data/lib/tomo/templates/config.rb.erb +66 -0
  118. data/lib/tomo/testing/Dockerfile +10 -0
  119. data/lib/tomo/testing/connection.rb +34 -0
  120. data/lib/tomo/testing/docker_image.rb +115 -0
  121. data/lib/tomo/testing/docker_plugin_tester.rb +39 -0
  122. data/lib/tomo/testing/host_extensions.rb +27 -0
  123. data/lib/tomo/testing/local.rb +75 -0
  124. data/lib/tomo/testing/mock_plugin_tester.rb +26 -0
  125. data/lib/tomo/testing/mocked_exec_error.rb +6 -0
  126. data/lib/tomo/testing/plugin_tester.rb +49 -0
  127. data/lib/tomo/testing/remote_extensions.rb +10 -0
  128. data/lib/tomo/testing/ssh_extensions.rb +13 -0
  129. data/lib/tomo/testing/tomo_test_ed25519 +7 -0
  130. data/lib/tomo/testing/tomo_test_ed25519.pub +1 -0
  131. data/lib/tomo/testing/ubuntu_setup.sh +33 -0
  132. data/lib/tomo/testing.rb +39 -0
  133. data/lib/tomo/version.rb +3 -0
  134. data/lib/tomo.rb +45 -0
  135. metadata +308 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a195d5ca53b88b0bb0b79a013fdf0526f4bbbca8d9e700960e4e055e9e82b520
4
+ data.tar.gz: 01f8f89d2bf321858481ec4581bce9c786ca461ebc4ddf730b9e984b94d904e1
5
+ SHA512:
6
+ metadata.gz: 7f59074b4183c57008dd6c15d05dc7c1acdb5b07c4836ab21ef4da4df10d7718af2e6e758873920a226b95dcba432a14bbb1c24a0938967a061a54de28ef20c2
7
+ data.tar.gz: 15c1f50a0d2843c055421bc3f345c0a08530c15aaf85bd3bff48ac059d299ee9eff346ce7ced1c594cc30f27b6750a587124e5f69805d9bedecd67f8adfd8971
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Matt Brictson
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 ADDED
@@ -0,0 +1,221 @@
1
+ # Tomo
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/tomo.svg)](https://rubygems.org/gems/tomo)
4
+ [![Travis](https://img.shields.io/travis/mattbrictson/tomo.svg?label=travis)](https://travis-ci.org/mattbrictson/tomo)
5
+ [![Circle](https://circleci.com/gh/mattbrictson/tomo.svg?style=shield)](https://circleci.com/gh/mattbrictson/tomo)
6
+ [![Code Climate](https://codeclimate.com/github/mattbrictson/tomo/badges/gpa.svg)](https://codeclimate.com/github/mattbrictson/tomo)
7
+ [![Say Thanks!](https://img.shields.io/badge/Say%20Thanks-!-1EAEDB.svg)](https://saythanks.io/to/mattbrictson)
8
+
9
+ Tomo is a friendly command-line tool for deploying Rails apps. It is a new alternative to Capistrano, Mina, and Shipit that optimizes for simplicity and developer happiness.
10
+
11
+ 💻 Rich command-line interface with built-in bash completions<br/>
12
+ ☁️ Multi-environment and role-based multi-host support<br/>
13
+ 💎 Everything you need to deploy a basic Rails app out of the box<br/>
14
+ 🔌 Easily extensible for polyglot projects (not just Rails!)<br/>
15
+ 💡 Concise, helpful error messages<br/>
16
+ 📚 Quality documentation<br/>
17
+ 🔬 Minimal dependencies<br/>
18
+
19
+ This project wouldn't be possible without the generosity of the open source Ruby community. Please support open source projects and your fellow developers by helping answer questions, contributing pull requests to improve code and documentation, or just [drop a note](https://saythanks.io/to/mattbrictson) to say thanks! ❤️
20
+
21
+ ---
22
+
23
+ - [Quick start](#quick-start)
24
+ - [Reference documentation](#reference-documentation)
25
+ - [FAQ](#faq)
26
+ - [Support](#support)
27
+ - [License](#license)
28
+ - [Code of conduct](#code-of-conduct)
29
+ - [Contribution guide](#contribution-guide) [TODO]
30
+
31
+ ## Quick start
32
+
33
+ Tomo is distributed as a ruby gem. To install:
34
+
35
+ ```
36
+ $ gem install tomo
37
+ ```
38
+
39
+ #### Getting help
40
+
41
+ An easy way to kick the tires is to view the `--help`.
42
+
43
+ ![$ tomo --help](./readme_images/tomo-help.png)
44
+
45
+ #### Configuring a project
46
+
47
+ Let’s init a project to see how tomo is configured.
48
+
49
+ ![$ tomo init](./readme_images/tomo-init.png)
50
+
51
+ The `.tomo/config.rb` file defines all the settings and tasks needed to setup and deploy a typical Rails project. An abbreviated version looks like this:
52
+
53
+ ```ruby
54
+ # .tomo/config.rb
55
+
56
+ plugin "git"
57
+ plugin "bundler"
58
+ plugin "rails"
59
+ # ...
60
+
61
+ host "user@hostname.or.ip.address"
62
+
63
+ set application: "my-rails-app"
64
+ set deploy_to: "/var/www/%<application>"
65
+ set git_url: "git@github.com:my-username/my-rails-app.git"
66
+ set git_branch: "master"
67
+ # ...
68
+
69
+ setup do
70
+ run "git:clone"
71
+ run "git:create_release"
72
+ run "bundler:install"
73
+ run "rails:db_schema_load"
74
+ # ...
75
+ end
76
+
77
+ deploy do
78
+ run "git:create_release"
79
+ run "core:symlink_shared"
80
+ run "core:write_release_json"
81
+ run "bundler:install"
82
+ run "rails:assets_precompile"
83
+ run "rails:db_migrate"
84
+ run "core:symlink_current"
85
+ # ...
86
+ end
87
+ ```
88
+
89
+ Eventually you'll want to edit the config file to specify the appropriate user and host, perhaps define some custom tasks, and tweak the settings to make them suitable for your Rails app. You can also take advantage of more advanced features like multiple hosts and environment-based config.
90
+
91
+ #### Host setup
92
+
93
+ With tomo, an initial deployment is separated into two distinct steps. The `setup` command prepares the host for its first deploy. Let’s take a look at the documentation with `--help`:
94
+
95
+ ![$ tomo setup --help](./readme_images/tomo-setup-help.png)
96
+
97
+ We can simulate the setup operation with the `--dry-run` option. Let's try it:
98
+
99
+ ![$ tomo setup --dry-run](./readme_images/tomo-setup-dry-run.png)
100
+
101
+ As you can see, the setup command in this project clones the git repository, installs ruby, node, bundler, and initializes the database. One the host is set up, it is ready for its first deploy.
102
+
103
+ #### Performing a deploy
104
+
105
+ Typically you only need to run `setup` once. From then on deploying a project is a matter of running the `deploy` command.
106
+
107
+ ![$ tomo deploy --help](./readme_images/tomo-deploy-help.png)
108
+
109
+ Like `setup`, this can be simulated with `--dry-run`, like this:
110
+
111
+ ![$ tomo deploy --dry-run](./readme_images/tomo-deploy-dry-run.png)
112
+
113
+ #### Running a single task
114
+
115
+ Tomo can also `run` individual remote tasks. Use the `tasks` command to see the list of tasks tomo knows about.
116
+
117
+ ![$ tomo tasks](./readme_images/tomo-tasks.png)
118
+
119
+ One of the built-in Rails tasks is `rails:console`, which brings up a fully-interactive Rails console over SSH. We can simulate this with `--dry-run` as well.
120
+
121
+ ![$ tomo run rails:console --dry-run](./readme_images/tomo-run-rails-console-dry-run.png)
122
+
123
+ #### Writing tasks
124
+
125
+ Tomo has many plugins built-in, but you can easily add your own to extend tomo with custom tasks. By convention, custom plugins are stored in `.tomo/plugins/`. These plugins can define tasks as plain ruby methods. For example:
126
+
127
+ ```ruby
128
+ # .tomo/plugins/my-plugin.rb
129
+
130
+ def hello
131
+ remote.run "echo", "hello", settings[:application]
132
+ end
133
+ ```
134
+
135
+ Use `remote.run` to execute shell scripts on the remote host, similar to how you would use Ruby's `system`. Project settings are accessible via `settings`, which is a plain Ruby hash.
136
+
137
+ Load your plugin in `config.rb` like this:
138
+
139
+ ```ruby
140
+ # .tomo/config.rb
141
+
142
+ plugin "./plugins/my-plugin.rb"
143
+ ```
144
+
145
+ And run it!
146
+
147
+ ![$ tomo run my-plugin:hello --dry-run](./readme_images/tomo-run-hello-dry-run.png)
148
+
149
+ #### Next steps
150
+
151
+ And just like that, you are now already familiar with the basics of tomo and how to extend it! Tomo is even more friendly and powerful with the help of bash completions. If you use bash, run `tomo completion-script` for instructions on setting them up.
152
+
153
+ To prepare your existing project for a real deploy, check out the sections of the reference documentation on [configuration](https://tomo-deploy.com/configuration/), [writing custom tasks](https://tomo-deploy.com/tutorials/writing-custom-tasks/) [TODO], the [setup command](https://tomo-deploy.com/commands/setup/), and the [deploy command](https://tomo-deploy.com/commands/deploy/). There is also a tutorial that walks through [deploying a new Rails app from scratch](https://tomo-deploy.com/tutorials/deploying-rails-from-scratch/) [TODO]. If you have questions, check out the [FAQ](#faq) and [support](#support) notes below. Enjoy using tomo!
154
+
155
+ ## Reference documentation
156
+
157
+ - [Configuration](https://tomo-deploy.com/configuration/)
158
+ - Commands
159
+ - [init](https://tomo-deploy.com/commands/init/)
160
+ - [setup](https://tomo-deploy.com/commands/setup/)
161
+ - [deploy](https://tomo-deploy.com/commands/deploy/)
162
+ - [run](https://tomo-deploy.com/commands/run/)
163
+ - [tasks](https://tomo-deploy.com/commands/tasks/)
164
+ - Plugins
165
+ - [core](https://tomo-deploy.com/plugins/core/)
166
+ - [bundler](https://tomo-deploy.com/plugins/bundler/)
167
+ - [env](https://tomo-deploy.com/plugins/env/)
168
+ - [git](https://tomo-deploy.com/plugins/git/)
169
+ - [nvm](https://tomo-deploy.com/plugins/nvm/)
170
+ - [puma](https://tomo-deploy.com/plugins/puma/)
171
+ - [rails](https://tomo-deploy.com/plugins/rails/)
172
+ - [rbenv](https://tomo-deploy.com/plugins/rbenv/)
173
+ - Tutorials
174
+ - [Deploying Rails From Scratch](https://tomo-deploy.com/tutorials/deploying-rails-from-scratch/) [TODO]
175
+ - [Writing Custom Tasks](https://tomo-deploy.com/tutorials/writing-custom-tasks/) [TODO]
176
+ - [Publishing a Plugin](https://tomo-deploy.com/tutorials/publishing-a-plugin/) [TODO]
177
+ - API
178
+ - [Host](https://tomo-deploy.com/api/Host/)
179
+ - [Logger](https://tomo-deploy.com/api/Logger/)
180
+ - [Paths](https://tomo-deploy.com/api/Paths/)
181
+ - [PluginDSL](https://tomo-deploy.com/api/Host/)
182
+ - [Remote](https://tomo-deploy.com/api/Remote/)
183
+ - [Result](https://tomo-deploy.com/api/Result/)
184
+ - [TaskLibrary](https://tomo-deploy.com/api/TaskLibrary/)
185
+ - [Testing::MockPluginTester](https://tomo-deploy.com/api/testing/MockPluginTester/)
186
+ - [Testing::DockerPluginTester](https://tomo-deploy.com/api/testing/DockerPluginTester/)
187
+
188
+ ## FAQ
189
+
190
+ #### What does the `unsupported option "accept-new"` error mean?
191
+
192
+ By default, tomo uses the ["accept-new"](https://www.openssh.com/txt/release-7.6) value for the StrictHostKeyChecking option, which is supported by OpenSSH 7.6 and newer. If you are using an older version, this will cause an error. As a workaround, you can override tomo's default behavior like this:
193
+
194
+ ```ruby
195
+ # Replace "accept-new" with something compatible with older versions of SSH
196
+ set ssh_strict_host_key_checking: true # or false
197
+ ```
198
+
199
+ ## Support
200
+
201
+ Thanks for your interest in Tomo! I use Tomo myself to deploy my own Rails projects and intend to keep this repository working and up to date for the foreseeable future. However Tomo is only a hobby, and as the sole maintainer, my ability to provide support and review pull request is limited and a bit sporadic. My priorities right now are:
202
+
203
+ 1. Improve test coverage
204
+ 2. Keep the project free of any serious bugs
205
+ 3. Stay up to date with the latest versions of Ruby and gem dependencies
206
+
207
+ If you'd like to help by submitting a pull request, that would be much appreciated! Check out the contribution guide to get started.
208
+
209
+ Otherwise if you want to report a bug, or have ideas, feedback or questions about Tomo, [let me know via GitHub issues](https://github.com/mattbrictson/tomo/issues/new) and I will do my best to provide a helpful answer. Happy hacking! —Matt
210
+
211
+ ## License
212
+
213
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
214
+
215
+ ## Code of conduct
216
+
217
+ Everyone interacting in the Tomo project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/mattbrictson/tomo/blob/master/CODE_OF_CONDUCT.md).
218
+
219
+ ## Contribution guide
220
+
221
+ [TODO]
data/exe/tomo ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "tomo"
4
+ Tomo::CLI.new.call(ARGV)
@@ -0,0 +1,36 @@
1
+ require "forwardable"
2
+
3
+ module Tomo
4
+ class CLI
5
+ class Command
6
+ class << self
7
+ extend Forwardable
8
+ def_delegators :parser, :arg, :option, :after_parse
9
+
10
+ def parser
11
+ @parser ||= Parser.new
12
+ end
13
+
14
+ def parse(argv)
15
+ command = new
16
+ parser.context = command
17
+ parser.banner = command.method(:banner)
18
+ *args, options = parser.parse(argv)
19
+ command.call(*args, options)
20
+ end
21
+ end
22
+
23
+ include Colors
24
+
25
+ private
26
+
27
+ def dry_run?
28
+ Tomo.dry_run?
29
+ end
30
+
31
+ def logger
32
+ Tomo.logger
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,48 @@
1
+ module Tomo
2
+ class CLI
3
+ module CommonOptions
4
+ # rubocop:disable Metrics/MethodLength
5
+ def self.included(mod)
6
+ mod.class_eval do
7
+ option :color,
8
+ "--[no-]color",
9
+ "Enable/disable color output" do |color|
10
+ Colors.enabled = color
11
+ end
12
+ option :debug,
13
+ "--[no-]debug",
14
+ "Enable/disable verbose debug logging" do |debug|
15
+ Tomo.debug = debug
16
+ end
17
+ option :trace,
18
+ "--[no-]trace",
19
+ "Display full backtrace on error" do |trace|
20
+ CLI.show_backtrace = trace
21
+ end
22
+ option :help, "-h, --help", "Print this documentation" do |_help|
23
+ puts instance_variable_get(:@parser)
24
+ exit
25
+ end
26
+
27
+ after_parse :dump_runtime_info
28
+ end
29
+ end
30
+ # rubocop:enable Metrics/MethodLength
31
+
32
+ private
33
+
34
+ def dump_runtime_info(*)
35
+ Tomo.logger.debug("tomo #{Tomo::VERSION}")
36
+ Tomo.logger.debug(RUBY_DESCRIPTION)
37
+ Tomo.logger.debug("rubygems #{Gem::VERSION}")
38
+ Tomo.logger.debug("bundler #{Bundler::VERSION}") if Tomo.bundled?
39
+
40
+ begin
41
+ require "concurrent"
42
+ Tomo.logger.debug("concurrent-ruby #{Concurrent::VERSION}")
43
+ rescue LoadError # rubocop:disable Lint/HandleExceptions
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,70 @@
1
+ module Tomo
2
+ class CLI
3
+ class Completions
4
+ def self.activate
5
+ @active = true
6
+ end
7
+
8
+ def self.active?
9
+ !!@active
10
+ end
11
+
12
+ def initialize(literal: false, stdout: $stdout, exit_proc: nil)
13
+ @literal = literal
14
+ @stdout = stdout
15
+ @exit_proc = exit_proc || Kernel.method(:exit)
16
+ end
17
+
18
+ def print_completions_and_exit(rules, *args, state:)
19
+ completions = completions_for(rules, *args, state)
20
+ words = completions.map { |c| bash_word_for(c, args.last) }
21
+ stdout.puts(words.join("\n")) unless words.empty?
22
+ exit_proc.call
23
+ end
24
+
25
+ private
26
+
27
+ attr_reader :literal, :stdout, :exit_proc
28
+
29
+ def completions_for(rules, *prefix_args, word, state)
30
+ all_candidates(rules, prefix_args, state).select do |cand|
31
+ next if !literal && redundant_option_completion?(cand, word)
32
+
33
+ cand.start_with?(word) && cand != word
34
+ end
35
+ end
36
+
37
+ def all_candidates(rules, prefix_args, state)
38
+ rules.flat_map do |rule|
39
+ rule.candidates(*prefix_args, literal: literal, state: state)
40
+ end
41
+ end
42
+
43
+ def redundant_option_completion?(cand, word)
44
+ # Don't complete switches until the user has typed at least "--"
45
+ return true if cand.start_with?("-") && !word.start_with?("--")
46
+
47
+ # Don't complete the =value part of long switch unless the user has
48
+ # already typed at least up to the = sign.
49
+ return true if cand.match?(/\A--.*=/) && !word.match?(/\A--.*=/)
50
+ end
51
+
52
+ # bash tokenizes the user's input prior to completion, and expects the
53
+ # completions we return to be only the last token of the string. So if the
54
+ # user typed "rails:c[TAB]", bash expects the completion to be the part
55
+ # after the ":" character. In other words we should return "console", not
56
+ # "rails:console". The special tokens we need to consider are ":" and "=".
57
+ #
58
+ # For convenience we also distinguish a partial completion vs a full
59
+ # completion. If it is a full completion we append a " " to the end of
60
+ # the word so that the user can naturally begin typing the next option or
61
+ # argument.
62
+ def bash_word_for(completion, user_input)
63
+ completion = "#{completion} " unless completion.end_with?("=")
64
+ return completion unless user_input.match?(/[:=]/)
65
+
66
+ completion.sub(/.*[:=]/, "")
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,59 @@
1
+ module Tomo
2
+ class CLI
3
+ module DeployOptions
4
+ # rubocop:disable Metrics/MethodLength
5
+ def self.included(mod)
6
+ mod.class_eval do
7
+ option :environment,
8
+ "-e, --environment ENVIRONMENT",
9
+ "Specify environment to use (e.g. production)",
10
+ values: :environment_names
11
+
12
+ option :settings,
13
+ "-s, --setting NAME=VALUE",
14
+ "Override setting NAME with the given VALUE",
15
+ values: :setting_completions
16
+
17
+ option :dry_run,
18
+ "--[no-]dry-run",
19
+ "Simulate running tasks instead of using real SSH" do |dry|
20
+ Tomo.dry_run = dry
21
+ end
22
+
23
+ after_parse :prompt_for_environment
24
+ end
25
+ end
26
+ # rubocop:enable Metrics/MethodLength
27
+
28
+ private
29
+
30
+ def environment_names(*_args, options)
31
+ load_configuration(options).environments.keys
32
+ end
33
+
34
+ def setting_completions(*_args, options)
35
+ runtime = configure_runtime(options, strict: false)
36
+ settings = runtime.execution_plan_for([]).settings
37
+
38
+ settings = settings.select do |_key, value|
39
+ value.nil? || value.is_a?(String) || value.is_a?(Numeric)
40
+ end.to_h
41
+
42
+ settings.keys.map { |sett| "#{sett}=" }
43
+ end
44
+
45
+ def prompt_for_environment(*args, options)
46
+ return unless options[:environment].nil?
47
+
48
+ envs = environment_names(*args, options)
49
+ return if envs.empty?
50
+ return unless Console.interactive?
51
+
52
+ options[:environment] = Console.menu(
53
+ "Choose an environment:",
54
+ choices: envs
55
+ )
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,16 @@
1
+ module Tomo
2
+ class CLI
3
+ class Error < ::Tomo::Error
4
+ attr_accessor :command_name
5
+
6
+ def to_console
7
+ tomo_command = ["tomo", command_name].compact.join(" ")
8
+ <<~ERROR
9
+ #{message}
10
+
11
+ Run #{blue("#{tomo_command} -h")} for help.
12
+ ERROR
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,9 @@
1
+ module Tomo
2
+ class CLI
3
+ class InterruptedError < Error
4
+ def to_console
5
+ "Interrupted"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,38 @@
1
+ module Tomo
2
+ class CLI
3
+ class Options
4
+ def initialize
5
+ @options = {}
6
+ end
7
+
8
+ def any?
9
+ options.any?
10
+ end
11
+
12
+ def key?(key)
13
+ !all(key).empty?
14
+ end
15
+
16
+ def fetch(key, default)
17
+ values = all(key)
18
+ values.empty? ? default : values.first
19
+ end
20
+
21
+ def [](key)
22
+ fetch(key, nil)
23
+ end
24
+
25
+ def []=(key, value)
26
+ all(key).clear.push(value)
27
+ end
28
+
29
+ def all(key)
30
+ options[key] ||= []
31
+ end
32
+
33
+ private
34
+
35
+ attr_reader :options
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,92 @@
1
+ require "forwardable"
2
+
3
+ module Tomo
4
+ class CLI
5
+ class Parser
6
+ extend Forwardable
7
+ include Colors
8
+
9
+ def_delegators :usage, :banner=, :to_s
10
+
11
+ attr_accessor :context
12
+
13
+ def initialize
14
+ @rules = Rules.new
15
+ @usage = Usage.new
16
+ @after_parse_methods = []
17
+ end
18
+
19
+ def arg(spec, values: [])
20
+ rules.add_arg(spec, proc_for(values))
21
+ end
22
+
23
+ def option(key, spec, desc=nil, values: [], &block)
24
+ rules.add_option(key, spec, proc_for(values), &block)
25
+ usage.add_option(spec, desc)
26
+ end
27
+
28
+ def after_parse(context_method_name)
29
+ after_parse_methods << context_method_name
30
+ end
31
+
32
+ def parse(argv)
33
+ state = State.new
34
+
35
+ options_argv, literal_argv = split(argv, "--")
36
+ evaluate(options_argv, state, literal: false)
37
+ evaluate(literal_argv, state, literal: true)
38
+ check_required_rules(state)
39
+ invoke_after_parse_methods(state)
40
+
41
+ [*state.args, state.options]
42
+ end
43
+
44
+ private
45
+
46
+ attr_reader :rules, :usage, :after_parse_methods
47
+
48
+ def evaluate(argv, state, literal:)
49
+ RulesEvaluator.evaluate(
50
+ rules: rules.to_a,
51
+ argv: argv,
52
+ state: state,
53
+ literal: literal
54
+ )
55
+ end
56
+
57
+ def check_required_rules(state)
58
+ (rules.to_a - state.processed_rules).each do |rule|
59
+ next unless rule.required?
60
+
61
+ type = rule.is_a?(Rules::Argument) ? "" : " option"
62
+ raise CLI::Error, "Please specify the #{yellow(rule.to_s)}#{type}."
63
+ end
64
+ end
65
+
66
+ def invoke_after_parse_methods(state)
67
+ after_parse_methods.each do |method|
68
+ context.send(method, *state.args, state.options)
69
+ end
70
+ end
71
+
72
+ def proc_for(values)
73
+ return values if values.respond_to?(:call)
74
+ return proc { values } unless values.is_a?(Symbol)
75
+
76
+ method = values
77
+ proc { |*args| context.send(method, *args) }
78
+ end
79
+
80
+ def split(argv, delimiter)
81
+ index = argv.index(delimiter)
82
+ return [argv, []] if index.nil?
83
+ return [argv, []] if index == argv.length - 1 && Completions.active?
84
+
85
+ before = argv[0...index]
86
+ after = argv[(index + 1)..-1]
87
+
88
+ [before, after]
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,47 @@
1
+ module Tomo
2
+ class CLI
3
+ module ProjectOptions
4
+ def self.included(mod)
5
+ mod.class_eval do
6
+ option :project,
7
+ "-c, --config PATH",
8
+ "Location of project config (default: #{DEFAULT_CONFIG_PATH})"
9
+ end
10
+ end
11
+
12
+ private
13
+
14
+ def configure_runtime(options, strict: true)
15
+ config = load_configuration(options)
16
+ env = options[:environment]
17
+ env = config.environments.keys.first if env.nil? && !strict
18
+ config = config.for_environment(env)
19
+ config.settings.merge!(settings_from_env)
20
+ config.settings.merge!(settings_from_options(options))
21
+ config.build_runtime
22
+ end
23
+
24
+ def load_configuration(options)
25
+ path = options[:project] || DEFAULT_CONFIG_PATH
26
+ @config_cache ||= {}
27
+ @config_cache[path] ||= Configuration.from_config_rb(path)
28
+ end
29
+
30
+ def settings_from_options(options)
31
+ options.all(:settings).each_with_object({}) do |arg, settings|
32
+ name, value = arg.split("=", 2)
33
+ settings[name.to_sym] = value
34
+ end
35
+ end
36
+
37
+ def settings_from_env
38
+ ENV.each_with_object({}) do |(key, value), result|
39
+ setting_name = key[/^TOMO_(\w+)$/i, 1]&.downcase
40
+ next if setting_name.nil?
41
+
42
+ result[setting_name.to_sym] = value
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end