Class: Bosh::Cli::Runner
Instance Attribute Summary collapse
- #args ⇒ Array readonly
- #options ⇒ Hash readonly
Class Method Summary collapse
Instance Method Summary collapse
- #add_shortcuts ⇒ Object
- #build_parse_tree ⇒ Object
-
#find_completions(words, node = @parse_tree, index = 0) ⇒ Object
Finds command completions in the parse tree.
- #get_gem_plugins ⇒ Object
-
#initialize(args, options = {}) ⇒ Runner
constructor
A new instance of Runner.
- #load_gem_plugins ⇒ Object
- #load_local_plugins ⇒ Object
-
#load_plugins ⇒ void
Discover and load CLI plugins from all available gems.
- #parse_global_options ⇒ Object
- #plugins_glob ⇒ Object
- #require_plugin(file) ⇒ Object
-
#run(exit_on_success = true) ⇒ void
Find and run CLI command.
- #search_parse_tree(node) ⇒ Object
- #try_alias ⇒ Object
- #usage ⇒ Object
Constructor Details
#initialize(args, options = {}) ⇒ Runner
Returns a new instance of Runner.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/cli/runner.rb', line 20 def initialize(args, = {}) @args = args @options = .dup = 'Usage: bosh [<options>] <command> [<args>]' @option_parser = OptionParser.new() Config.colorize = nil if ENV.has_key?('BOSH_COLOR') && ENV['BOSH_COLOR'] == 'false' Config.colorize = false end Config.output ||= STDOUT end |
Instance Attribute Details
#args ⇒ Array (readonly)
9 10 11 |
# File 'lib/cli/runner.rb', line 9 def args @args end |
#options ⇒ Hash (readonly)
12 13 14 |
# File 'lib/cli/runner.rb', line 12 def @options end |
Class Method Details
.run(args) ⇒ Object
15 16 17 |
# File 'lib/cli/runner.rb', line 15 def self.run(args) new(args).run end |
Instance Method Details
#add_shortcuts ⇒ Object
230 231 232 233 234 235 236 237 238 |
# File 'lib/cli/runner.rb', line 230 def add_shortcuts { 'st' => 'status', 'props' => 'properties', 'cck' => 'cloudcheck' }.each do |short, long| @parse_tree[short] = @parse_tree[long] end end |
#build_parse_tree ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/cli/runner.rb', line 215 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
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/cli/runner.rb', line 87 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_plugins ⇒ Object
201 202 203 204 205 206 207 208 209 |
# File 'lib/cli/runner.rb', line 201 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_plugins ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/cli/runner.rb', line 183 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.}".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_plugins ⇒ Object
176 177 178 179 180 181 |
# File 'lib/cli/runner.rb', line 176 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_plugins ⇒ void
This method returns an undefined value.
Discover and load CLI plugins from all available gems
171 172 173 174 |
# File 'lib/cli/runner.rb', line 171 def load_plugins load_local_plugins load_gem_plugins end |
#parse_global_options ⇒ Object
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 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/cli/runner.rb', line 104 def # -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('--parallel MAX', 'Sets the max number of parallel downloads') do |max| Config.max_parallel_downloads = Integer(max) 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 opts.on('--ca-cert FILE', 'Override CA certificate') do |file| @options[:ca_cert] = file end @args = @option_parser.order!(@args) end |
#plugins_glob ⇒ Object
166 167 |
# File 'lib/cli/runner.rb', line 166 def plugins_glob; 'bosh/cli/commands/*.rb' ; end |
#require_plugin(file) ⇒ Object
211 212 213 |
# File 'lib/cli/runner.rb', line 211 def require_plugin(file) require File.absolute_path(file) end |
#run(exit_on_success = true) ⇒ void
This method returns an undefined value.
Find and run CLI command
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 79 80 81 82 |
# File 'lib/cli/runner.rb', line 38 def run(exit_on_success=true) 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, info = command.run(@args, @options) exit(exit_code) if exit_on_success || exit_code != 0 [exit_code, info] rescue OptionParser::ParseError => e say_err(e.) nl say_err("Usage: bosh #{command.usage_with_params.columnize(60, 7)}") nl if command. say(command..indent(7)) end exit(1) end rescue OptionParser::ParseError => e say_err(e.) say_err(@option_parser.to_s) exit(1) rescue Bosh::Cli::CliError => e say_err(e.) nl exit(e.exit_code) end |
#search_parse_tree(node) ⇒ Object
244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/cli/runner.rb', line 244 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_alias ⇒ Object
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/cli/runner.rb', line 258 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 |
#usage ⇒ Object
240 241 242 |
# File 'lib/cli/runner.rb', line 240 def usage @option_parser.to_s end |