Class: Bolt::CLI
- Inherits:
-
Object
- Object
- Bolt::CLI
- Defined in:
- lib/bolt/cli.rb
Constant Summary collapse
- COMMANDS =
{ 'apply' => %w[], 'command' => %w[run], 'file' => %w[download upload], 'group' => %w[show], 'guide' => %w[], 'inventory' => %w[show], 'lookup' => %w[], 'module' => %w[add generate-types install show], 'plan' => %w[show run convert new], 'plugin' => %w[show], 'policy' => %w[apply new show], 'project' => %w[init migrate], 'script' => %w[run], 'secret' => %w[encrypt decrypt createkeys], 'task' => %w[show run] }.freeze
- TARGETING_OPTIONS =
%i[query rerun targets].freeze
- SUCCESS =
0
- FAILURE =
1
Instance Attribute Summary collapse
-
#outputter ⇒ Object
readonly
Returns the value of attribute outputter.
-
#rerun ⇒ Object
readonly
Returns the value of attribute rerun.
Instance Method Summary collapse
-
#execute(options) ⇒ Object
Execute a Bolt command.
-
#initialize(argv) ⇒ CLI
constructor
A new instance of CLI.
-
#parse ⇒ Object
TODO: Move most of this to the parser.
Constructor Details
Instance Attribute Details
#outputter ⇒ Object (readonly)
Returns the value of attribute outputter.
34 35 36 |
# File 'lib/bolt/cli.rb', line 34 def outputter @outputter end |
#rerun ⇒ Object (readonly)
Returns the value of attribute rerun.
34 35 36 |
# File 'lib/bolt/cli.rb', line 34 def rerun @rerun end |
Instance Method Details
#execute(options) ⇒ Object
Execute a Bolt command. The options
hash includes the subcommand and action to be run, as well as any additional arguments and options for the command.
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 |
# File 'lib/bolt/cli.rb', line 393 def execute() with_signal_handling do with_error_handling do # TODO: Separate from options hash and pass as own args. command = [:subcommand] action = [:action] # # INITIALIZE CORE CLASSES # project = if ENV['BOLT_PROJECT'] Bolt::Project.create_project(ENV['BOLT_PROJECT'], 'environment') elsif [:project] dir = Pathname.new([:project]) if (dir + Bolt::Project::BOLTDIR_NAME).directory? Bolt::Project.create_project(dir + Bolt::Project::BOLTDIR_NAME) else Bolt::Project.create_project(dir) end else Bolt::Project.find_boltdir(Dir.pwd) end config = Bolt::Config.from_project(project, ) @outputter = Bolt::Outputter.for_format( config.format, config.color, [:verbose], config.trace, config.spinner ) @rerun = Bolt::Rerun.new(config.rerunfile, config.save_rerun) # TODO: Subscribe this to the executor. analytics = begin client = Bolt::Analytics.build_client(config.analytics) client.bundled_content = bundled_content() client end Bolt::Logger.configure(config.log, config.color, config.disable_warnings) Bolt::Logger.stream = config.stream Bolt::Logger.analytics = analytics Bolt::Logger.flush_queue executor = Bolt::Executor.new( config.concurrency, analytics, [:noop], config.modified_concurrency, config.future ) pal = Bolt::PAL.new( Bolt::Config::Modulepath.new(config.modulepath), config.hiera_config, config.project.resource_types, config.compile_concurrency, config.trusted_external, config.apply_settings, config.project ) plugins = Bolt::Plugin.new(config, pal, analytics) inventory = Bolt::Inventory.from_config(config, plugins) log_outputter = Bolt::Outputter::Logger.new([:verbose], config.trace) # # FINALIZING SETUP # check_gem_install warn_inventory_overrides_cli(config, ) submit_screen_view(analytics, config, inventory, ) [:targets] = process_target_list(plugins, @rerun, ) # TODO: Fix casing issue in Windows. config.check_path_case('modulepath', config.modulepath) if [:clear_cache] FileUtils.rm(config.project.plugin_cache_file) if File.exist?(config.project.plugin_cache_file) FileUtils.rm(config.project.task_cache_file) if File.exist?(config.project.task_cache_file) FileUtils.rm(config.project.plan_cache_file) if File.exist?(config.project.plan_cache_file) end case command when 'apply', 'lookup' if %w[human rainbow].include?(config.format) executor.subscribe(outputter) end when 'plan' if %w[human rainbow].include?(config.format) executor.subscribe(outputter) else executor.subscribe(outputter, %i[message verbose]) end else executor.subscribe(outputter) end executor.subscribe(log_outputter) # TODO: Figure out where this should really go. It doesn't seem to # make sense in the application, since the params should already # be data when they reach that point. if %w[plan task].include?(command) && action == 'run' [:params] = parse_params( command, [:object], pal, **.slice(:params, :params_parsed) ) end application = Bolt::Application.new( analytics: analytics, config: config, executor: executor, inventory: inventory, pal: pal, plugins: plugins ) process_command(application, command, action, ) ensure analytics&.finish end end end |
#parse ⇒ Object
TODO: Move most of this to the parser.
Parse the command and validate options. All errors that are raised here are not handled by the outputter, as it relies on config being loaded.
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 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 165 166 |
# File 'lib/bolt/cli.rb', line 96 def parse with_error_handling do = {} parser = BoltOptionParser.new() # This part aims to handle both `bolt <mode> --help` and `bolt help <mode>`. remaining = parser.permute(@argv) unless @argv.empty? if @argv.empty? || help?(, remaining) # If the subcommand is not enabled, display the default # help text [:subcommand] = nil unless COMMANDS.include?([:subcommand]) if Bolt::Util.first_run? FileUtils.touch(Bolt::Util.first_runs_free) if [:subcommand].nil? && $stdout.isatty raise Bolt::CLIExit end end # Update the parser for the subcommand (or lack thereof) parser.update puts parser.help raise Bolt::CLIExit end if [:version] puts Bolt::VERSION raise Bolt::CLIExit end [:object] = remaining.shift # Handle reading a command from a file if [:subcommand] == 'command' && [:object] [:object] = Bolt::Util.get_arg_input([:object]) end # Only parse params for task or plan if %w[task plan].include?([:subcommand]) params, remaining = remaining.partition { |s| s =~ /.+=/ } if [:params] unless params.empty? raise Bolt::CLIError, "Parameters must be specified through either the --params " \ "option or param=value pairs, not both" end [:params_parsed] = true elsif params.any? [:params_parsed] = false [:params] = Hash[params.map { |a| a.split('=', 2) }] else [:params_parsed] = true [:params] = {} end end [:leftovers] = remaining # Default to verbose for everything except plans unless .key?(:verbose) [:verbose] = [:subcommand] != 'plan' end validate() validate_ps_version end end |