Class: Bosh::Cli::Runner

Inherits:
Object show all
Defined in:
lib/cli/runner.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args, options = {}) ⇒ Runner

Returns a new instance of Runner.

Parameters:

  • args (Array)


20
21
22
23
24
25
26
27
28
29
# File 'lib/cli/runner.rb', line 20

def initialize(args, options = {})
  @args = args
  @options = options.dup

  banner = "Usage: bosh [<options>] <command> [<args>]"
  @option_parser = OptionParser.new(banner)

  Config.colorize = true
  Config.output ||= STDOUT
end

Instance Attribute Details

#argsArray (readonly)

Returns:

  • (Array)


9
10
11
# File 'lib/cli/runner.rb', line 9

def args
  @args
end

#optionsHash (readonly)

Returns:

  • (Hash)


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

def options
  @options
end

Class Method Details

.run(args) ⇒ Object

Parameters:

  • args (Array)


15
16
17
# File 'lib/cli/runner.rb', line 15

def self.run(args)
  new(args).run
end

Instance Method Details

#add_shortcutsObject



218
219
220
221
222
223
224
225
226
# File 'lib/cli/runner.rb', line 218

def add_shortcuts
  {
    "st" => "status",
    "props" => "properties",
    "cck" => "cloudcheck"
  }.each do |short, long|
    @parse_tree[short] = @parse_tree[long]
  end
end

#build_parse_treeObject



203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/cli/runner.rb', line 203

def build_parse_tree
  @parse_tree = ParseTreeNode.new

  Config.commands.each_value do |command|
    p = @parse_tree
    n_kw = command.keywords.size

    command.keywords.each_with_index do |kw, i|
      p[kw] ||= ParseTreeNode.new
      p = p[kw]
      p.command = command if i == n_kw - 1
    end
  end
end

#find_completions(words, node = @parse_tree, index = 0) ⇒ Object

Finds command completions in the parse tree

Parameters:

  • words (Array)

    Completion prefix

  • node (Bosh::Cli::ParseTreeNode) (defaults to: @parse_tree)

    Current parse tree node



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/cli/runner.rb', line 83

def find_completions(words, node = @parse_tree, index = 0)
  word = words[index]

  # exact match and not on the last word
  if node[word] && words.length != index
    find_completions(words, node[word], index + 1)

    # exact match at the last word
  elsif node[word]
    node[word].values

    # find all partial matches
  else
    node.keys.grep(/^#{word}/)
  end
end

#get_gem_pluginsObject



189
190
191
192
193
194
195
196
197
# File 'lib/cli/runner.rb', line 189

def get_gem_plugins
  Gem::Specification.latest_specs(true).map { |spec|
    spec.matches_for_glob(plugins_glob)
  }.flatten.uniq
rescue
  err("Cannot load plugins, ".make_yellow +
          "please run `gem update --system' to ".make_yellow +
          "update your RubyGems".make_yellow)
end

#load_gem_pluginsObject



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/cli/runner.rb', line 171

def load_gem_plugins
  get_gem_plugins.each do |plugin_path|
    original_commands = Config.commands.size

    begin
      next unless require_plugin plugin_path
    rescue Exception => e
      err("Failed to load plugin #{plugin_path}: #{e.message}".make_red)
    end

    if Config.commands =~ original_commands
      say(("File #{plugin_path} has been loaded as plugin but it didn't " +
          "contain any commands.\nMake sure this plugin is updated to be " +
          "compatible with BOSH CLI 1.0.").columnize(80).make_yellow)
    end
  end
end

#load_local_pluginsObject



164
165
166
167
168
169
# File 'lib/cli/runner.rb', line 164

def load_local_plugins
  Dir.glob(File.join("lib", plugins_glob)).each do |file|
    say("WARNING: loading local plugin: #{file}")
    require_plugin(file)
  end
end

#load_pluginsvoid

This method returns an undefined value.

Discover and load CLI plugins from all available gems



159
160
161
162
# File 'lib/cli/runner.rb', line 159

def load_plugins
  load_local_plugins
  load_gem_plugins
end

#parse_global_optionsObject



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
125
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
# File 'lib/cli/runner.rb', line 100

def parse_global_options
  # -v is reserved for verbose but having 'bosh -v' is handy,
  # hence the little hack
  if @args.size == 1 && (@args[0] == "-v" || @args[0] == "--version")
    @args = %w(version)
    return
  end

  opts = @option_parser
  config_desc = "Override configuration file. Also can be overridden " +
                "by BOSH_CONFIG environment variable. Defaults to " +
                "$HOME/.bosh_config. Override precedence is command-" +
                "line option, then environment variable, then home directory."
  opts.on("-c", "--config FILE", config_desc) do |file|
    @options[:config] = file
  end

  opts.on("--[no-]color", "Toggle colorized output") do |v|
    Config.colorize = v
  end

  opts.on("-v", "--verbose", "Show additional output") do
    @options[:verbose] = true
  end
  opts.on("-q", "--quiet", "Suppress all output") do
    Config.output = nil
  end
  opts.on("-n", "--non-interactive", "Don't ask for user input") do
    @options[:non_interactive] = true
  end
  opts.on("-N", "--no-track", "Return Task ID and don't track") do
    @options[:no_track] = true
  end
  opts.on("-P", "--poll INTERVAL", "Director task polling interval") do |interval|
    @options[:poll_interval] = Integer(interval)
  end
  opts.on("-t", "--target URL", "Override target") do |target|
    @options[:target] = target
  end
  opts.on("-u", "--user USER", "Override username") do |user|
    @options[:username] = user
  end
  opts.on("-p", "--password PASSWORD", "Override password") do |pass|
    @options[:password] = pass
  end
  opts.on("-d", "--deployment FILE", "Override deployment") do |file|
    @options[:deployment] = file
  end
  opts.on("-h", "--help", "here you go") do
    @args << 'help'
  end

  @args = @option_parser.order!(@args)
end

#plugins_globObject



155
# File 'lib/cli/runner.rb', line 155

def plugins_glob; "bosh/cli/commands/*.rb"; end

#require_plugin(file) ⇒ Object



199
200
201
# File 'lib/cli/runner.rb', line 199

def require_plugin(file)
  require File.absolute_path(file)
end

#runvoid

This method returns an undefined value.

Find and run CLI command



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/cli/runner.rb', line 33

def run
  parse_global_options

  Config.interactive = !@options[:non_interactive]
  Config.poll_interval = @options[:poll_interval]

  load_plugins
  build_parse_tree
  add_shortcuts

  @args = %w(help) if @args.empty?

  command = search_parse_tree(@parse_tree)
  if command.nil? && Config.interactive
    command = try_alias
  end

  if command.nil?
    err("Unknown command: #{@args.join(" ")}")
  end

  command.runner = self
  begin
    exit_code = command.run(@args, @options)
    exit(exit_code)
  rescue OptionParser::ParseError => e
    say_err(e.message)
    nl
    say_err("Usage: bosh #{command.usage_with_params.columnize(60, 7)}")
    nl
    if command.has_options?
      say(command.options_summary.indent(7))
    end
    exit(1)
  end

rescue OptionParser::ParseError => e
  say_err(e.message)
  say_err(@option_parser.to_s)
  exit(1)

rescue Bosh::Cli::CliError => e
  say_err(e.message)
  nl
  exit(e.exit_code)
end

#search_parse_tree(node) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/cli/runner.rb', line 232

def search_parse_tree(node)
  return nil if node.nil?
  arg = @args.shift

  longer_command = search_parse_tree(node[arg])

  if longer_command.nil?
    @args.unshift(arg) if arg # backtrack if needed
    node.command
  else
    longer_command
  end
end

#try_aliasObject



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
272
273
274
275
# File 'lib/cli/runner.rb', line 246

def try_alias
  # Tries to find best match among aliases (possibly multiple words),
  # then unwinds it onto the remaining args and searches parse tree again.
  # Not the most effective algorithm but does the job.
  config = Bosh::Cli::Config.new(@options[:config])
  candidate = []
  best_match = nil
  save_args = @args.dup

  while (arg = @args.shift)
    candidate << arg
    resolved = config.resolve_alias(:cli, candidate.join(" "))
    if best_match && resolved.nil?
      @args.unshift(arg)
      break
    end
    best_match = resolved
  end

  if best_match.nil?
    @args = save_args
    return
  end

  best_match.split(/\s+/).reverse.each do |keyword|
    @args.unshift(keyword)
  end

  search_parse_tree(@parse_tree)
end

#usageObject



228
229
230
# File 'lib/cli/runner.rb', line 228

def usage
  @option_parser.to_s
end