Class: Brick::CLI
Defined Under Namespace
Classes: Build, Help, ProjectNew, Run, ServiceNew, SubcommandLoader, Up
Class Method Summary
collapse
Instance Method Summary
collapse
convert_to_class_name, convert_to_snake_case, filename_to_qualified_string, snake_case_basename
#parse_options
Constructor Details
#initialize(argv = []) ⇒ CLI
Returns a new instance of CLI.
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
# File 'lib/brick/cli.rb', line 161
def initialize(argv=[])
super()
command_name_words = self.class.snake_case_name.split('_')
@cmd_args = ARGV.dup - command_name_words
@cmd_args = parse_options @cmd_args
@cmd_args.delete(command_name_words.join('-'))
Brick::Config.merge!(config)
project_name = ::Brick::Config[:project]
config_file = ::Brick::Config[:config_file]
project_dir = File.dirname(config_file)
::Brick::Config[:project_dir] = project_dir
=begin
if config[:help]
logger.info opt_parser
exit 1
end
=end
end
|
Class Method Details
.category(new_category) ⇒ Object
139
140
141
|
# File 'lib/brick/cli.rb', line 139
def self.category(new_category)
@category = new_category
end
|
.common_optparser ⇒ Object
53
54
55
|
# File 'lib/brick/cli.rb', line 53
def self.common_optparser
@@common_optparser ||= Application.new
end
|
.dependency_loaders ⇒ Object
147
148
149
|
# File 'lib/brick/cli.rb', line 147
def self.dependency_loaders
@dependency_loaders ||= []
end
|
.deps(&block) ⇒ Object
151
152
153
|
# File 'lib/brick/cli.rb', line 151
def self.deps(&block)
dependency_loaders << block
end
|
.inherited(subclass) ⇒ Object
22
23
24
25
26
|
# File 'lib/brick/cli.rb', line 22
def self.inherited(subclass)
unless subclass.unnamed?
subcommands[subclass.snake_case_name] = subclass
end
end
|
.list_commands(preferred_category = nil) ⇒ Object
is given, only subcommands in that category are shown
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/brick/cli.rb', line 93
def self.list_commands(preferred_category=nil)
load_commands
category_desc = preferred_category ? preferred_category + " " : ''
logger.info "Available #{category_desc}subcommands: (for details, brick SUB-COMMAND --help)\n\n"
if preferred_category && subcommands_by_category.key?(preferred_category)
commands_to_show = {preferred_category => subcommands_by_category[preferred_category]}
else
commands_to_show = subcommands_by_category
end
commands_to_show.sort.each do |category, commands|
next if category =~ /deprecated/i
logger.info "** #{category.upcase} COMMANDS **"
commands.sort.each do |command|
logger.info subcommands[command].banner if subcommands[command]
end
logger.info
end
end
|
.list_parameters ⇒ Object
87
88
89
90
|
# File 'lib/brick/cli.rb', line 87
def self.list_parameters
puts common_optparser.opt_parser.to_s
puts ""
end
|
.load_commands ⇒ Object
129
130
131
|
# File 'lib/brick/cli.rb', line 129
def self.load_commands
@commands_loaded ||= subcommand_loader.load_commands
end
|
.load_deps ⇒ Object
155
156
157
158
159
|
# File 'lib/brick/cli.rb', line 155
def self.load_deps
dependency_loaders.each do |dep_loader|
dep_loader.call
end
end
|
.logger ⇒ Object
12
13
14
15
16
17
18
19
|
# File 'lib/brick/cli.rb', line 12
def self.logger
@@logger ||= Logger.new(STDOUT)
@@logger.level = Logger::INFO
@@logger.formatter = proc do |severity, datetime, progname, msg|
"#{msg}\n"
end
@@logger
end
|
.run(args, options = {}) ⇒ Object
CLI options that the subcommand knows how to handle.
Arguments
- args:
-
usually ARGV
- options:
-
A Mixlib::CLI option parser hash. These options are how
subcommands know about global knife CLI options
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/brick/cli.rb', line 38
def self.run(args, options={})
common_optparser.opt_parser.banner="Usage: brick SUBCOMMAND (options)"
CLI_Validator::validate
load_commands
subcommand_class = subcommand_class_from(args)
subcommand_class.options = common_optparser.class.options.merge!(subcommand_class.options)
subcommand_class.load_deps
instance = subcommand_class.new(args)
instance.run_with_pretty_exceptions
end
|
.snake_case_name ⇒ Object
125
126
127
|
# File 'lib/brick/cli.rb', line 125
def self.snake_case_name
convert_to_snake_case(name.split('::').last) unless unnamed?
end
|
.subcommand_category ⇒ Object
143
144
145
|
# File 'lib/brick/cli.rb', line 143
def self.subcommand_category
@category || snake_case_name.split('_').first unless unnamed?
end
|
.subcommand_class_from(args) ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/brick/cli.rb', line 57
def self.subcommand_class_from(args)
command_words = args.select {|arg| arg =~ /^(([[:alnum:]])[[:alnum:]\_\-]+)$/ }
subcommand_class = nil
while ( !subcommand_class ) && ( !command_words.empty? )
snake_case_class_name = command_words.join("_")
unless subcommand_class = subcommands[snake_case_class_name]
command_words.pop
end
end
subcommand_class ||= subcommands[args.first.gsub('-', '_')]
subcommand_class || subcommand_not_found!(args)
end
|
.subcommand_loader ⇒ Object
134
135
136
|
# File 'lib/brick/cli.rb', line 134
def self.subcommand_loader
@subcommand_loader ||= CLI::SubcommandLoader.new
end
|
.subcommand_not_found!(args) ⇒ Object
77
78
79
80
81
82
83
84
85
|
# File 'lib/brick/cli.rb', line 77
def self.subcommand_not_found!(args)
logger.fatal("Cannot find sub command for: '#{args.join(' ')}'")
list_commands
exit 10
end
|
.subcommands ⇒ Object
73
74
75
|
# File 'lib/brick/cli.rb', line 73
def self.subcommands
@@subcommands ||= {}
end
|
.subcommands_by_category ⇒ Object
115
116
117
118
119
120
121
122
123
|
# File 'lib/brick/cli.rb', line 115
def self.subcommands_by_category
unless @subcommands_by_category
@subcommands_by_category = Hash.new { |hash, key| hash[key] = [] }
subcommands.each do |snake_cased, klass|
@subcommands_by_category[klass.subcommand_category] << snake_cased
end
end
@subcommands_by_category
end
|
.unnamed? ⇒ Boolean
28
29
30
|
# File 'lib/brick/cli.rb', line 28
def self.unnamed?
name.nil? || name.empty?
end
|
Instance Method Details
#humanize_exception(e) ⇒ Object
204
205
206
207
208
209
210
211
212
213
214
|
# File 'lib/brick/cli.rb', line 204
def humanize_exception(e)
case e
when SystemExit
raise when Errno::ECONNREFUSED, Timeout::Error, Errno::ETIMEDOUT, SocketError
logger.error "Network Error: #{e.message}"
logger.info "Check your network settings"
else
logger.error "#{e.class.name}: #{e.message}"
end
end
|
#run_with_pretty_exceptions ⇒ Object
193
194
195
196
197
198
199
200
201
202
|
# File 'lib/brick/cli.rb', line 193
def run_with_pretty_exceptions
unless self.respond_to?(:run)
logger.error "You need to add a #run method to your brick command before you can use it"
end
run
rescue Exception => e
raise
humanize_exception(e)
exit 100
end
|
#show_usage ⇒ Object
216
217
218
|
# File 'lib/brick/cli.rb', line 216
def show_usage
puts( self.opt_parser.to_s)
end
|