Class: Zillabyte::Command::Base

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/zillabyte/cli/base.rb

Direct Known Subclasses

Auth, Data, Download, Flows, Help, Nuke, Repl, Version

Constant Summary collapse

META_COLUMNS =
["since", "confidence", "source"]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#app, #ask, #command, #create_git_remote, #display, #error, #extract_app_from_git_config, #extract_app_in_dir, #format_with_bang, #friendly_dir, #get_flow_ui_link, #get_info, #get_rich_info, #git, #handle_downloading_manifest, #has_git?, #longest, #read_multiline, #truncate_message, #version_okay?, #with_tty

Constructor Details

#initialize(args = [], options = {}) ⇒ Base

Returns a new instance of Base.



15
16
17
18
# File 'lib/zillabyte/cli/base.rb', line 15

def initialize(args=[], options={})
  @args = args
  @options = options
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



12
13
14
# File 'lib/zillabyte/cli/base.rb', line 12

def args
  @args
end

#optionsObject (readonly)

Returns the value of attribute options.



13
14
15
# File 'lib/zillabyte/cli/base.rb', line 13

def options
  @options
end

Class Method Details

.alias_command(new, old) ⇒ Object



133
134
135
136
# File 'lib/zillabyte/cli/base.rb', line 133

def self.alias_command(new, old)
  raise "no such command: #{old}" unless Zillabyte::Command.commands[old]
  Zillabyte::Command.command_aliases[new] = old
end

.extract_banner(help) ⇒ Object



107
108
109
# File 'lib/zillabyte/cli/base.rb', line 107

def self.extract_banner(help)
  help.first
end

.extract_description(help) ⇒ Object



115
116
117
118
119
# File 'lib/zillabyte/cli/base.rb', line 115

def self.extract_description(help)
  help.reject do |line|
    line =~ /^\s+-(.+)#(.+)/
  end.join("\n")
end

.extract_help(file, line_number) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/zillabyte/cli/base.rb', line 88

def self.extract_help(file, line_number)
  buffer = []
  lines = Zillabyte::Command.files[file]

  (line_number.to_i-2).downto(0) do |i|
    line = lines[i]
    case line[0..0]
      when ""
      when "#"
        buffer.unshift(line[1..-1])
      else
        break
    end
  end

  buffer
end

.extract_help_from_caller(line) ⇒ Object

Parse the caller format and identify the file and line number as identified in : www.ruby-doc.org/core/classes/Kernel.html#M001397. This will look for a colon followed by a digit as the delimiter. The biggest complication is windows paths, which have a colon after the drive letter. This regex will match paths as anything from the beginning to a colon directly followed by a number (the line number).

Examples of the caller format :

  • c:/Ruby192/lib/…/lib/zillabyte/cli/addons.rb:8:in ‘<module:Command>’

  • c:/Ruby192/lib/…/zillabyte-2.0.1/lib/zillabyte/cli/pg.rb:96:in ‘<class:Pg>’

  • /Users/ph7/.…./xray-1.1/lib/xray/thread_dump_signal_handler.rb:9



79
80
81
82
83
84
85
86
# File 'lib/zillabyte/cli/base.rb', line 79

def self.extract_help_from_caller(line)
  # pull out of the caller the information for the file path and line number
  if line =~ /^(.+?):(\d+)/
    extract_help($1, $2)
  else
    raise("unable to extract help from caller: #{line}")
  end
end

.extract_options(help) ⇒ Object



121
122
123
124
125
126
127
128
129
130
# File 'lib/zillabyte/cli/base.rb', line 121

def self.extract_options(help)
  r = help.select do |line|
    line =~ /^\s+-(.+)#(.+)/
  end.inject([]) do |options, line|
    args = line.split('#', 2).first
    args = args.split(/,\s*/).map {|arg| arg.strip}.sort.reverse
    name = args.last.split(' ', 2).first[2..-1]
    options << { :name => name, :args => args }
  end
end

.extract_summary(help) ⇒ Object



111
112
113
# File 'lib/zillabyte/cli/base.rb', line 111

def self.extract_summary(help)
  extract_description(help).split("\n")[2].to_s.split("\n").first
end

.inherited(klass) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/zillabyte/cli/base.rb', line 29

def self.inherited(klass)
  unless klass == Zillabyte::Command::Base
    help = extract_help_from_caller(caller.first)

    Zillabyte::Command.register_namespace(
      :name => klass.namespace,
      :description => help.first
    )
  end
end

.method_added(method) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/zillabyte/cli/base.rb', line 40

def self.method_added(method)
  return if self == Zillabyte::Command::Base
  return if private_method_defined?(method)
  return if protected_method_defined?(method)

  help = extract_help_from_caller(caller.first)
  resolved_method = (method.to_s == "index") ? nil : method.to_s
  command = [ self.namespace, resolved_method ].compact.join(":")
  banner = extract_banner(help) || command

  Zillabyte::Command.register_command(
    :klass       => self,
    :method      => method,
    :namespace   => self.namespace,
    :command     => command,
    :banner      => banner.strip,
    :help        => help.join("\n"),
    :summary     => extract_summary(help),
    :description => extract_description(help),
    :options     => extract_options(help)
  )

  alias_command command.gsub(/_/, '-'), command if command =~ /_/
end

.namespaceObject



8
9
10
# File 'lib/zillabyte/cli/base.rb', line 8

def self.namespace
  self.to_s.split("::").last.downcase
end

Instance Method Details

#apiObject



20
21
22
23
24
# File 'lib/zillabyte/cli/base.rb', line 20

def api
  require("zillabyte/api")
  require("zillabyte/auth")
  @__api ||= Zillabyte::API.new(:api_key => Zillabyte::Auth.api_key, :session => self)
end