Module: Strelka::CLI
- Extended by:
- GLI::App, Loggability, MethodUtilities
- Includes:
- Constants
- Defined in:
- lib/strelka/cli.rb
Overview
The command-line interface to Strelka.
Defined Under Namespace
Modules: Config, Discover, Start, Subcommand
Constant Summary collapse
- COLOR_SCHEME =
Make a HighLine color scheme
HighLine::ColorScheme.new do |scheme| scheme[:header] = [ :bold, :yellow ] scheme[:subheader] = [ :bold, :white ] scheme[:key] = [ :white ] scheme[:value] = [ :bold, :white ] scheme[:error] = [ :red ] scheme[:warning] = [ :yellow ] scheme[:message] = [ :reset ] end
Class Method Summary collapse
-
.add_registered_subcommands ⇒ Object
Add the commands from the registered subcommand modules.
-
.commands_from(subdir) ⇒ Object
Load commands from any files in the specified directory relative to LOAD_PATHs.
-
.install_highline_colorscheme ⇒ Object
Install the color scheme used by HighLine.
-
.load_commands(path) ⇒ Object
Custom command loader.
-
.load_config(global = {}) ⇒ Object
Load the config file using either strelka-base's config-loader if available, or fall back to DEFAULT_CONFIG_FILE.
-
.outfile ⇒ Object
If the command's output was redirected to a file, return the open File object for it.
-
.prompt ⇒ Object
Return the HighLine prompt used by the command to communicate with the user.
-
.register(&block) ⇒ Object
Register one or more subcommands with the 'strelka' command shell.
-
.register_subcommands(mod) ⇒ Object
Add the specified +mod+ule containing subcommands to the 'strelka' command.
-
.require_additional_libs(requires) ⇒ Object
Load any additional Ruby libraries given with the -r global option.
-
.reset_prompt ⇒ Object
Discard the existing HighLine prompt object if one existed.
-
.run ⇒ Object
Overridden -- Add registered subcommands immediately before running.
-
.setup_output(global) ⇒ Object
Set up the output levels and globals based on the associated
globaloptions.
Instance Method Summary collapse
-
#outfile ⇒ Object
The IO opened to the output file.
-
#subcommand_modules ⇒ Object
Registered subcommand modules.
Methods included from MethodUtilities
attr_predicate, attr_predicate_accessor, singleton_attr_accessor, singleton_attr_reader, singleton_attr_writer, singleton_method_alias, singleton_predicate_accessor, singleton_predicate_reader
Class Method Details
.add_registered_subcommands ⇒ Object
Add the commands from the registered subcommand modules.
156 157 158 159 160 161 162 163 164 |
# File 'lib/strelka/cli.rb', line 156 def self::add_registered_subcommands self.subcommand_modules ||= [] self.subcommand_modules.each do |mod| merged_commands = mod.commands.merge( self.commands ) self.commands.update( merged_commands ) command_objs = self.commands_declaration_order | self.commands.values self.commands_declaration_order.replace( command_objs ) end end |
.commands_from(subdir) ⇒ Object
Load commands from any files in the specified directory relative to LOAD_PATHs
383 384 385 386 387 388 |
# File 'lib/strelka/cli.rb', line 383 def self::commands_from( subdir ) $LOAD_PATH.map {|path| Pathname(path) }.each do |libdir| command_dir = libdir. + subdir load_commands( command_dir ) end end |
.install_highline_colorscheme ⇒ Object
Install the color scheme used by HighLine
198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/strelka/cli.rb', line 198 def self::install_highline_colorscheme HighLine.color_scheme = HighLine::ColorScheme.new do |cs| cs[:headline] = [ :bold, :white, :on_black ] cs[:success] = [ :green ] cs[:error] = [ :bold, :red ] cs[:highlight] = [ :bold, :yellow ] cs[:search_hit] = [ :black, :on_white ] cs[:prompt] = [ :cyan ] cs[:even_row] = [ :bold ] cs[:odd_row] = [ :normal ] end end |
.load_commands(path) ⇒ Object
Custom command loader. The default one is silly.
373 374 375 376 377 378 379 |
# File 'lib/strelka/cli.rb', line 373 def self::load_commands( path ) self.log.debug "Load commands from %s" % [ path ] Pathname.glob( path + '*.rb' ).each do |rbfile| self.log.debug " loading %s..." % [ rbfile ] require( rbfile ) end end |
.load_config(global = {}) ⇒ Object
Load the config file using either strelka-base's config-loader if available, or fall back to DEFAULT_CONFIG_FILE
214 215 216 217 218 219 |
# File 'lib/strelka/cli.rb', line 214 def self::load_config( global={} ) Strelka.load_config( global.config ) if global.config # Set up the logging formatter Loggability.format_with( :color ) if $stdout.tty? end |
.outfile ⇒ Object
If the command's output was redirected to a file, return the open File object for it.
176 177 178 |
# File 'lib/strelka/cli.rb', line 176 def self::outfile return @outfile end |
.prompt ⇒ Object
Return the HighLine prompt used by the command to communicate with the user.
169 170 171 |
# File 'lib/strelka/cli.rb', line 169 def self::prompt @prompt ||= HighLine.new( $stdin, $stderr ) end |
.register(&block) ⇒ Object
Register one or more subcommands with the 'strelka' command shell. The given block will be evaluated in the context of Strelka::CLI.
367 368 369 |
# File 'lib/strelka/cli.rb', line 367 def self::register( &block ) self.instance_eval( &block ) end |
.register_subcommands(mod) ⇒ Object
Add the specified +mod+ule containing subcommands to the 'strelka' command.
147 148 149 150 151 152 |
# File 'lib/strelka/cli.rb', line 147 def self::register_subcommands( mod ) self.subcommand_modules ||= [] self.subcommand_modules.push( mod ) mod.extend( GLI::App, GLI::AppSupport, Strelka::Constants, Loggability ) mod.log_to( :strelka ) end |
.require_additional_libs(requires) ⇒ Object
Load any additional Ruby libraries given with the -r global option.
189 190 191 192 193 194 |
# File 'lib/strelka/cli.rb', line 189 def self::require_additional_libs( requires) requires.each do |path| path = "strelka/#{path}" unless path.start_with?( 'strelka/' ) require( path ) end end |
.reset_prompt ⇒ Object
Discard the existing HighLine prompt object if one existed. Mostly useful for testing.
183 184 185 |
# File 'lib/strelka/cli.rb', line 183 def self::reset_prompt @prompt = nil end |
.run ⇒ Object
Overridden -- Add registered subcommands immediately before running.
140 141 142 143 |
# File 'lib/strelka/cli.rb', line 140 def self::run( * ) self.add_registered_subcommands super end |
.setup_output(global) ⇒ Object
Set up the output levels and globals based on the associated global options.
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/strelka/cli.rb', line 223 def self::setup_output( global ) # Turn on Ruby debugging and/or verbosity if specified if global[:n] $DRYRUN = true Loggability.level = :warn else $DRYRUN = false end if global[:verbose] $VERBOSE = true Loggability.level = :info end if global[:debug] $DEBUG = true Loggability.level = :debug end if (( filename = global[:o] )) if filename.to_s == '-' @prompt = HighLine.new( $stdin, $stdout ) else @outfile = filename.open( 'w', encoding: 'utf-8' ) HighLine.use_color = false @prompt = HighLine.new( $stdin, @outfile ) end end end |
Instance Method Details
#outfile ⇒ Object
The IO opened to the output file
136 |
# File 'lib/strelka/cli.rb', line 136 singleton_attr_accessor :outfile |
#subcommand_modules ⇒ Object
Registered subcommand modules
132 |
# File 'lib/strelka/cli.rb', line 132 singleton_attr_accessor :subcommand_modules |