foreman 0.85.0 → 0.87.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/data/export/systemd/master.target.erb +1 -1
- data/data/export/systemd/process.service.erb +6 -3
- data/lib/foreman/cli.rb +3 -3
- data/lib/foreman/export/systemd.rb +7 -13
- 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/actions.rb +318 -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/basic.rb +35 -0
- data/lib/foreman/vendor/thor/lib/thor/line_editor/readline.rb +88 -0
- data/lib/foreman/vendor/thor/lib/thor/line_editor.rb +17 -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/parser.rb +4 -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/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/shell.rb +81 -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/vendor/thor/lib/thor.rb +492 -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/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 +1 -1
- metadata +39 -22
- data/data/export/systemd/process_master.target.erb +0 -2
@@ -0,0 +1,220 @@
|
|
1
|
+
class Foreman::Thor
|
2
|
+
class Options < Arguments #:nodoc: # rubocop:disable ClassLength
|
3
|
+
LONG_RE = /^(--\w+(?:-\w+)*)$/
|
4
|
+
SHORT_RE = /^(-[a-z])$/i
|
5
|
+
EQ_RE = /^(--\w+(?:-\w+)*|-[a-z])=(.*)$/i
|
6
|
+
SHORT_SQ_RE = /^-([a-z]{2,})$/i # Allow either -x -v or -xv style for single char args
|
7
|
+
SHORT_NUM = /^(-[a-z])#{NUMERIC}$/i
|
8
|
+
OPTS_END = "--".freeze
|
9
|
+
|
10
|
+
# Receives a hash and makes it switches.
|
11
|
+
def self.to_switches(options)
|
12
|
+
options.map do |key, value|
|
13
|
+
case value
|
14
|
+
when true
|
15
|
+
"--#{key}"
|
16
|
+
when Array
|
17
|
+
"--#{key} #{value.map(&:inspect).join(' ')}"
|
18
|
+
when Hash
|
19
|
+
"--#{key} #{value.map { |k, v| "#{k}:#{v}" }.join(' ')}"
|
20
|
+
when nil, false
|
21
|
+
""
|
22
|
+
else
|
23
|
+
"--#{key} #{value.inspect}"
|
24
|
+
end
|
25
|
+
end.join(" ")
|
26
|
+
end
|
27
|
+
|
28
|
+
# Takes a hash of Foreman::Thor::Option and a hash with defaults.
|
29
|
+
#
|
30
|
+
# If +stop_on_unknown+ is true, #parse will stop as soon as it encounters
|
31
|
+
# an unknown option or a regular argument.
|
32
|
+
def initialize(hash_options = {}, defaults = {}, stop_on_unknown = false)
|
33
|
+
@stop_on_unknown = stop_on_unknown
|
34
|
+
options = hash_options.values
|
35
|
+
super(options)
|
36
|
+
|
37
|
+
# Add defaults
|
38
|
+
defaults.each do |key, value|
|
39
|
+
@assigns[key.to_s] = value
|
40
|
+
@non_assigned_required.delete(hash_options[key])
|
41
|
+
end
|
42
|
+
|
43
|
+
@shorts = {}
|
44
|
+
@switches = {}
|
45
|
+
@extra = []
|
46
|
+
|
47
|
+
options.each do |option|
|
48
|
+
@switches[option.switch_name] = option
|
49
|
+
|
50
|
+
option.aliases.each do |short|
|
51
|
+
name = short.to_s.sub(/^(?!\-)/, "-")
|
52
|
+
@shorts[name] ||= option.switch_name
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def remaining
|
58
|
+
@extra
|
59
|
+
end
|
60
|
+
|
61
|
+
def peek
|
62
|
+
return super unless @parsing_options
|
63
|
+
|
64
|
+
result = super
|
65
|
+
if result == OPTS_END
|
66
|
+
shift
|
67
|
+
@parsing_options = false
|
68
|
+
super
|
69
|
+
else
|
70
|
+
result
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def parse(args) # rubocop:disable MethodLength
|
75
|
+
@pile = args.dup
|
76
|
+
@parsing_options = true
|
77
|
+
|
78
|
+
while peek
|
79
|
+
if parsing_options?
|
80
|
+
match, is_switch = current_is_switch?
|
81
|
+
shifted = shift
|
82
|
+
|
83
|
+
if is_switch
|
84
|
+
case shifted
|
85
|
+
when SHORT_SQ_RE
|
86
|
+
unshift($1.split("").map { |f| "-#{f}" })
|
87
|
+
next
|
88
|
+
when EQ_RE, SHORT_NUM
|
89
|
+
unshift($2)
|
90
|
+
switch = $1
|
91
|
+
when LONG_RE, SHORT_RE
|
92
|
+
switch = $1
|
93
|
+
end
|
94
|
+
|
95
|
+
switch = normalize_switch(switch)
|
96
|
+
option = switch_option(switch)
|
97
|
+
@assigns[option.human_name] = parse_peek(switch, option)
|
98
|
+
elsif @stop_on_unknown
|
99
|
+
@parsing_options = false
|
100
|
+
@extra << shifted
|
101
|
+
@extra << shift while peek
|
102
|
+
break
|
103
|
+
elsif match
|
104
|
+
@extra << shifted
|
105
|
+
@extra << shift while peek && peek !~ /^-/
|
106
|
+
else
|
107
|
+
@extra << shifted
|
108
|
+
end
|
109
|
+
else
|
110
|
+
@extra << shift
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
check_requirement!
|
115
|
+
|
116
|
+
assigns = Foreman::Thor::CoreExt::HashWithIndifferentAccess.new(@assigns)
|
117
|
+
assigns.freeze
|
118
|
+
assigns
|
119
|
+
end
|
120
|
+
|
121
|
+
def check_unknown!
|
122
|
+
# an unknown option starts with - or -- and has no more --'s afterward.
|
123
|
+
unknown = @extra.select { |str| str =~ /^--?(?:(?!--).)*$/ }
|
124
|
+
raise UnknownArgumentError, "Unknown switches '#{unknown.join(', ')}'" unless unknown.empty?
|
125
|
+
end
|
126
|
+
|
127
|
+
protected
|
128
|
+
|
129
|
+
# Check if the current value in peek is a registered switch.
|
130
|
+
#
|
131
|
+
# Two booleans are returned. The first is true if the current value
|
132
|
+
# starts with a hyphen; the second is true if it is a registered switch.
|
133
|
+
def current_is_switch?
|
134
|
+
case peek
|
135
|
+
when LONG_RE, SHORT_RE, EQ_RE, SHORT_NUM
|
136
|
+
[true, switch?($1)]
|
137
|
+
when SHORT_SQ_RE
|
138
|
+
[true, $1.split("").any? { |f| switch?("-#{f}") }]
|
139
|
+
else
|
140
|
+
[false, false]
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
def current_is_switch_formatted?
|
145
|
+
case peek
|
146
|
+
when LONG_RE, SHORT_RE, EQ_RE, SHORT_NUM, SHORT_SQ_RE
|
147
|
+
true
|
148
|
+
else
|
149
|
+
false
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
def current_is_value?
|
154
|
+
peek && (!parsing_options? || super)
|
155
|
+
end
|
156
|
+
|
157
|
+
def switch?(arg)
|
158
|
+
switch_option(normalize_switch(arg))
|
159
|
+
end
|
160
|
+
|
161
|
+
def switch_option(arg)
|
162
|
+
if match = no_or_skip?(arg) # rubocop:disable AssignmentInCondition
|
163
|
+
@switches[arg] || @switches["--#{match}"]
|
164
|
+
else
|
165
|
+
@switches[arg]
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
# Check if the given argument is actually a shortcut.
|
170
|
+
#
|
171
|
+
def normalize_switch(arg)
|
172
|
+
(@shorts[arg] || arg).tr("_", "-")
|
173
|
+
end
|
174
|
+
|
175
|
+
def parsing_options?
|
176
|
+
peek
|
177
|
+
@parsing_options
|
178
|
+
end
|
179
|
+
|
180
|
+
# Parse boolean values which can be given as --foo=true, --foo or --no-foo.
|
181
|
+
#
|
182
|
+
def parse_boolean(switch)
|
183
|
+
if current_is_value?
|
184
|
+
if ["true", "TRUE", "t", "T", true].include?(peek)
|
185
|
+
shift
|
186
|
+
true
|
187
|
+
elsif ["false", "FALSE", "f", "F", false].include?(peek)
|
188
|
+
shift
|
189
|
+
false
|
190
|
+
else
|
191
|
+
true
|
192
|
+
end
|
193
|
+
else
|
194
|
+
@switches.key?(switch) || !no_or_skip?(switch)
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
# Parse the value at the peek analyzing if it requires an input or not.
|
199
|
+
#
|
200
|
+
def parse_peek(switch, option)
|
201
|
+
if parsing_options? && (current_is_switch_formatted? || last?)
|
202
|
+
if option.boolean?
|
203
|
+
# No problem for boolean types
|
204
|
+
elsif no_or_skip?(switch)
|
205
|
+
return nil # User set value to nil
|
206
|
+
elsif option.string? && !option.required?
|
207
|
+
# Return the default if there is one, else the human name
|
208
|
+
return option.lazy_default || option.default || option.human_name
|
209
|
+
elsif option.lazy_default
|
210
|
+
return option.lazy_default
|
211
|
+
else
|
212
|
+
raise MalformattedArgumentError, "No value provided for option '#{switch}'"
|
213
|
+
end
|
214
|
+
end
|
215
|
+
|
216
|
+
@non_assigned_required.delete(option)
|
217
|
+
send(:"parse_#{option.type}", switch)
|
218
|
+
end
|
219
|
+
end
|
220
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require "rake"
|
2
|
+
require "rake/dsl_definition"
|
3
|
+
|
4
|
+
class Foreman::Thor
|
5
|
+
# Adds a compatibility layer to your Foreman::Thor classes which allows you to use
|
6
|
+
# rake package tasks. For example, to use rspec rake tasks, one can do:
|
7
|
+
#
|
8
|
+
# require 'foreman/vendor/thor/lib/thor/rake_compat'
|
9
|
+
# require 'rspec/core/rake_task'
|
10
|
+
#
|
11
|
+
# class Default < Foreman::Thor
|
12
|
+
# include Foreman::Thor::RakeCompat
|
13
|
+
#
|
14
|
+
# RSpec::Core::RakeTask.new(:spec) do |t|
|
15
|
+
# t.spec_opts = ['--options', './.rspec']
|
16
|
+
# t.spec_files = FileList['spec/**/*_spec.rb']
|
17
|
+
# end
|
18
|
+
# end
|
19
|
+
#
|
20
|
+
module RakeCompat
|
21
|
+
include Rake::DSL if defined?(Rake::DSL)
|
22
|
+
|
23
|
+
def self.rake_classes
|
24
|
+
@rake_classes ||= []
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.included(base)
|
28
|
+
# Hack. Make rakefile point to invoker, so rdoc task is generated properly.
|
29
|
+
rakefile = File.basename(caller[0].match(/(.*):\d+/)[1])
|
30
|
+
Rake.application.instance_variable_set(:@rakefile, rakefile)
|
31
|
+
rake_classes << base
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# override task on (main), for compatibility with Rake 0.9
|
37
|
+
instance_eval do
|
38
|
+
alias rake_namespace namespace
|
39
|
+
|
40
|
+
def task(*)
|
41
|
+
task = super
|
42
|
+
|
43
|
+
if klass = Foreman::Thor::RakeCompat.rake_classes.last # rubocop:disable AssignmentInCondition
|
44
|
+
non_namespaced_name = task.name.split(":").last
|
45
|
+
|
46
|
+
description = non_namespaced_name
|
47
|
+
description << task.arg_names.map { |n| n.to_s.upcase }.join(" ")
|
48
|
+
description.strip!
|
49
|
+
|
50
|
+
klass.desc description, Rake.application.last_description || non_namespaced_name
|
51
|
+
Rake.application.last_description = nil
|
52
|
+
klass.send :define_method, non_namespaced_name do |*args|
|
53
|
+
Rake::Task[task.name.to_sym].invoke(*args)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
task
|
58
|
+
end
|
59
|
+
|
60
|
+
def namespace(name)
|
61
|
+
if klass = Foreman::Thor::RakeCompat.rake_classes.last # rubocop:disable AssignmentInCondition
|
62
|
+
const_name = Foreman::Thor::Util.camel_case(name.to_s).to_sym
|
63
|
+
klass.const_set(const_name, Class.new(Foreman::Thor))
|
64
|
+
new_klass = klass.const_get(const_name)
|
65
|
+
Foreman::Thor::RakeCompat.rake_classes << new_klass
|
66
|
+
end
|
67
|
+
|
68
|
+
super
|
69
|
+
Foreman::Thor::RakeCompat.rake_classes.pop
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,322 @@
|
|
1
|
+
require "foreman/vendor/thor/lib/thor"
|
2
|
+
require "foreman/vendor/thor/lib/thor/group"
|
3
|
+
require "foreman/vendor/thor/lib/thor/core_ext/io_binary_read"
|
4
|
+
|
5
|
+
require "fileutils"
|
6
|
+
require "open-uri"
|
7
|
+
require "yaml"
|
8
|
+
require "digest/md5"
|
9
|
+
require "pathname"
|
10
|
+
|
11
|
+
class Foreman::Thor::Runner < Foreman::Thor #:nodoc: # rubocop:disable ClassLength
|
12
|
+
map "-T" => :list, "-i" => :install, "-u" => :update, "-v" => :version
|
13
|
+
|
14
|
+
def self.banner(command, all = false, subcommand = false)
|
15
|
+
"thor " + command.formatted_usage(self, all, subcommand)
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.exit_on_failure?
|
19
|
+
true
|
20
|
+
end
|
21
|
+
|
22
|
+
# Override Foreman::Thor#help so it can give information about any class and any method.
|
23
|
+
#
|
24
|
+
def help(meth = nil)
|
25
|
+
if meth && !respond_to?(meth)
|
26
|
+
initialize_thorfiles(meth)
|
27
|
+
klass, command = Foreman::Thor::Util.find_class_and_command_by_namespace(meth)
|
28
|
+
self.class.handle_no_command_error(command, false) if klass.nil?
|
29
|
+
klass.start(["-h", command].compact, :shell => shell)
|
30
|
+
else
|
31
|
+
super
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# If a command is not found on Foreman::Thor::Runner, method missing is invoked and
|
36
|
+
# Foreman::Thor::Runner is then responsible for finding the command in all classes.
|
37
|
+
#
|
38
|
+
def method_missing(meth, *args)
|
39
|
+
meth = meth.to_s
|
40
|
+
initialize_thorfiles(meth)
|
41
|
+
klass, command = Foreman::Thor::Util.find_class_and_command_by_namespace(meth)
|
42
|
+
self.class.handle_no_command_error(command, false) if klass.nil?
|
43
|
+
args.unshift(command) if command
|
44
|
+
klass.start(args, :shell => shell)
|
45
|
+
end
|
46
|
+
|
47
|
+
desc "install NAME", "Install an optionally named Foreman::Thor file into your system commands"
|
48
|
+
method_options :as => :string, :relative => :boolean, :force => :boolean
|
49
|
+
def install(name) # rubocop:disable MethodLength
|
50
|
+
initialize_thorfiles
|
51
|
+
|
52
|
+
# If a directory name is provided as the argument, look for a 'main.thor'
|
53
|
+
# command in said directory.
|
54
|
+
begin
|
55
|
+
if File.directory?(File.expand_path(name))
|
56
|
+
base = File.join(name, "main.thor")
|
57
|
+
package = :directory
|
58
|
+
contents = open(base, &:read)
|
59
|
+
else
|
60
|
+
base = name
|
61
|
+
package = :file
|
62
|
+
contents = open(name, &:read)
|
63
|
+
end
|
64
|
+
rescue OpenURI::HTTPError
|
65
|
+
raise Error, "Error opening URI '#{name}'"
|
66
|
+
rescue Errno::ENOENT
|
67
|
+
raise Error, "Error opening file '#{name}'"
|
68
|
+
end
|
69
|
+
|
70
|
+
say "Your Foreman::Thorfile contains:"
|
71
|
+
say contents
|
72
|
+
|
73
|
+
unless options["force"]
|
74
|
+
return false if no?("Do you wish to continue [y/N]?")
|
75
|
+
end
|
76
|
+
|
77
|
+
as = options["as"] || begin
|
78
|
+
first_line = contents.split("\n")[0]
|
79
|
+
(match = first_line.match(/\s*#\s*module:\s*([^\n]*)/)) ? match[1].strip : nil
|
80
|
+
end
|
81
|
+
|
82
|
+
unless as
|
83
|
+
basename = File.basename(name)
|
84
|
+
as = ask("Please specify a name for #{name} in the system repository [#{basename}]:")
|
85
|
+
as = basename if as.empty?
|
86
|
+
end
|
87
|
+
|
88
|
+
location = if options[:relative] || name =~ %r{^https?://}
|
89
|
+
name
|
90
|
+
else
|
91
|
+
File.expand_path(name)
|
92
|
+
end
|
93
|
+
|
94
|
+
thor_yaml[as] = {
|
95
|
+
:filename => Digest::MD5.hexdigest(name + as),
|
96
|
+
:location => location,
|
97
|
+
:namespaces => Foreman::Thor::Util.namespaces_in_content(contents, base)
|
98
|
+
}
|
99
|
+
|
100
|
+
save_yaml(thor_yaml)
|
101
|
+
say "Storing thor file in your system repository"
|
102
|
+
destination = File.join(thor_root, thor_yaml[as][:filename])
|
103
|
+
|
104
|
+
if package == :file
|
105
|
+
File.open(destination, "w") { |f| f.puts contents }
|
106
|
+
else
|
107
|
+
FileUtils.cp_r(name, destination)
|
108
|
+
end
|
109
|
+
|
110
|
+
thor_yaml[as][:filename] # Indicate success
|
111
|
+
end
|
112
|
+
|
113
|
+
desc "version", "Show Foreman::Thor version"
|
114
|
+
def version
|
115
|
+
require "foreman/vendor/thor/lib/thor/version"
|
116
|
+
say "Foreman::Thor #{Foreman::Thor::VERSION}"
|
117
|
+
end
|
118
|
+
|
119
|
+
desc "uninstall NAME", "Uninstall a named Foreman::Thor module"
|
120
|
+
def uninstall(name)
|
121
|
+
raise Error, "Can't find module '#{name}'" unless thor_yaml[name]
|
122
|
+
say "Uninstalling #{name}."
|
123
|
+
FileUtils.rm_rf(File.join(thor_root, (thor_yaml[name][:filename]).to_s))
|
124
|
+
|
125
|
+
thor_yaml.delete(name)
|
126
|
+
save_yaml(thor_yaml)
|
127
|
+
|
128
|
+
puts "Done."
|
129
|
+
end
|
130
|
+
|
131
|
+
desc "update NAME", "Update a Foreman::Thor file from its original location"
|
132
|
+
def update(name)
|
133
|
+
raise Error, "Can't find module '#{name}'" if !thor_yaml[name] || !thor_yaml[name][:location]
|
134
|
+
|
135
|
+
say "Updating '#{name}' from #{thor_yaml[name][:location]}"
|
136
|
+
|
137
|
+
old_filename = thor_yaml[name][:filename]
|
138
|
+
self.options = options.merge("as" => name)
|
139
|
+
|
140
|
+
if File.directory? File.expand_path(name)
|
141
|
+
FileUtils.rm_rf(File.join(thor_root, old_filename))
|
142
|
+
|
143
|
+
thor_yaml.delete(old_filename)
|
144
|
+
save_yaml(thor_yaml)
|
145
|
+
|
146
|
+
filename = install(name)
|
147
|
+
else
|
148
|
+
filename = install(thor_yaml[name][:location])
|
149
|
+
end
|
150
|
+
|
151
|
+
File.delete(File.join(thor_root, old_filename)) unless filename == old_filename
|
152
|
+
end
|
153
|
+
|
154
|
+
desc "installed", "List the installed Foreman::Thor modules and commands"
|
155
|
+
method_options :internal => :boolean
|
156
|
+
def installed
|
157
|
+
initialize_thorfiles(nil, true)
|
158
|
+
display_klasses(true, options["internal"])
|
159
|
+
end
|
160
|
+
|
161
|
+
desc "list [SEARCH]", "List the available thor commands (--substring means .*SEARCH)"
|
162
|
+
method_options :substring => :boolean, :group => :string, :all => :boolean, :debug => :boolean
|
163
|
+
def list(search = "")
|
164
|
+
initialize_thorfiles
|
165
|
+
|
166
|
+
search = ".*#{search}" if options["substring"]
|
167
|
+
search = /^#{search}.*/i
|
168
|
+
group = options[:group] || "standard"
|
169
|
+
|
170
|
+
klasses = Foreman::Thor::Base.subclasses.select do |k|
|
171
|
+
(options[:all] || k.group == group) && k.namespace =~ search
|
172
|
+
end
|
173
|
+
|
174
|
+
display_klasses(false, false, klasses)
|
175
|
+
end
|
176
|
+
|
177
|
+
private
|
178
|
+
|
179
|
+
def thor_root
|
180
|
+
Foreman::Thor::Util.thor_root
|
181
|
+
end
|
182
|
+
|
183
|
+
def thor_yaml
|
184
|
+
@thor_yaml ||= begin
|
185
|
+
yaml_file = File.join(thor_root, "thor.yml")
|
186
|
+
yaml = YAML.load_file(yaml_file) if File.exist?(yaml_file)
|
187
|
+
yaml || {}
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
# Save the yaml file. If none exists in thor root, creates one.
|
192
|
+
#
|
193
|
+
def save_yaml(yaml)
|
194
|
+
yaml_file = File.join(thor_root, "thor.yml")
|
195
|
+
|
196
|
+
unless File.exist?(yaml_file)
|
197
|
+
FileUtils.mkdir_p(thor_root)
|
198
|
+
yaml_file = File.join(thor_root, "thor.yml")
|
199
|
+
FileUtils.touch(yaml_file)
|
200
|
+
end
|
201
|
+
|
202
|
+
File.open(yaml_file, "w") { |f| f.puts yaml.to_yaml }
|
203
|
+
end
|
204
|
+
|
205
|
+
# Load the Foreman::Thorfiles. If relevant_to is supplied, looks for specific files
|
206
|
+
# in the thor_root instead of loading them all.
|
207
|
+
#
|
208
|
+
# By default, it also traverses the current path until find Foreman::Thor files, as
|
209
|
+
# described in thorfiles. This look up can be skipped by supplying
|
210
|
+
# skip_lookup true.
|
211
|
+
#
|
212
|
+
def initialize_thorfiles(relevant_to = nil, skip_lookup = false)
|
213
|
+
thorfiles(relevant_to, skip_lookup).each do |f|
|
214
|
+
Foreman::Thor::Util.load_thorfile(f, nil, options[:debug]) unless Foreman::Thor::Base.subclass_files.keys.include?(File.expand_path(f))
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
# Finds Foreman::Thorfiles by traversing from your current directory down to the root
|
219
|
+
# directory of your system. If at any time we find a Foreman::Thor file, we stop.
|
220
|
+
#
|
221
|
+
# We also ensure that system-wide Foreman::Thorfiles are loaded first, so local
|
222
|
+
# Foreman::Thorfiles can override them.
|
223
|
+
#
|
224
|
+
# ==== Example
|
225
|
+
#
|
226
|
+
# If we start at /Users/wycats/dev/thor ...
|
227
|
+
#
|
228
|
+
# 1. /Users/wycats/dev/thor
|
229
|
+
# 2. /Users/wycats/dev
|
230
|
+
# 3. /Users/wycats <-- we find a Foreman::Thorfile here, so we stop
|
231
|
+
#
|
232
|
+
# Suppose we start at c:\Documents and Settings\james\dev\thor ...
|
233
|
+
#
|
234
|
+
# 1. c:\Documents and Settings\james\dev\thor
|
235
|
+
# 2. c:\Documents and Settings\james\dev
|
236
|
+
# 3. c:\Documents and Settings\james
|
237
|
+
# 4. c:\Documents and Settings
|
238
|
+
# 5. c:\ <-- no Foreman::Thorfiles found!
|
239
|
+
#
|
240
|
+
def thorfiles(relevant_to = nil, skip_lookup = false)
|
241
|
+
thorfiles = []
|
242
|
+
|
243
|
+
unless skip_lookup
|
244
|
+
Pathname.pwd.ascend do |path|
|
245
|
+
thorfiles = Foreman::Thor::Util.globs_for(path).map { |g| Dir[g] }.flatten
|
246
|
+
break unless thorfiles.empty?
|
247
|
+
end
|
248
|
+
end
|
249
|
+
|
250
|
+
files = (relevant_to ? thorfiles_relevant_to(relevant_to) : Foreman::Thor::Util.thor_root_glob)
|
251
|
+
files += thorfiles
|
252
|
+
files -= ["#{thor_root}/thor.yml"]
|
253
|
+
|
254
|
+
files.map! do |file|
|
255
|
+
File.directory?(file) ? File.join(file, "main.thor") : file
|
256
|
+
end
|
257
|
+
end
|
258
|
+
|
259
|
+
# Load Foreman::Thorfiles relevant to the given method. If you provide "foo:bar" it
|
260
|
+
# will load all thor files in the thor.yaml that has "foo" e "foo:bar"
|
261
|
+
# namespaces registered.
|
262
|
+
#
|
263
|
+
def thorfiles_relevant_to(meth)
|
264
|
+
lookup = [meth, meth.split(":")[0...-1].join(":")]
|
265
|
+
|
266
|
+
files = thor_yaml.select do |_, v|
|
267
|
+
v[:namespaces] && !(v[:namespaces] & lookup).empty?
|
268
|
+
end
|
269
|
+
|
270
|
+
files.map { |_, v| File.join(thor_root, (v[:filename]).to_s) }
|
271
|
+
end
|
272
|
+
|
273
|
+
# Display information about the given klasses. If with_module is given,
|
274
|
+
# it shows a table with information extracted from the yaml file.
|
275
|
+
#
|
276
|
+
def display_klasses(with_modules = false, show_internal = false, klasses = Foreman::Thor::Base.subclasses)
|
277
|
+
klasses -= [Foreman::Thor, Foreman::Thor::Runner, Foreman::Thor::Group] unless show_internal
|
278
|
+
|
279
|
+
raise Error, "No Foreman::Thor commands available" if klasses.empty?
|
280
|
+
show_modules if with_modules && !thor_yaml.empty?
|
281
|
+
|
282
|
+
list = Hash.new { |h, k| h[k] = [] }
|
283
|
+
groups = klasses.select { |k| k.ancestors.include?(Foreman::Thor::Group) }
|
284
|
+
|
285
|
+
# Get classes which inherit from Foreman::Thor
|
286
|
+
(klasses - groups).each { |k| list[k.namespace.split(":").first] += k.printable_commands(false) }
|
287
|
+
|
288
|
+
# Get classes which inherit from Foreman::Thor::Base
|
289
|
+
groups.map! { |k| k.printable_commands(false).first }
|
290
|
+
list["root"] = groups
|
291
|
+
|
292
|
+
# Order namespaces with default coming first
|
293
|
+
list = list.sort { |a, b| a[0].sub(/^default/, "") <=> b[0].sub(/^default/, "") }
|
294
|
+
list.each { |n, commands| display_commands(n, commands) unless commands.empty? }
|
295
|
+
end
|
296
|
+
|
297
|
+
def display_commands(namespace, list) #:nodoc:
|
298
|
+
list.sort! { |a, b| a[0] <=> b[0] }
|
299
|
+
|
300
|
+
say shell.set_color(namespace, :blue, true)
|
301
|
+
say "-" * namespace.size
|
302
|
+
|
303
|
+
print_table(list, :truncate => true)
|
304
|
+
say
|
305
|
+
end
|
306
|
+
alias_method :display_tasks, :display_commands
|
307
|
+
|
308
|
+
def show_modules #:nodoc:
|
309
|
+
info = []
|
310
|
+
labels = %w(Modules Namespaces)
|
311
|
+
|
312
|
+
info << labels
|
313
|
+
info << ["-" * labels[0].size, "-" * labels[1].size]
|
314
|
+
|
315
|
+
thor_yaml.each do |name, hash|
|
316
|
+
info << [name, hash[:namespaces].join(", ")]
|
317
|
+
end
|
318
|
+
|
319
|
+
print_table info
|
320
|
+
say ""
|
321
|
+
end
|
322
|
+
end
|