foreman 0.84.0 → 0.87.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +1 -1
- data/data/export/daemon/process.conf.erb +1 -1
- data/data/export/launchd/launchd.plist.erb +1 -1
- data/data/export/systemd/master.target.erb +1 -1
- data/data/export/systemd/process.service.erb +7 -4
- data/data/export/upstart/process.conf.erb +1 -1
- data/lib/foreman/cli.rb +3 -3
- data/lib/foreman/engine.rb +2 -1
- data/lib/foreman/export/base.rb +5 -1
- data/lib/foreman/export/systemd.rb +7 -13
- data/lib/foreman/procfile.rb +1 -1
- data/lib/foreman/vendor/thor/lib/thor.rb +492 -0
- data/lib/foreman/vendor/thor/lib/thor/actions.rb +318 -0
- data/lib/foreman/vendor/thor/lib/thor/actions/create_file.rb +103 -0
- data/lib/foreman/vendor/thor/lib/thor/actions/create_link.rb +59 -0
- data/lib/foreman/vendor/thor/lib/thor/actions/directory.rb +118 -0
- data/lib/foreman/vendor/thor/lib/thor/actions/empty_directory.rb +135 -0
- data/lib/foreman/vendor/thor/lib/thor/actions/file_manipulation.rb +327 -0
- data/lib/foreman/vendor/thor/lib/thor/actions/inject_into_file.rb +103 -0
- data/lib/foreman/vendor/thor/lib/thor/base.rb +656 -0
- data/lib/foreman/vendor/thor/lib/thor/command.rb +133 -0
- data/lib/foreman/vendor/thor/lib/thor/core_ext/hash_with_indifferent_access.rb +85 -0
- data/lib/foreman/vendor/thor/lib/thor/core_ext/io_binary_read.rb +12 -0
- data/lib/foreman/vendor/thor/lib/thor/core_ext/ordered_hash.rb +129 -0
- data/lib/foreman/vendor/thor/lib/thor/error.rb +32 -0
- data/lib/foreman/vendor/thor/lib/thor/group.rb +281 -0
- data/lib/foreman/vendor/thor/lib/thor/invocation.rb +177 -0
- data/lib/foreman/vendor/thor/lib/thor/line_editor.rb +17 -0
- data/lib/foreman/vendor/thor/lib/thor/line_editor/basic.rb +35 -0
- data/lib/foreman/vendor/thor/lib/thor/line_editor/readline.rb +88 -0
- data/lib/foreman/vendor/thor/lib/thor/parser.rb +4 -0
- data/lib/foreman/vendor/thor/lib/thor/parser/argument.rb +70 -0
- data/lib/foreman/vendor/thor/lib/thor/parser/arguments.rb +175 -0
- data/lib/foreman/vendor/thor/lib/thor/parser/option.rb +146 -0
- data/lib/foreman/vendor/thor/lib/thor/parser/options.rb +220 -0
- data/lib/foreman/vendor/thor/lib/thor/rake_compat.rb +71 -0
- data/lib/foreman/vendor/thor/lib/thor/runner.rb +322 -0
- data/lib/foreman/vendor/thor/lib/thor/shell.rb +81 -0
- data/lib/foreman/vendor/thor/lib/thor/shell/basic.rb +436 -0
- data/lib/foreman/vendor/thor/lib/thor/shell/color.rb +149 -0
- data/lib/foreman/vendor/thor/lib/thor/shell/html.rb +126 -0
- data/lib/foreman/vendor/thor/lib/thor/util.rb +268 -0
- data/lib/foreman/vendor/thor/lib/thor/version.rb +3 -0
- data/lib/foreman/version.rb +1 -1
- data/man/foreman.1 +1 -1
- data/spec/foreman/export/systemd_spec.rb +88 -46
- data/spec/foreman/procfile_spec.rb +7 -0
- data/spec/resources/export/systemd/{app-alpha@.service → app-alpha.1.service} +6 -3
- data/spec/resources/export/systemd/{app-bravo@.service → app-alpha.2.service} +6 -3
- data/spec/resources/export/systemd/app-bravo.1.service +18 -0
- data/spec/resources/export/systemd/app.target +1 -1
- data/spec/spec_helper.rb +11 -3
- metadata +42 -26
- data/data/export/systemd/process_master.target.erb +0 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d793d4fa84a1ecb6daadb477756195e44f9ccd90de90be6f817505015028c06e
|
4
|
+
data.tar.gz: cab6c4bce9ffa9bceef2eeb15023dad3d9b404bba2a0c2d67a1fb785dc09bb12
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 62699c71def241e50ae7b41ad409a723a90b17027748daf989bf251497b1ede3bfd13c8ed0edb29f93395b88a88dfcbc119424bf310fe6cb432fde61f7686856
|
7
|
+
data.tar.gz: 8549d39b952cb2702d15498fee39b8fe8c11e60acb39e33aa79d44b899bdbc204291e6320647fcdf0d3d3f3d255950066e318a1c876d80e05c00d90f3af49cbd
|
data/README.md
CHANGED
@@ -10,7 +10,7 @@ Manage Procfile-based applications
|
|
10
10
|
|
11
11
|
$ gem install foreman
|
12
12
|
|
13
|
-
Ruby users should take care *not* to install foreman in their project's `Gemfile`.
|
13
|
+
Ruby users should take care *not* to install foreman in their project's `Gemfile`. See this [wiki article](https://github.com/ddollar/foreman/wiki/Don't-Bundle-Foreman) for more details.
|
14
14
|
|
15
15
|
## Getting Started
|
16
16
|
|
@@ -3,6 +3,6 @@ stop on stopping <%= app %>-<%= name.gsub('_', '-') %>
|
|
3
3
|
respawn
|
4
4
|
|
5
5
|
env PORT=<%= port %><% engine.env.each_pair do |var, env| %>
|
6
|
-
env <%= var
|
6
|
+
env <%= var %>=<%= env %><% end %>
|
7
7
|
|
8
8
|
exec start-stop-daemon --start --chuid <%= user %> --chdir <%= engine.root %> --make-pidfile --pidfile <%= run %>/<%= app %>-<%= name %>-<%= num %>.pid --exec <%= executable %><%= arguments %> >> <%= log %>/<%= app %>-<%= name %>-<%= num %>.log 2>&1
|
@@ -1,15 +1,18 @@
|
|
1
1
|
[Unit]
|
2
|
-
PartOf=<%= app
|
2
|
+
PartOf=<%= app %>.target
|
3
|
+
StopWhenUnneeded=yes
|
3
4
|
|
4
5
|
[Service]
|
5
6
|
User=<%= user %>
|
6
7
|
WorkingDirectory=<%= engine.root %>
|
7
|
-
Environment=PORT
|
8
|
+
Environment=PORT=<%= port %>
|
9
|
+
Environment=PS=<%= process_name %>
|
8
10
|
<% engine.env.each_pair do |var,env| -%>
|
9
|
-
Environment="<%= var
|
11
|
+
Environment="<%= var %>=<%= env %>"
|
10
12
|
<% end -%>
|
11
|
-
ExecStart=/bin/bash -lc 'exec <%= process.command %>'
|
13
|
+
ExecStart=/bin/bash -lc 'exec -a "<%= app %>-<%= process_name %>" <%= process.command %>'
|
12
14
|
Restart=always
|
15
|
+
RestartSec=14s
|
13
16
|
StandardInput=null
|
14
17
|
StandardOutput=syslog
|
15
18
|
StandardError=syslog
|
data/lib/foreman/cli.rb
CHANGED
@@ -6,9 +6,9 @@ require "foreman/export"
|
|
6
6
|
require "foreman/version"
|
7
7
|
require "shellwords"
|
8
8
|
require "yaml"
|
9
|
-
require "thor"
|
9
|
+
require "foreman/vendor/thor/lib/thor"
|
10
10
|
|
11
|
-
class Foreman::CLI < Thor
|
11
|
+
class Foreman::CLI < Foreman::Thor
|
12
12
|
|
13
13
|
include Foreman::Helpers
|
14
14
|
|
@@ -157,6 +157,6 @@ private ######################################################################
|
|
157
157
|
original_options = super
|
158
158
|
return original_options unless File.file?(".foreman")
|
159
159
|
defaults = ::YAML::load_file(".foreman") || {}
|
160
|
-
Thor::CoreExt::HashWithIndifferentAccess.new(defaults.merge(original_options))
|
160
|
+
Foreman::Thor::CoreExt::HashWithIndifferentAccess.new(defaults.merge(original_options))
|
161
161
|
end
|
162
162
|
end
|
data/lib/foreman/engine.rb
CHANGED
data/lib/foreman/export/base.rb
CHANGED
@@ -135,7 +135,11 @@ private ######################################################################
|
|
135
135
|
end
|
136
136
|
|
137
137
|
def write_template(name, target, binding)
|
138
|
-
compiled = ERB.
|
138
|
+
compiled = if ERB.instance_method(:initialize).parameters.assoc(:key) # Ruby 2.6+
|
139
|
+
ERB.new(export_template(name), trim_mode: '-').result(binding)
|
140
|
+
else
|
141
|
+
ERB.new(export_template(name), nil, '-').result(binding)
|
142
|
+
end
|
139
143
|
write_file target, compiled
|
140
144
|
end
|
141
145
|
|
@@ -17,22 +17,16 @@ class Foreman::Export::Systemd < Foreman::Export::Base
|
|
17
17
|
clean_dir file
|
18
18
|
end
|
19
19
|
|
20
|
-
|
20
|
+
service_names = []
|
21
21
|
|
22
22
|
engine.each_process do |name, process|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
.collect { |port| "#{app}-#{name}@#{port}.service" }
|
30
|
-
.each do |process_name|
|
31
|
-
create_symlink("#{app}-#{name}.target.wants/#{process_name}", "../#{service_fn}") rescue Errno::EEXIST # This is needed because rr-mocks do not call the origial cleanup
|
23
|
+
1.upto(engine.formation[name]) do |num|
|
24
|
+
port = engine.port_for(process, num)
|
25
|
+
process_name = "#{name}.#{num}"
|
26
|
+
service_filename = "#{app}-#{process_name}.service"
|
27
|
+
write_template "systemd/process.service.erb", service_filename, binding
|
28
|
+
service_names << service_filename
|
32
29
|
end
|
33
|
-
|
34
|
-
write_template "systemd/process_master.target.erb", "#{app}-#{name}.target", binding
|
35
|
-
process_master_names << "#{app}-#{name}.target"
|
36
30
|
end
|
37
31
|
|
38
32
|
write_template "systemd/master.target.erb", "#{app}.target", binding
|
data/lib/foreman/procfile.rb
CHANGED
@@ -0,0 +1,492 @@
|
|
1
|
+
require "set"
|
2
|
+
require "foreman/vendor/thor/lib/thor/base"
|
3
|
+
|
4
|
+
class Foreman::Thor
|
5
|
+
class << self
|
6
|
+
# Allows for custom "Command" package naming.
|
7
|
+
#
|
8
|
+
# === Parameters
|
9
|
+
# name<String>
|
10
|
+
# options<Hash>
|
11
|
+
#
|
12
|
+
def package_name(name, _ = {})
|
13
|
+
@package_name = name.nil? || name == "" ? nil : name
|
14
|
+
end
|
15
|
+
|
16
|
+
# Sets the default command when thor is executed without an explicit command to be called.
|
17
|
+
#
|
18
|
+
# ==== Parameters
|
19
|
+
# meth<Symbol>:: name of the default command
|
20
|
+
#
|
21
|
+
def default_command(meth = nil)
|
22
|
+
if meth
|
23
|
+
@default_command = meth == :none ? "help" : meth.to_s
|
24
|
+
else
|
25
|
+
@default_command ||= from_superclass(:default_command, "help")
|
26
|
+
end
|
27
|
+
end
|
28
|
+
alias_method :default_task, :default_command
|
29
|
+
|
30
|
+
# Registers another Foreman::Thor subclass as a command.
|
31
|
+
#
|
32
|
+
# ==== Parameters
|
33
|
+
# klass<Class>:: Foreman::Thor subclass to register
|
34
|
+
# command<String>:: Subcommand name to use
|
35
|
+
# usage<String>:: Short usage for the subcommand
|
36
|
+
# description<String>:: Description for the subcommand
|
37
|
+
def register(klass, subcommand_name, usage, description, options = {})
|
38
|
+
if klass <= Foreman::Thor::Group
|
39
|
+
desc usage, description, options
|
40
|
+
define_method(subcommand_name) { |*args| invoke(klass, args) }
|
41
|
+
else
|
42
|
+
desc usage, description, options
|
43
|
+
subcommand subcommand_name, klass
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# Defines the usage and the description of the next command.
|
48
|
+
#
|
49
|
+
# ==== Parameters
|
50
|
+
# usage<String>
|
51
|
+
# description<String>
|
52
|
+
# options<String>
|
53
|
+
#
|
54
|
+
def desc(usage, description, options = {})
|
55
|
+
if options[:for]
|
56
|
+
command = find_and_refresh_command(options[:for])
|
57
|
+
command.usage = usage if usage
|
58
|
+
command.description = description if description
|
59
|
+
else
|
60
|
+
@usage = usage
|
61
|
+
@desc = description
|
62
|
+
@hide = options[:hide] || false
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
# Defines the long description of the next command.
|
67
|
+
#
|
68
|
+
# ==== Parameters
|
69
|
+
# long description<String>
|
70
|
+
#
|
71
|
+
def long_desc(long_description, options = {})
|
72
|
+
if options[:for]
|
73
|
+
command = find_and_refresh_command(options[:for])
|
74
|
+
command.long_description = long_description if long_description
|
75
|
+
else
|
76
|
+
@long_desc = long_description
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
# Maps an input to a command. If you define:
|
81
|
+
#
|
82
|
+
# map "-T" => "list"
|
83
|
+
#
|
84
|
+
# Running:
|
85
|
+
#
|
86
|
+
# thor -T
|
87
|
+
#
|
88
|
+
# Will invoke the list command.
|
89
|
+
#
|
90
|
+
# ==== Parameters
|
91
|
+
# Hash[String|Array => Symbol]:: Maps the string or the strings in the array to the given command.
|
92
|
+
#
|
93
|
+
def map(mappings = nil)
|
94
|
+
@map ||= from_superclass(:map, {})
|
95
|
+
|
96
|
+
if mappings
|
97
|
+
mappings.each do |key, value|
|
98
|
+
if key.respond_to?(:each)
|
99
|
+
key.each { |subkey| @map[subkey] = value }
|
100
|
+
else
|
101
|
+
@map[key] = value
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
@map
|
107
|
+
end
|
108
|
+
|
109
|
+
# Declares the options for the next command to be declared.
|
110
|
+
#
|
111
|
+
# ==== Parameters
|
112
|
+
# Hash[Symbol => Object]:: The hash key is the name of the option and the value
|
113
|
+
# is the type of the option. Can be :string, :array, :hash, :boolean, :numeric
|
114
|
+
# or :required (string). If you give a value, the type of the value is used.
|
115
|
+
#
|
116
|
+
def method_options(options = nil)
|
117
|
+
@method_options ||= {}
|
118
|
+
build_options(options, @method_options) if options
|
119
|
+
@method_options
|
120
|
+
end
|
121
|
+
|
122
|
+
alias_method :options, :method_options
|
123
|
+
|
124
|
+
# Adds an option to the set of method options. If :for is given as option,
|
125
|
+
# it allows you to change the options from a previous defined command.
|
126
|
+
#
|
127
|
+
# def previous_command
|
128
|
+
# # magic
|
129
|
+
# end
|
130
|
+
#
|
131
|
+
# method_option :foo => :bar, :for => :previous_command
|
132
|
+
#
|
133
|
+
# def next_command
|
134
|
+
# # magic
|
135
|
+
# end
|
136
|
+
#
|
137
|
+
# ==== Parameters
|
138
|
+
# name<Symbol>:: The name of the argument.
|
139
|
+
# options<Hash>:: Described below.
|
140
|
+
#
|
141
|
+
# ==== Options
|
142
|
+
# :desc - Description for the argument.
|
143
|
+
# :required - If the argument is required or not.
|
144
|
+
# :default - Default value for this argument. It cannot be required and have default values.
|
145
|
+
# :aliases - Aliases for this option.
|
146
|
+
# :type - The type of the argument, can be :string, :hash, :array, :numeric or :boolean.
|
147
|
+
# :banner - String to show on usage notes.
|
148
|
+
# :hide - If you want to hide this option from the help.
|
149
|
+
#
|
150
|
+
def method_option(name, options = {})
|
151
|
+
scope = if options[:for]
|
152
|
+
find_and_refresh_command(options[:for]).options
|
153
|
+
else
|
154
|
+
method_options
|
155
|
+
end
|
156
|
+
|
157
|
+
build_option(name, options, scope)
|
158
|
+
end
|
159
|
+
alias_method :option, :method_option
|
160
|
+
|
161
|
+
def disable_class_options
|
162
|
+
@disable_class_options = true
|
163
|
+
end
|
164
|
+
|
165
|
+
# Prints help information for the given command.
|
166
|
+
#
|
167
|
+
# ==== Parameters
|
168
|
+
# shell<Foreman::Thor::Shell>
|
169
|
+
# command_name<String>
|
170
|
+
#
|
171
|
+
def command_help(shell, command_name)
|
172
|
+
meth = normalize_command_name(command_name)
|
173
|
+
command = all_commands[meth]
|
174
|
+
handle_no_command_error(meth) unless command
|
175
|
+
|
176
|
+
shell.say "Usage:"
|
177
|
+
shell.say " #{banner(command)}"
|
178
|
+
shell.say
|
179
|
+
class_options_help(shell, nil => command.options.values)
|
180
|
+
if command.long_description
|
181
|
+
shell.say "Description:"
|
182
|
+
shell.print_wrapped(command.long_description, :indent => 2)
|
183
|
+
else
|
184
|
+
shell.say command.description
|
185
|
+
end
|
186
|
+
end
|
187
|
+
alias_method :task_help, :command_help
|
188
|
+
|
189
|
+
# Prints help information for this class.
|
190
|
+
#
|
191
|
+
# ==== Parameters
|
192
|
+
# shell<Foreman::Thor::Shell>
|
193
|
+
#
|
194
|
+
def help(shell, subcommand = false)
|
195
|
+
list = printable_commands(true, subcommand)
|
196
|
+
Foreman::Thor::Util.thor_classes_in(self).each do |klass|
|
197
|
+
list += klass.printable_commands(false)
|
198
|
+
end
|
199
|
+
list.sort! { |a, b| a[0] <=> b[0] }
|
200
|
+
|
201
|
+
if defined?(@package_name) && @package_name
|
202
|
+
shell.say "#{@package_name} commands:"
|
203
|
+
else
|
204
|
+
shell.say "Commands:"
|
205
|
+
end
|
206
|
+
|
207
|
+
shell.print_table(list, :indent => 2, :truncate => true)
|
208
|
+
shell.say
|
209
|
+
class_options_help(shell)
|
210
|
+
end
|
211
|
+
|
212
|
+
# Returns commands ready to be printed.
|
213
|
+
def printable_commands(all = true, subcommand = false)
|
214
|
+
(all ? all_commands : commands).map do |_, command|
|
215
|
+
next if command.hidden?
|
216
|
+
item = []
|
217
|
+
item << banner(command, false, subcommand)
|
218
|
+
item << (command.description ? "# #{command.description.gsub(/\s+/m, ' ')}" : "")
|
219
|
+
item
|
220
|
+
end.compact
|
221
|
+
end
|
222
|
+
alias_method :printable_tasks, :printable_commands
|
223
|
+
|
224
|
+
def subcommands
|
225
|
+
@subcommands ||= from_superclass(:subcommands, [])
|
226
|
+
end
|
227
|
+
alias_method :subtasks, :subcommands
|
228
|
+
|
229
|
+
def subcommand_classes
|
230
|
+
@subcommand_classes ||= {}
|
231
|
+
end
|
232
|
+
|
233
|
+
def subcommand(subcommand, subcommand_class)
|
234
|
+
subcommands << subcommand.to_s
|
235
|
+
subcommand_class.subcommand_help subcommand
|
236
|
+
subcommand_classes[subcommand.to_s] = subcommand_class
|
237
|
+
|
238
|
+
define_method(subcommand) do |*args|
|
239
|
+
args, opts = Foreman::Thor::Arguments.split(args)
|
240
|
+
invoke_args = [args, opts, {:invoked_via_subcommand => true, :class_options => options}]
|
241
|
+
invoke_args.unshift "help" if opts.delete("--help") || opts.delete("-h")
|
242
|
+
invoke subcommand_class, *invoke_args
|
243
|
+
end
|
244
|
+
end
|
245
|
+
alias_method :subtask, :subcommand
|
246
|
+
|
247
|
+
# Extend check unknown options to accept a hash of conditions.
|
248
|
+
#
|
249
|
+
# === Parameters
|
250
|
+
# options<Hash>: A hash containing :only and/or :except keys
|
251
|
+
def check_unknown_options!(options = {})
|
252
|
+
@check_unknown_options ||= {}
|
253
|
+
options.each do |key, value|
|
254
|
+
if value
|
255
|
+
@check_unknown_options[key] = Array(value)
|
256
|
+
else
|
257
|
+
@check_unknown_options.delete(key)
|
258
|
+
end
|
259
|
+
end
|
260
|
+
@check_unknown_options
|
261
|
+
end
|
262
|
+
|
263
|
+
# Overwrite check_unknown_options? to take subcommands and options into account.
|
264
|
+
def check_unknown_options?(config) #:nodoc:
|
265
|
+
options = check_unknown_options
|
266
|
+
return false unless options
|
267
|
+
|
268
|
+
command = config[:current_command]
|
269
|
+
return true unless command
|
270
|
+
|
271
|
+
name = command.name
|
272
|
+
|
273
|
+
if subcommands.include?(name)
|
274
|
+
false
|
275
|
+
elsif options[:except]
|
276
|
+
!options[:except].include?(name.to_sym)
|
277
|
+
elsif options[:only]
|
278
|
+
options[:only].include?(name.to_sym)
|
279
|
+
else
|
280
|
+
true
|
281
|
+
end
|
282
|
+
end
|
283
|
+
|
284
|
+
# Stop parsing of options as soon as an unknown option or a regular
|
285
|
+
# argument is encountered. All remaining arguments are passed to the command.
|
286
|
+
# This is useful if you have a command that can receive arbitrary additional
|
287
|
+
# options, and where those additional options should not be handled by
|
288
|
+
# Foreman::Thor.
|
289
|
+
#
|
290
|
+
# ==== Example
|
291
|
+
#
|
292
|
+
# To better understand how this is useful, let's consider a command that calls
|
293
|
+
# an external command. A user may want to pass arbitrary options and
|
294
|
+
# arguments to that command. The command itself also accepts some options,
|
295
|
+
# which should be handled by Foreman::Thor.
|
296
|
+
#
|
297
|
+
# class_option "verbose", :type => :boolean
|
298
|
+
# stop_on_unknown_option! :exec
|
299
|
+
# check_unknown_options! :except => :exec
|
300
|
+
#
|
301
|
+
# desc "exec", "Run a shell command"
|
302
|
+
# def exec(*args)
|
303
|
+
# puts "diagnostic output" if options[:verbose]
|
304
|
+
# Kernel.exec(*args)
|
305
|
+
# end
|
306
|
+
#
|
307
|
+
# Here +exec+ can be called with +--verbose+ to get diagnostic output,
|
308
|
+
# e.g.:
|
309
|
+
#
|
310
|
+
# $ thor exec --verbose echo foo
|
311
|
+
# diagnostic output
|
312
|
+
# foo
|
313
|
+
#
|
314
|
+
# But if +--verbose+ is given after +echo+, it is passed to +echo+ instead:
|
315
|
+
#
|
316
|
+
# $ thor exec echo --verbose foo
|
317
|
+
# --verbose foo
|
318
|
+
#
|
319
|
+
# ==== Parameters
|
320
|
+
# Symbol ...:: A list of commands that should be affected.
|
321
|
+
def stop_on_unknown_option!(*command_names)
|
322
|
+
stop_on_unknown_option.merge(command_names)
|
323
|
+
end
|
324
|
+
|
325
|
+
def stop_on_unknown_option?(command) #:nodoc:
|
326
|
+
command && stop_on_unknown_option.include?(command.name.to_sym)
|
327
|
+
end
|
328
|
+
|
329
|
+
protected
|
330
|
+
|
331
|
+
def stop_on_unknown_option #:nodoc:
|
332
|
+
@stop_on_unknown_option ||= Set.new
|
333
|
+
end
|
334
|
+
|
335
|
+
# The method responsible for dispatching given the args.
|
336
|
+
def dispatch(meth, given_args, given_opts, config) #:nodoc: # rubocop:disable MethodLength
|
337
|
+
meth ||= retrieve_command_name(given_args)
|
338
|
+
command = all_commands[normalize_command_name(meth)]
|
339
|
+
|
340
|
+
if !command && config[:invoked_via_subcommand]
|
341
|
+
# We're a subcommand and our first argument didn't match any of our
|
342
|
+
# commands. So we put it back and call our default command.
|
343
|
+
given_args.unshift(meth)
|
344
|
+
command = all_commands[normalize_command_name(default_command)]
|
345
|
+
end
|
346
|
+
|
347
|
+
if command
|
348
|
+
args, opts = Foreman::Thor::Options.split(given_args)
|
349
|
+
if stop_on_unknown_option?(command) && !args.empty?
|
350
|
+
# given_args starts with a non-option, so we treat everything as
|
351
|
+
# ordinary arguments
|
352
|
+
args.concat opts
|
353
|
+
opts.clear
|
354
|
+
end
|
355
|
+
else
|
356
|
+
args = given_args
|
357
|
+
opts = nil
|
358
|
+
command = dynamic_command_class.new(meth)
|
359
|
+
end
|
360
|
+
|
361
|
+
opts = given_opts || opts || []
|
362
|
+
config[:current_command] = command
|
363
|
+
config[:command_options] = command.options
|
364
|
+
|
365
|
+
instance = new(args, opts, config)
|
366
|
+
yield instance if block_given?
|
367
|
+
args = instance.args
|
368
|
+
trailing = args[Range.new(arguments.size, -1)]
|
369
|
+
instance.invoke_command(command, trailing || [])
|
370
|
+
end
|
371
|
+
|
372
|
+
# The banner for this class. You can customize it if you are invoking the
|
373
|
+
# thor class by another ways which is not the Foreman::Thor::Runner. It receives
|
374
|
+
# the command that is going to be invoked and a boolean which indicates if
|
375
|
+
# the namespace should be displayed as arguments.
|
376
|
+
#
|
377
|
+
def banner(command, namespace = nil, subcommand = false)
|
378
|
+
"#{basename} #{command.formatted_usage(self, $thor_runner, subcommand)}"
|
379
|
+
end
|
380
|
+
|
381
|
+
def baseclass #:nodoc:
|
382
|
+
Foreman::Thor
|
383
|
+
end
|
384
|
+
|
385
|
+
def dynamic_command_class #:nodoc:
|
386
|
+
Foreman::Thor::DynamicCommand
|
387
|
+
end
|
388
|
+
|
389
|
+
def create_command(meth) #:nodoc:
|
390
|
+
@usage ||= nil
|
391
|
+
@desc ||= nil
|
392
|
+
@long_desc ||= nil
|
393
|
+
@disable_class_options ||= nil
|
394
|
+
|
395
|
+
if @usage && @desc
|
396
|
+
base_class = @hide ? Foreman::Thor::HiddenCommand : Foreman::Thor::Command
|
397
|
+
commands[meth] = base_class.new(meth, @desc, @long_desc, @usage, method_options, @disable_class_options)
|
398
|
+
@usage, @desc, @long_desc, @method_options, @hide, @disable_class_options = nil
|
399
|
+
true
|
400
|
+
elsif all_commands[meth] || meth == "method_missing"
|
401
|
+
true
|
402
|
+
else
|
403
|
+
puts "[WARNING] Attempted to create command #{meth.inspect} without usage or description. " \
|
404
|
+
"Call desc if you want this method to be available as command or declare it inside a " \
|
405
|
+
"no_commands{} block. Invoked from #{caller[1].inspect}."
|
406
|
+
false
|
407
|
+
end
|
408
|
+
end
|
409
|
+
alias_method :create_task, :create_command
|
410
|
+
|
411
|
+
def initialize_added #:nodoc:
|
412
|
+
class_options.merge!(method_options)
|
413
|
+
@method_options = nil
|
414
|
+
end
|
415
|
+
|
416
|
+
# Retrieve the command name from given args.
|
417
|
+
def retrieve_command_name(args) #:nodoc:
|
418
|
+
meth = args.first.to_s unless args.empty?
|
419
|
+
args.shift if meth && (map[meth] || meth !~ /^\-/)
|
420
|
+
end
|
421
|
+
alias_method :retrieve_task_name, :retrieve_command_name
|
422
|
+
|
423
|
+
# receives a (possibly nil) command name and returns a name that is in
|
424
|
+
# the commands hash. In addition to normalizing aliases, this logic
|
425
|
+
# will determine if a shortened command is an unambiguous substring of
|
426
|
+
# a command or alias.
|
427
|
+
#
|
428
|
+
# +normalize_command_name+ also converts names like +animal-prison+
|
429
|
+
# into +animal_prison+.
|
430
|
+
def normalize_command_name(meth) #:nodoc:
|
431
|
+
return default_command.to_s.tr("-", "_") unless meth
|
432
|
+
|
433
|
+
possibilities = find_command_possibilities(meth)
|
434
|
+
raise AmbiguousTaskError, "Ambiguous command #{meth} matches [#{possibilities.join(', ')}]" if possibilities.size > 1
|
435
|
+
|
436
|
+
if possibilities.empty?
|
437
|
+
meth ||= default_command
|
438
|
+
elsif map[meth]
|
439
|
+
meth = map[meth]
|
440
|
+
else
|
441
|
+
meth = possibilities.first
|
442
|
+
end
|
443
|
+
|
444
|
+
meth.to_s.tr("-", "_") # treat foo-bar as foo_bar
|
445
|
+
end
|
446
|
+
alias_method :normalize_task_name, :normalize_command_name
|
447
|
+
|
448
|
+
# this is the logic that takes the command name passed in by the user
|
449
|
+
# and determines whether it is an unambiguous substrings of a command or
|
450
|
+
# alias name.
|
451
|
+
def find_command_possibilities(meth)
|
452
|
+
len = meth.to_s.length
|
453
|
+
possibilities = all_commands.merge(map).keys.select { |n| meth == n[0, len] }.sort
|
454
|
+
unique_possibilities = possibilities.map { |k| map[k] || k }.uniq
|
455
|
+
|
456
|
+
if possibilities.include?(meth)
|
457
|
+
[meth]
|
458
|
+
elsif unique_possibilities.size == 1
|
459
|
+
unique_possibilities
|
460
|
+
else
|
461
|
+
possibilities
|
462
|
+
end
|
463
|
+
end
|
464
|
+
alias_method :find_task_possibilities, :find_command_possibilities
|
465
|
+
|
466
|
+
def subcommand_help(cmd)
|
467
|
+
desc "help [COMMAND]", "Describe subcommands or one specific subcommand"
|
468
|
+
class_eval "
|
469
|
+
def help(command = nil, subcommand = true); super; end
|
470
|
+
"
|
471
|
+
end
|
472
|
+
alias_method :subtask_help, :subcommand_help
|
473
|
+
end
|
474
|
+
|
475
|
+
include Foreman::Thor::Base
|
476
|
+
|
477
|
+
map HELP_MAPPINGS => :help
|
478
|
+
|
479
|
+
desc "help [COMMAND]", "Describe available commands or one specific command"
|
480
|
+
disable_class_options
|
481
|
+
def help(command = nil, subcommand = false)
|
482
|
+
if command
|
483
|
+
if self.class.subcommands.include? command
|
484
|
+
self.class.subcommand_classes[command].help(shell, true)
|
485
|
+
else
|
486
|
+
self.class.command_help(shell, command)
|
487
|
+
end
|
488
|
+
else
|
489
|
+
self.class.help(shell, subcommand)
|
490
|
+
end
|
491
|
+
end
|
492
|
+
end
|