Module: DLDInternet::Thor::MixIns::NoCommands
- Includes:
- Mixlib::Logging
- Defined in:
- lib/dldinternet/thor/mixins/no_commands.rb
Instance Method Summary collapse
- #abort!(msg) ⇒ Object
-
#check_for_help(args) ⇒ Object
Child classes can override this if desired.
- #command_out(res, fmtr = nil) ⇒ Object
- #command_post(rc = 0) ⇒ Object
- #command_pre(*args) ⇒ Object
- #config_to_yaml ⇒ Object
- #default_format(obj, opts = nil, format_helper = nil) ⇒ Object
- #default_formatter(obj, opts = nil) ⇒ Object
- #default_header(obj, format_helper = nil) ⇒ Object
- #format_line(obj, fmtr = nil) ⇒ Object
- #get_logger(force = false) ⇒ Object
- #handle_no_command_error(command, has_namespace = $thor_runner) ⇒ Object
- #hash_it(robj) ⇒ Object
- #header_line(obj, fmtr = nil) ⇒ Object
-
#invoke_command(command, *args) ⇒ Object
:nodoc:.
- #load_config ⇒ Object
- #load_inifile ⇒ Object
- #notation ⇒ Object
- #output(obj, fmtr = nil, header = false) ⇒ Object
- #parse_options(*args) ⇒ Object
- #solve_pointers(haystack, hash = nil) ⇒ Object
- #string_it(obj, fmtr = nil, header = false) ⇒ Object
- #validate_options(*args) ⇒ Object
- #write(obj) ⇒ Object
- #writeable_options ⇒ Object
Instance Method Details
#abort!(msg) ⇒ Object
273 274 275 276 |
# File 'lib/dldinternet/thor/mixins/no_commands.rb', line 273 def abort!(msg) @logger.error msg exit -1 end |
#check_for_help(args) ⇒ Object
Child classes can override this if desired
229 230 231 232 233 234 235 |
# File 'lib/dldinternet/thor/mixins/no_commands.rb', line 229 def check_for_help(args) if args && args.size > 0 && (args[0] && args[0].downcase.eql?('help') || args.select {|a| a.match(/--help/i)}.any?) invocations = @_invocations.map {|_, v| v[0]} self.class.command_help(shell, invocations[-1], invocations) exit 0 end end |
#command_out(res, fmtr = nil) ⇒ Object
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 |
# File 'lib/dldinternet/thor/mixins/no_commands.rb', line 366 def command_out(res, fmtr=nil) unless fmtr fmtr = formatter.call(res, ) fmtr.table_widths end # [2017-10-06 Christo] header_line and format_line serves to invoke client hooks if provided. # It means we may go through the process once with an object and a second time with a formatted string case [:format] when /text|none|plain/ output(header_line(res, fmtr), fmtr, true) unless [:header] === false case res.class.name when /Array/ fmtr.object.each do |obj| output format_line(obj, fmtr) end # when /Hash|String/ else output format_line(fmtr.object || res, fmtr) end else output fmtr.object || res end end |
#command_post(rc = 0) ⇒ Object
360 361 362 363 364 |
# File 'lib/dldinternet/thor/mixins/no_commands.rb', line 360 def command_post(rc=0) command_post_stop_vcr @command_post = true rc end |
#command_pre(*args) ⇒ Object
353 354 355 356 357 358 |
# File 'lib/dldinternet/thor/mixins/no_commands.rb', line 353 def command_pre(*args) # args.flatten! (args) @logger.info @_invocations.map{ |_,v| v[0]}.join(' ') if [:verbose] command_pre_start_vcr(args) end |
#config_to_yaml ⇒ Object
193 194 195 196 197 198 199 200 201 |
# File 'lib/dldinternet/thor/mixins/no_commands.rb', line 193 def config_to_yaml begin yaml = ConfigMash.new(::YAML.load(File.read([:configfile]))) yaml = solve_pointers(yaml) yaml rescue StandardError => e raise e end end |
#default_format(obj, opts = nil, format_helper = nil) ⇒ Object
308 309 310 311 |
# File 'lib/dldinternet/thor/mixins/no_commands.rb', line 308 def default_format(obj, opts=nil, format_helper=nil) format_helper ||= default_formatter(obj, opts || ) format_helper.format_it end |
#default_formatter(obj, opts = nil) ⇒ Object
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/dldinternet/thor/mixins/no_commands.rb', line 282 def default_formatter(obj, opts=nil) opts ||= Hashie::Mash.new(.to_h) format_helper = DLDInternet::Formatters::Basic.new(obj, notation, title: opts[:title], columns: opts[:columns]) case notation.to_sym when :json when :yaml when :csv when :none # when :basic when :text # noop when :awesome format_helper = DLDInternet::Formatters::Awesome.new(obj, notation, title: opts[:title], columns: opts[:columns]) when :table format_helper = DLDInternet::Formatters::Table.new(obj, notation, title: opts[:title], columns: opts[:columns]) else raise DLDInternet::Formatters::Error, "Unknown format requested: #{notation}" end format_helper end |
#default_header(obj, format_helper = nil) ⇒ Object
303 304 305 306 |
# File 'lib/dldinternet/thor/mixins/no_commands.rb', line 303 def default_header(obj, format_helper=nil) format_helper ||= default_formatter(obj) format_helper.header_it end |
#format_line(obj, fmtr = nil) ⇒ Object
406 407 408 |
# File 'lib/dldinternet/thor/mixins/no_commands.rb', line 406 def format_line(obj, fmtr=nil) @format.call(obj, fmtr) end |
#get_logger(force = false) ⇒ Object
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/dldinternet/thor/mixins/no_commands.rb', line 237 def get_logger(force=false) return unless force || @logger.nil? [:log_level] ||= :warn lcs = ::Logging::ColorScheme.new('compiler', :levels => { :trace => :blue, :debug => :cyan, :info => :green, :note => :green, :warn => :yellow, :error => :red, :fatal => :red, :todo => :purple, }) scheme = lcs.scheme scheme['trace'] = "\e[38;5;33m" scheme['fatal'] = "\e[38;5;89m" scheme['todo'] = "\e[38;5;55m" lcs.scheme scheme @config = .dup @config[:log_opts] = lambda {|mlll| { :pattern => "%#{mlll}l: %m %g\n", :date_pattern => '%Y-%m-%d %H:%M:%S', :color_scheme => 'compiler', :trace => (@config[:trace].nil? ? false : @config[:trace]), # [2014-06-30 Christo] DO NOT do this ... it needs to be a FixNum!!!! # If you want to do ::Logging.init first then fine ... go ahead :) # :level => @config[:log_level], } } @config[:log_levels] ||= LOG_LEVELS [:log_config] = @config # initLogging(@config) @logger = getLogger(@config) end |
#handle_no_command_error(command, has_namespace = $thor_runner) ⇒ Object
416 417 418 419 420 421 |
# File 'lib/dldinternet/thor/mixins/no_commands.rb', line 416 def handle_no_command_error(command, has_namespace = $thor_runner) get_logger unless @logger#:nodoc: @logger.error "Could not find command #{command.inspect} in #{namespace.inspect} namespace." if has_namespace @logger.error "Could not find command #{command.inspect}." 1 end |
#hash_it(robj) ⇒ Object
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 |
# File 'lib/dldinternet/thor/mixins/no_commands.rb', line 313 def hash_it(robj) klass = robj.class repre = begin klass.const_get('Representation') rescue false end if robj.nil? robj elsif robj.respond_to?(:to_hash) robj.to_hash elsif robj.respond_to?(:to_a) robj.to_a.map { |obj| hash_it(obj) } elsif repre representation = repre.new(robj) representation.to_hash elsif robj.respond_to?(:to_h) robj.to_h else robj end end |
#header_line(obj, fmtr = nil) ⇒ Object
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 |
# File 'lib/dldinternet/thor/mixins/no_commands.rb', line 390 def header_line(obj, fmtr=nil) if obj.is_a?(String) obj elsif obj.is_a?(Hash) @header.call(obj, fmtr) elsif obj.is_a?(Array) if obj.size > 0 header_line(obj[0], fmtr) else header_line({}, fmtr) end else raise "Cannot produce header from this object: #{obj.class.name}" end end |
#invoke_command(command, *args) ⇒ Object
:nodoc:
410 411 412 413 414 |
# File 'lib/dldinternet/thor/mixins/no_commands.rb', line 410 def invoke_command(command, *args) #:nodoc: ::DLDInternet::Thor::Command.invocations = @_invocations.dup.map{ |_,v| v[0]} = command. super end |
#load_config ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/dldinternet/thor/mixins/no_commands.rb', line 126 def load_config if [:configfile].blank? @logger.error 'Invalid/No configuration file specified' else [:configfile] = File.([:configfile]) if ::File.exist?([:configfile]) begin # envs = ::Config::Factory::Environments.load_file(@options[:configfile]) hash = config_to_yaml envs = ::Config::Factory::Environments.load_hash(hash) if envs and envs.is_a?(Hash) and [:environment] [:environments] = ::Hashie::Mash.new(envs) else yaml = ::YAML.load(File.read([:configfile])) if yaml yaml.each {|key, value| [key.to_s.gsub(%r{[-]}, '_').to_sym]=value } else msg = "#{options[:configfile]} is not a valid configuration!" @logger.error msg raise StandardError.new(msg) end end rescue ::Exception => e @logger.error "#{e.class.name} #{e.message}" raise e end else @logger.warn "'#{options[:configfile]}' not found" @logger.error "Invalid/No configuration file specified" exit 2 #@options[:environments] = ::Hashie::Mash.new end end end |
#load_inifile ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/dldinternet/thor/mixins/no_commands.rb', line 83 def load_inifile unless ![:inifile].blank? && File.exist?([:inifile]) raise "#{@options[:inifile]} not found!" end begin ini = ::IniFile.load([:inifile]) ini['global'].each{ |key,value| [key.to_s]=value ENV[key.to_s]=value } def (k,v,regex,rerun) matches = v.match(regex) if matches var = matches[1] if [var] [k]=v.gsub(/\%\(#{var}\)/,[var]).gsub(/\%#{var}/,[var]) else rerun[var] = 1 end end end pending = nil rerun = {} begin pending = rerun rerun = {} .to_hash.each{|k,v| if v.to_s.match(/\%/) (k,v,%r'[^\\]\%\((\w+)\)', rerun) (k,v,%r'[^\\]\%(\w+)', rerun) end } # Should break out the first time that we make no progress! end while pending != rerun rescue ::IniFile::Error => e # noop rescue ::Exception => e @logger.error "#{e.class.name} #{e.message}" raise e end end |
#notation ⇒ Object
278 279 280 |
# File 'lib/dldinternet/thor/mixins/no_commands.rb', line 278 def notation @config[:format] || :none end |
#output(obj, fmtr = nil, header = false) ⇒ Object
345 346 347 348 349 350 351 |
# File 'lib/dldinternet/thor/mixins/no_commands.rb', line 345 def output(obj, fmtr=nil, header=false) unless obj.nil? it = obj.is_a?(Array) ? obj.map { |o| hash_it(o) } : (obj.is_a?(String) ? obj : hash_it(obj)) str = string_it(it, fmtr, header) write str end end |
#parse_options(*args) ⇒ Object
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/dldinternet/thor/mixins/no_commands.rb', line 203 def (*args) get_logger(true) args.flatten! check_for_help(args) (args) if [:inifile] [:inifile] = File.([:inifile]) load_inifile elsif [:configfile] [:configfile] = File.([:configfile]) if [:configfile] =~ /\.ini/i [:inifile] = [:configfile] load_inifile else load_config end end if [:debug] @logger.info "Options:\n#{options.ai}" end end |
#solve_pointers(haystack, hash = nil) ⇒ Object
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 191 |
# File 'lib/dldinternet/thor/mixins/no_commands.rb', line 163 def solve_pointers(haystack, hash=nil) hash ||= haystack return haystack unless hash.is_a?(Hash) && hash.size hash.dup.each do |k,v| if %r{^[#/]}.match?(k) && v.nil? begin require 'hana' pointer = Hana::Pointer.new k v = pointer.eval(haystack) hash.delete(k) hash.merge!(v) rescue Exception => e require 'json-pointer' pointer = ::JsonPointer.new(haystack, k, :symbolize_keys => true) if pointer.exists? hash.delete(k) hash.merge!(pointer.value) end end end end hash.each do |k,v| if v && v.is_a?(Hash) haystack = solve_pointers(haystack, v) end end haystack end |
#string_it(obj, fmtr = nil, header = false) ⇒ Object
336 337 338 339 |
# File 'lib/dldinternet/thor/mixins/no_commands.rb', line 336 def string_it(obj, fmtr=nil, header=false) fmtr ||= @formatter.call(obj, ) fmtr.send(header ? :header_it : :format_it, obj) end |
#validate_options(*args) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/dldinternet/thor/mixins/no_commands.rb', line 62 def (*args) if [:log_level] log_level = [:log_level].to_sym raise "Invalid log-level: #{log_level}" unless LOG_LEVELS.include?(log_level) [:log_level] = log_level end [:format] ||= [:output] [:output] ||= [:format] args.flatten! if !args.empty? && args.map{ |a| /^-/.match(a) }.any? raise DLDInternet::Thor::BadArgumentError, "Invalid arguments provided: #{args}" end end |
#write(obj) ⇒ Object
341 342 343 |
# File 'lib/dldinternet/thor/mixins/no_commands.rb', line 341 def write(obj) writer.call(obj) end |
#writeable_options ⇒ Object
78 79 80 81 |
# File 'lib/dldinternet/thor/mixins/no_commands.rb', line 78 def return if .is_a?(OptionsMash) = OptionsMash.new(.to_h) end |