foreman 0.85.0 → 0.87.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/data/export/systemd/master.target.erb +1 -1
  4. data/data/export/systemd/process.service.erb +6 -3
  5. data/lib/foreman/cli.rb +3 -3
  6. data/lib/foreman/export/systemd.rb +7 -13
  7. data/lib/foreman/vendor/thor/lib/thor.rb +492 -0
  8. data/lib/foreman/vendor/thor/lib/thor/actions.rb +318 -0
  9. data/lib/foreman/vendor/thor/lib/thor/actions/create_file.rb +103 -0
  10. data/lib/foreman/vendor/thor/lib/thor/actions/create_link.rb +59 -0
  11. data/lib/foreman/vendor/thor/lib/thor/actions/directory.rb +118 -0
  12. data/lib/foreman/vendor/thor/lib/thor/actions/empty_directory.rb +135 -0
  13. data/lib/foreman/vendor/thor/lib/thor/actions/file_manipulation.rb +327 -0
  14. data/lib/foreman/vendor/thor/lib/thor/actions/inject_into_file.rb +103 -0
  15. data/lib/foreman/vendor/thor/lib/thor/base.rb +656 -0
  16. data/lib/foreman/vendor/thor/lib/thor/command.rb +133 -0
  17. data/lib/foreman/vendor/thor/lib/thor/core_ext/hash_with_indifferent_access.rb +85 -0
  18. data/lib/foreman/vendor/thor/lib/thor/core_ext/io_binary_read.rb +12 -0
  19. data/lib/foreman/vendor/thor/lib/thor/core_ext/ordered_hash.rb +129 -0
  20. data/lib/foreman/vendor/thor/lib/thor/error.rb +32 -0
  21. data/lib/foreman/vendor/thor/lib/thor/group.rb +281 -0
  22. data/lib/foreman/vendor/thor/lib/thor/invocation.rb +177 -0
  23. data/lib/foreman/vendor/thor/lib/thor/line_editor.rb +17 -0
  24. data/lib/foreman/vendor/thor/lib/thor/line_editor/basic.rb +35 -0
  25. data/lib/foreman/vendor/thor/lib/thor/line_editor/readline.rb +88 -0
  26. data/lib/foreman/vendor/thor/lib/thor/parser.rb +4 -0
  27. data/lib/foreman/vendor/thor/lib/thor/parser/argument.rb +70 -0
  28. data/lib/foreman/vendor/thor/lib/thor/parser/arguments.rb +175 -0
  29. data/lib/foreman/vendor/thor/lib/thor/parser/option.rb +146 -0
  30. data/lib/foreman/vendor/thor/lib/thor/parser/options.rb +220 -0
  31. data/lib/foreman/vendor/thor/lib/thor/rake_compat.rb +71 -0
  32. data/lib/foreman/vendor/thor/lib/thor/runner.rb +322 -0
  33. data/lib/foreman/vendor/thor/lib/thor/shell.rb +81 -0
  34. data/lib/foreman/vendor/thor/lib/thor/shell/basic.rb +436 -0
  35. data/lib/foreman/vendor/thor/lib/thor/shell/color.rb +149 -0
  36. data/lib/foreman/vendor/thor/lib/thor/shell/html.rb +126 -0
  37. data/lib/foreman/vendor/thor/lib/thor/util.rb +268 -0
  38. data/lib/foreman/vendor/thor/lib/thor/version.rb +3 -0
  39. data/lib/foreman/version.rb +1 -1
  40. data/man/foreman.1 +1 -1
  41. data/spec/foreman/export/systemd_spec.rb +88 -46
  42. data/spec/resources/export/systemd/{app-alpha@.service → app-alpha.1.service} +6 -3
  43. data/spec/resources/export/systemd/{app-bravo@.service → app-alpha.2.service} +6 -3
  44. data/spec/resources/export/systemd/app-bravo.1.service +18 -0
  45. data/spec/resources/export/systemd/app.target +1 -1
  46. data/spec/spec_helper.rb +1 -1
  47. metadata +42 -25
  48. data/data/export/systemd/process_master.target.erb +0 -2
@@ -0,0 +1,103 @@
1
+ require "foreman/vendor/thor/lib/thor/actions/empty_directory"
2
+
3
+ class Foreman::Thor
4
+ module Actions
5
+ # Injects the given content into a file. Different from gsub_file, this
6
+ # method is reversible.
7
+ #
8
+ # ==== Parameters
9
+ # destination<String>:: Relative path to the destination root
10
+ # data<String>:: Data to add to the file. Can be given as a block.
11
+ # config<Hash>:: give :verbose => false to not log the status and the flag
12
+ # for injection (:after or :before) or :force => true for
13
+ # insert two or more times the same content.
14
+ #
15
+ # ==== Examples
16
+ #
17
+ # insert_into_file "config/environment.rb", "config.gem :thor", :after => "Rails::Initializer.run do |config|\n"
18
+ #
19
+ # insert_into_file "config/environment.rb", :after => "Rails::Initializer.run do |config|\n" do
20
+ # gems = ask "Which gems would you like to add?"
21
+ # gems.split(" ").map{ |gem| " config.gem :#{gem}" }.join("\n")
22
+ # end
23
+ #
24
+ def insert_into_file(destination, *args, &block)
25
+ data = block_given? ? block : args.shift
26
+ config = args.shift
27
+ action InjectIntoFile.new(self, destination, data, config)
28
+ end
29
+ alias_method :inject_into_file, :insert_into_file
30
+
31
+ class InjectIntoFile < EmptyDirectory #:nodoc:
32
+ attr_reader :replacement, :flag, :behavior
33
+
34
+ def initialize(base, destination, data, config)
35
+ super(base, destination, {:verbose => true}.merge(config))
36
+
37
+ @behavior, @flag = if @config.key?(:after)
38
+ [:after, @config.delete(:after)]
39
+ else
40
+ [:before, @config.delete(:before)]
41
+ end
42
+
43
+ @replacement = data.is_a?(Proc) ? data.call : data
44
+ @flag = Regexp.escape(@flag) unless @flag.is_a?(Regexp)
45
+ end
46
+
47
+ def invoke!
48
+ say_status :invoke
49
+
50
+ content = if @behavior == :after
51
+ '\0' + replacement
52
+ else
53
+ replacement + '\0'
54
+ end
55
+
56
+ replace!(/#{flag}/, content, config[:force])
57
+ end
58
+
59
+ def revoke!
60
+ say_status :revoke
61
+
62
+ regexp = if @behavior == :after
63
+ content = '\1\2'
64
+ /(#{flag})(.*)(#{Regexp.escape(replacement)})/m
65
+ else
66
+ content = '\2\3'
67
+ /(#{Regexp.escape(replacement)})(.*)(#{flag})/m
68
+ end
69
+
70
+ replace!(regexp, content, true)
71
+ end
72
+
73
+ protected
74
+
75
+ def say_status(behavior)
76
+ status = if behavior == :invoke
77
+ if flag == /\A/
78
+ :prepend
79
+ elsif flag == /\z/
80
+ :append
81
+ else
82
+ :insert
83
+ end
84
+ else
85
+ :subtract
86
+ end
87
+
88
+ super(status, config[:verbose])
89
+ end
90
+
91
+ # Adds the content to the file.
92
+ #
93
+ def replace!(regexp, string, force)
94
+ return if base.options[:pretend]
95
+ content = File.binread(destination)
96
+ if force || !content.include?(replacement)
97
+ content.gsub!(regexp, string)
98
+ File.open(destination, "wb") { |file| file.write(content) }
99
+ end
100
+ end
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,656 @@
1
+ require "foreman/vendor/thor/lib/thor/command"
2
+ require "foreman/vendor/thor/lib/thor/core_ext/hash_with_indifferent_access"
3
+ require "foreman/vendor/thor/lib/thor/core_ext/ordered_hash"
4
+ require "foreman/vendor/thor/lib/thor/error"
5
+ require "foreman/vendor/thor/lib/thor/invocation"
6
+ require "foreman/vendor/thor/lib/thor/parser"
7
+ require "foreman/vendor/thor/lib/thor/shell"
8
+ require "foreman/vendor/thor/lib/thor/line_editor"
9
+ require "foreman/vendor/thor/lib/thor/util"
10
+
11
+ class Foreman::Thor
12
+ autoload :Actions, "foreman/vendor/thor/lib/thor/actions"
13
+ autoload :RakeCompat, "foreman/vendor/thor/lib/thor/rake_compat"
14
+ autoload :Group, "foreman/vendor/thor/lib/thor/group"
15
+
16
+ # Shortcuts for help.
17
+ HELP_MAPPINGS = %w(-h -? --help -D)
18
+
19
+ # Foreman::Thor methods that should not be overwritten by the user.
20
+ THOR_RESERVED_WORDS = %w(invoke shell options behavior root destination_root relative_root
21
+ action add_file create_file in_root inside run run_ruby_script)
22
+
23
+ TEMPLATE_EXTNAME = ".tt"
24
+
25
+ module Base
26
+ attr_accessor :options, :parent_options, :args
27
+
28
+ # It receives arguments in an Array and two hashes, one for options and
29
+ # other for configuration.
30
+ #
31
+ # Notice that it does not check if all required arguments were supplied.
32
+ # It should be done by the parser.
33
+ #
34
+ # ==== Parameters
35
+ # args<Array[Object]>:: An array of objects. The objects are applied to their
36
+ # respective accessors declared with <tt>argument</tt>.
37
+ #
38
+ # options<Hash>:: An options hash that will be available as self.options.
39
+ # The hash given is converted to a hash with indifferent
40
+ # access, magic predicates (options.skip?) and then frozen.
41
+ #
42
+ # config<Hash>:: Configuration for this Foreman::Thor class.
43
+ #
44
+ def initialize(args = [], local_options = {}, config = {})
45
+ parse_options = config[:current_command] && config[:current_command].disable_class_options ? {} : self.class.class_options
46
+
47
+ # The start method splits inbound arguments at the first argument
48
+ # that looks like an option (starts with - or --). It then calls
49
+ # new, passing in the two halves of the arguments Array as the
50
+ # first two parameters.
51
+
52
+ command_options = config.delete(:command_options) # hook for start
53
+ parse_options = parse_options.merge(command_options) if command_options
54
+ if local_options.is_a?(Array)
55
+ array_options = local_options
56
+ hash_options = {}
57
+ else
58
+ # Handle the case where the class was explicitly instantiated
59
+ # with pre-parsed options.
60
+ array_options = []
61
+ hash_options = local_options
62
+ end
63
+
64
+ # Let Foreman::Thor::Options parse the options first, so it can remove
65
+ # declared options from the array. This will leave us with
66
+ # a list of arguments that weren't declared.
67
+ stop_on_unknown = self.class.stop_on_unknown_option? config[:current_command]
68
+ opts = Foreman::Thor::Options.new(parse_options, hash_options, stop_on_unknown)
69
+ self.options = opts.parse(array_options)
70
+ self.options = config[:class_options].merge(options) if config[:class_options]
71
+
72
+ # If unknown options are disallowed, make sure that none of the
73
+ # remaining arguments looks like an option.
74
+ opts.check_unknown! if self.class.check_unknown_options?(config)
75
+
76
+ # Add the remaining arguments from the options parser to the
77
+ # arguments passed in to initialize. Then remove any positional
78
+ # arguments declared using #argument (this is primarily used
79
+ # by Foreman::Thor::Group). Tis will leave us with the remaining
80
+ # positional arguments.
81
+ to_parse = args
82
+ to_parse += opts.remaining unless self.class.strict_args_position?(config)
83
+
84
+ thor_args = Foreman::Thor::Arguments.new(self.class.arguments)
85
+ thor_args.parse(to_parse).each { |k, v| __send__("#{k}=", v) }
86
+ @args = thor_args.remaining
87
+ end
88
+
89
+ class << self
90
+ def included(base) #:nodoc:
91
+ base.extend ClassMethods
92
+ base.send :include, Invocation
93
+ base.send :include, Shell
94
+ end
95
+
96
+ # Returns the classes that inherits from Foreman::Thor or Foreman::Thor::Group.
97
+ #
98
+ # ==== Returns
99
+ # Array[Class]
100
+ #
101
+ def subclasses
102
+ @subclasses ||= []
103
+ end
104
+
105
+ # Returns the files where the subclasses are kept.
106
+ #
107
+ # ==== Returns
108
+ # Hash[path<String> => Class]
109
+ #
110
+ def subclass_files
111
+ @subclass_files ||= Hash.new { |h, k| h[k] = [] }
112
+ end
113
+
114
+ # Whenever a class inherits from Foreman::Thor or Foreman::Thor::Group, we should track the
115
+ # class and the file on Foreman::Thor::Base. This is the method responsable for it.
116
+ #
117
+ def register_klass_file(klass) #:nodoc:
118
+ file = caller[1].match(/(.*):\d+/)[1]
119
+ Foreman::Thor::Base.subclasses << klass unless Foreman::Thor::Base.subclasses.include?(klass)
120
+
121
+ file_subclasses = Foreman::Thor::Base.subclass_files[File.expand_path(file)]
122
+ file_subclasses << klass unless file_subclasses.include?(klass)
123
+ end
124
+ end
125
+
126
+ module ClassMethods
127
+ def attr_reader(*) #:nodoc:
128
+ no_commands { super }
129
+ end
130
+
131
+ def attr_writer(*) #:nodoc:
132
+ no_commands { super }
133
+ end
134
+
135
+ def attr_accessor(*) #:nodoc:
136
+ no_commands { super }
137
+ end
138
+
139
+ # If you want to raise an error for unknown options, call check_unknown_options!
140
+ # This is disabled by default to allow dynamic invocations.
141
+ def check_unknown_options!
142
+ @check_unknown_options = true
143
+ end
144
+
145
+ def check_unknown_options #:nodoc:
146
+ @check_unknown_options ||= from_superclass(:check_unknown_options, false)
147
+ end
148
+
149
+ def check_unknown_options?(config) #:nodoc:
150
+ !!check_unknown_options
151
+ end
152
+
153
+ # If true, option parsing is suspended as soon as an unknown option or a
154
+ # regular argument is encountered. All remaining arguments are passed to
155
+ # the command as regular arguments.
156
+ def stop_on_unknown_option?(command_name) #:nodoc:
157
+ false
158
+ end
159
+
160
+ # If you want only strict string args (useful when cascading thor classes),
161
+ # call strict_args_position! This is disabled by default to allow dynamic
162
+ # invocations.
163
+ def strict_args_position!
164
+ @strict_args_position = true
165
+ end
166
+
167
+ def strict_args_position #:nodoc:
168
+ @strict_args_position ||= from_superclass(:strict_args_position, false)
169
+ end
170
+
171
+ def strict_args_position?(config) #:nodoc:
172
+ !!strict_args_position
173
+ end
174
+
175
+ # Adds an argument to the class and creates an attr_accessor for it.
176
+ #
177
+ # Arguments are different from options in several aspects. The first one
178
+ # is how they are parsed from the command line, arguments are retrieved
179
+ # from position:
180
+ #
181
+ # thor command NAME
182
+ #
183
+ # Instead of:
184
+ #
185
+ # thor command --name=NAME
186
+ #
187
+ # Besides, arguments are used inside your code as an accessor (self.argument),
188
+ # while options are all kept in a hash (self.options).
189
+ #
190
+ # Finally, arguments cannot have type :default or :boolean but can be
191
+ # optional (supplying :optional => :true or :required => false), although
192
+ # you cannot have a required argument after a non-required argument. If you
193
+ # try it, an error is raised.
194
+ #
195
+ # ==== Parameters
196
+ # name<Symbol>:: The name of the argument.
197
+ # options<Hash>:: Described below.
198
+ #
199
+ # ==== Options
200
+ # :desc - Description for the argument.
201
+ # :required - If the argument is required or not.
202
+ # :optional - If the argument is optional or not.
203
+ # :type - The type of the argument, can be :string, :hash, :array, :numeric.
204
+ # :default - Default value for this argument. It cannot be required and have default values.
205
+ # :banner - String to show on usage notes.
206
+ #
207
+ # ==== Errors
208
+ # ArgumentError:: Raised if you supply a required argument after a non required one.
209
+ #
210
+ def argument(name, options = {})
211
+ is_thor_reserved_word?(name, :argument)
212
+ no_commands { attr_accessor name }
213
+
214
+ required = if options.key?(:optional)
215
+ !options[:optional]
216
+ elsif options.key?(:required)
217
+ options[:required]
218
+ else
219
+ options[:default].nil?
220
+ end
221
+
222
+ remove_argument name
223
+
224
+ if required
225
+ arguments.each do |argument|
226
+ next if argument.required?
227
+ raise ArgumentError, "You cannot have #{name.to_s.inspect} as required argument after " \
228
+ "the non-required argument #{argument.human_name.inspect}."
229
+ end
230
+ end
231
+
232
+ options[:required] = required
233
+
234
+ arguments << Foreman::Thor::Argument.new(name, options)
235
+ end
236
+
237
+ # Returns this class arguments, looking up in the ancestors chain.
238
+ #
239
+ # ==== Returns
240
+ # Array[Foreman::Thor::Argument]
241
+ #
242
+ def arguments
243
+ @arguments ||= from_superclass(:arguments, [])
244
+ end
245
+
246
+ # Adds a bunch of options to the set of class options.
247
+ #
248
+ # class_options :foo => false, :bar => :required, :baz => :string
249
+ #
250
+ # If you prefer more detailed declaration, check class_option.
251
+ #
252
+ # ==== Parameters
253
+ # Hash[Symbol => Object]
254
+ #
255
+ def class_options(options = nil)
256
+ @class_options ||= from_superclass(:class_options, {})
257
+ build_options(options, @class_options) if options
258
+ @class_options
259
+ end
260
+
261
+ # Adds an option to the set of class options
262
+ #
263
+ # ==== Parameters
264
+ # name<Symbol>:: The name of the argument.
265
+ # options<Hash>:: Described below.
266
+ #
267
+ # ==== Options
268
+ # :desc:: -- Description for the argument.
269
+ # :required:: -- If the argument is required or not.
270
+ # :default:: -- Default value for this argument.
271
+ # :group:: -- The group for this options. Use by class options to output options in different levels.
272
+ # :aliases:: -- Aliases for this option. <b>Note:</b> Foreman::Thor follows a convention of one-dash-one-letter options. Thus aliases like "-something" wouldn't be parsed; use either "\--something" or "-s" instead.
273
+ # :type:: -- The type of the argument, can be :string, :hash, :array, :numeric or :boolean.
274
+ # :banner:: -- String to show on usage notes.
275
+ # :hide:: -- If you want to hide this option from the help.
276
+ #
277
+ def class_option(name, options = {})
278
+ build_option(name, options, class_options)
279
+ end
280
+
281
+ # Removes a previous defined argument. If :undefine is given, undefine
282
+ # accessors as well.
283
+ #
284
+ # ==== Parameters
285
+ # names<Array>:: Arguments to be removed
286
+ #
287
+ # ==== Examples
288
+ #
289
+ # remove_argument :foo
290
+ # remove_argument :foo, :bar, :baz, :undefine => true
291
+ #
292
+ def remove_argument(*names)
293
+ options = names.last.is_a?(Hash) ? names.pop : {}
294
+
295
+ names.each do |name|
296
+ arguments.delete_if { |a| a.name == name.to_s }
297
+ undef_method name, "#{name}=" if options[:undefine]
298
+ end
299
+ end
300
+
301
+ # Removes a previous defined class option.
302
+ #
303
+ # ==== Parameters
304
+ # names<Array>:: Class options to be removed
305
+ #
306
+ # ==== Examples
307
+ #
308
+ # remove_class_option :foo
309
+ # remove_class_option :foo, :bar, :baz
310
+ #
311
+ def remove_class_option(*names)
312
+ names.each do |name|
313
+ class_options.delete(name)
314
+ end
315
+ end
316
+
317
+ # Defines the group. This is used when thor list is invoked so you can specify
318
+ # that only commands from a pre-defined group will be shown. Defaults to standard.
319
+ #
320
+ # ==== Parameters
321
+ # name<String|Symbol>
322
+ #
323
+ def group(name = nil)
324
+ if name
325
+ @group = name.to_s
326
+ else
327
+ @group ||= from_superclass(:group, "standard")
328
+ end
329
+ end
330
+
331
+ # Returns the commands for this Foreman::Thor class.
332
+ #
333
+ # ==== Returns
334
+ # OrderedHash:: An ordered hash with commands names as keys and Foreman::Thor::Command
335
+ # objects as values.
336
+ #
337
+ def commands
338
+ @commands ||= Foreman::Thor::CoreExt::OrderedHash.new
339
+ end
340
+ alias_method :tasks, :commands
341
+
342
+ # Returns the commands for this Foreman::Thor class and all subclasses.
343
+ #
344
+ # ==== Returns
345
+ # OrderedHash:: An ordered hash with commands names as keys and Foreman::Thor::Command
346
+ # objects as values.
347
+ #
348
+ def all_commands
349
+ @all_commands ||= from_superclass(:all_commands, Foreman::Thor::CoreExt::OrderedHash.new)
350
+ @all_commands.merge!(commands)
351
+ end
352
+ alias_method :all_tasks, :all_commands
353
+
354
+ # Removes a given command from this Foreman::Thor class. This is usually done if you
355
+ # are inheriting from another class and don't want it to be available
356
+ # anymore.
357
+ #
358
+ # By default it only remove the mapping to the command. But you can supply
359
+ # :undefine => true to undefine the method from the class as well.
360
+ #
361
+ # ==== Parameters
362
+ # name<Symbol|String>:: The name of the command to be removed
363
+ # options<Hash>:: You can give :undefine => true if you want commands the method
364
+ # to be undefined from the class as well.
365
+ #
366
+ def remove_command(*names)
367
+ options = names.last.is_a?(Hash) ? names.pop : {}
368
+
369
+ names.each do |name|
370
+ commands.delete(name.to_s)
371
+ all_commands.delete(name.to_s)
372
+ undef_method name if options[:undefine]
373
+ end
374
+ end
375
+ alias_method :remove_task, :remove_command
376
+
377
+ # All methods defined inside the given block are not added as commands.
378
+ #
379
+ # So you can do:
380
+ #
381
+ # class MyScript < Foreman::Thor
382
+ # no_commands do
383
+ # def this_is_not_a_command
384
+ # end
385
+ # end
386
+ # end
387
+ #
388
+ # You can also add the method and remove it from the command list:
389
+ #
390
+ # class MyScript < Foreman::Thor
391
+ # def this_is_not_a_command
392
+ # end
393
+ # remove_command :this_is_not_a_command
394
+ # end
395
+ #
396
+ def no_commands
397
+ @no_commands = true
398
+ yield
399
+ ensure
400
+ @no_commands = false
401
+ end
402
+ alias_method :no_tasks, :no_commands
403
+
404
+ # Sets the namespace for the Foreman::Thor or Foreman::Thor::Group class. By default the
405
+ # namespace is retrieved from the class name. If your Foreman::Thor class is named
406
+ # Scripts::MyScript, the help method, for example, will be called as:
407
+ #
408
+ # thor scripts:my_script -h
409
+ #
410
+ # If you change the namespace:
411
+ #
412
+ # namespace :my_scripts
413
+ #
414
+ # You change how your commands are invoked:
415
+ #
416
+ # thor my_scripts -h
417
+ #
418
+ # Finally, if you change your namespace to default:
419
+ #
420
+ # namespace :default
421
+ #
422
+ # Your commands can be invoked with a shortcut. Instead of:
423
+ #
424
+ # thor :my_command
425
+ #
426
+ def namespace(name = nil)
427
+ if name
428
+ @namespace = name.to_s
429
+ else
430
+ @namespace ||= Foreman::Thor::Util.namespace_from_thor_class(self)
431
+ end
432
+ end
433
+
434
+ # Parses the command and options from the given args, instantiate the class
435
+ # and invoke the command. This method is used when the arguments must be parsed
436
+ # from an array. If you are inside Ruby and want to use a Foreman::Thor class, you
437
+ # can simply initialize it:
438
+ #
439
+ # script = MyScript.new(args, options, config)
440
+ # script.invoke(:command, first_arg, second_arg, third_arg)
441
+ #
442
+ def start(given_args = ARGV, config = {})
443
+ config[:shell] ||= Foreman::Thor::Base.shell.new
444
+ dispatch(nil, given_args.dup, nil, config)
445
+ rescue Foreman::Thor::Error => e
446
+ config[:debug] || ENV["THOR_DEBUG"] == "1" ? (raise e) : config[:shell].error(e.message)
447
+ exit(1) if exit_on_failure?
448
+ rescue Errno::EPIPE
449
+ # This happens if a thor command is piped to something like `head`,
450
+ # which closes the pipe when it's done reading. This will also
451
+ # mean that if the pipe is closed, further unnecessary
452
+ # computation will not occur.
453
+ exit(0)
454
+ end
455
+
456
+ # Allows to use private methods from parent in child classes as commands.
457
+ #
458
+ # ==== Parameters
459
+ # names<Array>:: Method names to be used as commands
460
+ #
461
+ # ==== Examples
462
+ #
463
+ # public_command :foo
464
+ # public_command :foo, :bar, :baz
465
+ #
466
+ def public_command(*names)
467
+ names.each do |name|
468
+ class_eval "def #{name}(*); super end"
469
+ end
470
+ end
471
+ alias_method :public_task, :public_command
472
+
473
+ def handle_no_command_error(command, has_namespace = $thor_runner) #:nodoc:
474
+ raise UndefinedCommandError, "Could not find command #{command.inspect} in #{namespace.inspect} namespace." if has_namespace
475
+ raise UndefinedCommandError, "Could not find command #{command.inspect}."
476
+ end
477
+ alias_method :handle_no_task_error, :handle_no_command_error
478
+
479
+ def handle_argument_error(command, error, args, arity) #:nodoc:
480
+ msg = "ERROR: \"#{basename} #{command.name}\" was called with "
481
+ msg << "no arguments" if args.empty?
482
+ msg << "arguments " << args.inspect unless args.empty?
483
+ msg << "\nUsage: #{banner(command).inspect}"
484
+ raise InvocationError, msg
485
+ end
486
+
487
+ protected
488
+
489
+ # Prints the class options per group. If an option does not belong to
490
+ # any group, it's printed as Class option.
491
+ #
492
+ def class_options_help(shell, groups = {}) #:nodoc:
493
+ # Group options by group
494
+ class_options.each do |_, value|
495
+ groups[value.group] ||= []
496
+ groups[value.group] << value
497
+ end
498
+
499
+ # Deal with default group
500
+ global_options = groups.delete(nil) || []
501
+ print_options(shell, global_options)
502
+
503
+ # Print all others
504
+ groups.each do |group_name, options|
505
+ print_options(shell, options, group_name)
506
+ end
507
+ end
508
+
509
+ # Receives a set of options and print them.
510
+ def print_options(shell, options, group_name = nil)
511
+ return if options.empty?
512
+
513
+ list = []
514
+ padding = options.map { |o| o.aliases.size }.max.to_i * 4
515
+
516
+ options.each do |option|
517
+ next if option.hide
518
+ item = [option.usage(padding)]
519
+ item.push(option.description ? "# #{option.description}" : "")
520
+
521
+ list << item
522
+ list << ["", "# Default: #{option.default}"] if option.show_default?
523
+ list << ["", "# Possible values: #{option.enum.join(', ')}"] if option.enum
524
+ end
525
+
526
+ shell.say(group_name ? "#{group_name} options:" : "Options:")
527
+ shell.print_table(list, :indent => 2)
528
+ shell.say ""
529
+ end
530
+
531
+ # Raises an error if the word given is a Foreman::Thor reserved word.
532
+ def is_thor_reserved_word?(word, type) #:nodoc:
533
+ return false unless THOR_RESERVED_WORDS.include?(word.to_s)
534
+ raise "#{word.inspect} is a Foreman::Thor reserved word and cannot be defined as #{type}"
535
+ end
536
+
537
+ # Build an option and adds it to the given scope.
538
+ #
539
+ # ==== Parameters
540
+ # name<Symbol>:: The name of the argument.
541
+ # options<Hash>:: Described in both class_option and method_option.
542
+ # scope<Hash>:: Options hash that is being built up
543
+ def build_option(name, options, scope) #:nodoc:
544
+ scope[name] = Foreman::Thor::Option.new(name, options)
545
+ end
546
+
547
+ # Receives a hash of options, parse them and add to the scope. This is a
548
+ # fast way to set a bunch of options:
549
+ #
550
+ # build_options :foo => true, :bar => :required, :baz => :string
551
+ #
552
+ # ==== Parameters
553
+ # Hash[Symbol => Object]
554
+ def build_options(options, scope) #:nodoc:
555
+ options.each do |key, value|
556
+ scope[key] = Foreman::Thor::Option.parse(key, value)
557
+ end
558
+ end
559
+
560
+ # Finds a command with the given name. If the command belongs to the current
561
+ # class, just return it, otherwise dup it and add the fresh copy to the
562
+ # current command hash.
563
+ def find_and_refresh_command(name) #:nodoc:
564
+ if commands[name.to_s]
565
+ commands[name.to_s]
566
+ elsif command = all_commands[name.to_s] # rubocop:disable AssignmentInCondition
567
+ commands[name.to_s] = command.clone
568
+ else
569
+ raise ArgumentError, "You supplied :for => #{name.inspect}, but the command #{name.inspect} could not be found."
570
+ end
571
+ end
572
+ alias_method :find_and_refresh_task, :find_and_refresh_command
573
+
574
+ # Everytime someone inherits from a Foreman::Thor class, register the klass
575
+ # and file into baseclass.
576
+ def inherited(klass)
577
+ Foreman::Thor::Base.register_klass_file(klass)
578
+ klass.instance_variable_set(:@no_commands, false)
579
+ end
580
+
581
+ # Fire this callback whenever a method is added. Added methods are
582
+ # tracked as commands by invoking the create_command method.
583
+ def method_added(meth)
584
+ meth = meth.to_s
585
+
586
+ if meth == "initialize"
587
+ initialize_added
588
+ return
589
+ end
590
+
591
+ # Return if it's not a public instance method
592
+ return unless public_method_defined?(meth.to_sym)
593
+
594
+ @no_commands ||= false
595
+ return if @no_commands || !create_command(meth)
596
+
597
+ is_thor_reserved_word?(meth, :command)
598
+ Foreman::Thor::Base.register_klass_file(self)
599
+ end
600
+
601
+ # Retrieves a value from superclass. If it reaches the baseclass,
602
+ # returns default.
603
+ def from_superclass(method, default = nil)
604
+ if self == baseclass || !superclass.respond_to?(method, true)
605
+ default
606
+ else
607
+ value = superclass.send(method)
608
+
609
+ # Ruby implements `dup` on Object, but raises a `TypeError`
610
+ # if the method is called on immediates. As a result, we
611
+ # don't have a good way to check whether dup will succeed
612
+ # without calling it and rescuing the TypeError.
613
+ begin
614
+ value.dup
615
+ rescue TypeError
616
+ value
617
+ end
618
+
619
+ end
620
+ end
621
+
622
+ # A flag that makes the process exit with status 1 if any error happens.
623
+ def exit_on_failure?
624
+ false
625
+ end
626
+
627
+ #
628
+ # The basename of the program invoking the thor class.
629
+ #
630
+ def basename
631
+ File.basename($PROGRAM_NAME).split(" ").first
632
+ end
633
+
634
+ # SIGNATURE: Sets the baseclass. This is where the superclass lookup
635
+ # finishes.
636
+ def baseclass #:nodoc:
637
+ end
638
+
639
+ # SIGNATURE: Creates a new command if valid_command? is true. This method is
640
+ # called when a new method is added to the class.
641
+ def create_command(meth) #:nodoc:
642
+ end
643
+ alias_method :create_task, :create_command
644
+
645
+ # SIGNATURE: Defines behavior when the initialize method is added to the
646
+ # class.
647
+ def initialize_added #:nodoc:
648
+ end
649
+
650
+ # SIGNATURE: The hook invoked by start.
651
+ def dispatch(command, given_args, given_opts, config) #:nodoc:
652
+ raise NotImplementedError
653
+ end
654
+ end
655
+ end
656
+ end