Class: Bolt::CLI

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ CLI

Returns a new instance of CLI.


59
60
61
62
63
# File 'lib/bolt/cli.rb', line 59

def initialize(argv)
  Bolt::Logger.initialize_logging
  @logger = Bolt::Logger.logger(self)
  @argv   = argv
end

Instance Attribute Details

#outputterObject (readonly)

Returns the value of attribute outputter.


34
35
36
# File 'lib/bolt/cli.rb', line 34

def outputter
  @outputter
end

#rerunObject (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.

Parameters:

  • options (Hash)

    The CLI options.


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(options)
  with_signal_handling do
    with_error_handling do
      # TODO: Separate from options hash and pass as own args.
      command = options[:subcommand]
      action  = options[:action]

      #
      # INITIALIZE CORE CLASSES
      #

      project = if ENV['BOLT_PROJECT']
                  Bolt::Project.create_project(ENV['BOLT_PROJECT'], 'environment')
                elsif options[:project]
                  dir = Pathname.new(options[: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, options)

      @outputter = Bolt::Outputter.for_format(
        config.format,
        config.color,
        options[: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(options)
        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,
        options[: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(options[:verbose], config.trace)

      #
      # FINALIZING SETUP
      #

      check_gem_install
      warn_inventory_overrides_cli(config, options)
      submit_screen_view(analytics, config, inventory, options)
      options[:targets] = process_target_list(plugins, @rerun, options)

      # TODO: Fix casing issue in Windows.
      config.check_path_case('modulepath', config.modulepath)

      if options[: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'
        options[:params] = parse_params(
          command,
          options[:object],
          pal,
          **options.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, options)
    ensure
      analytics&.finish
    end
  end
end

#parseObject

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
    options = {}
    parser  = BoltOptionParser.new(options)

    # This part aims to handle both `bolt <mode> --help` and `bolt help <mode>`.
    remaining = parser.permute(@argv) unless @argv.empty?

    if @argv.empty? || help?(options, remaining)
      # If the subcommand is not enabled, display the default
      # help text
      options[:subcommand] = nil unless COMMANDS.include?(options[:subcommand])

      if Bolt::Util.first_run?
        FileUtils.touch(Bolt::Util.first_runs_free)

        if options[:subcommand].nil? && $stdout.isatty
          welcome_message
          raise Bolt::CLIExit
        end
      end

      # Update the parser for the subcommand (or lack thereof)
      parser.update
      puts parser.help
      raise Bolt::CLIExit
    end

    if options[:version]
      puts Bolt::VERSION
      raise Bolt::CLIExit
    end

    options[:object] = remaining.shift

    # Handle reading a command from a file
    if options[:subcommand] == 'command' && options[:object]
      options[:object] = Bolt::Util.get_arg_input(options[:object])
    end

    # Only parse params for task or plan
    if %w[task plan].include?(options[:subcommand])
      params, remaining = remaining.partition { |s| s =~ /.+=/ }
      if options[:params]
        unless params.empty?
          raise Bolt::CLIError,
                "Parameters must be specified through either the --params " \
                "option or param=value pairs, not both"
        end
        options[:params_parsed] = true
      elsif params.any?
        options[:params_parsed] = false
        options[:params] = Hash[params.map { |a| a.split('=', 2) }]
      else
        options[:params_parsed] = true
        options[:params] = {}
      end
    end
    options[:leftovers] = remaining

    # Default to verbose for everything except plans
    unless options.key?(:verbose)
      options[:verbose] = options[:subcommand] != 'plan'
    end

    validate(options)
    validate_ps_version

    options
  end
end