# frozen_string_literal: true # Note this file includes very few 'requires' because it expects to be used from the CLI. require 'optparse' module Bolt class BoltOptionParser < OptionParser OPTIONS = { inventory: %w[nodes targets query rerun description], authentication: %w[user password private-key host-key-check ssl ssl-verify], escalation: %w[run-as sudo-password], run_context: %w[concurrency inventoryfile save-rerun], global_config_setters: %w[modulepath boltdir configfile], transports: %w[transport connect-timeout tty], display: %w[format color verbose trace], global: %w[help version debug] }.freeze ACTION_OPTS = OPTIONS.values.flatten.freeze def get_help_text(subcommand, action = nil) case subcommand when 'apply' { flags: ACTION_OPTS + %w[noop execute compile-concurrency], banner: APPLY_HELP } when 'command' { flags: ACTION_OPTS, banner: COMMAND_HELP } when 'file' { flags: ACTION_OPTS + %w[tmpdir], banner: FILE_HELP } when 'inventory' { flags: OPTIONS[:inventory] + OPTIONS[:global] + %w[format inventoryfile boltdir configfile detail], banner: INVENTORY_HELP } when 'group' { flags: OPTIONS[:global] + %w[format inventoryfile boltdir configfile], banner: GROUP_HELP } when 'plan' case action when 'convert' { flags: OPTIONS[:global] + OPTIONS[:global_config_setters], banner: PLAN_CONVERT_HELP } when 'show' { flags: OPTIONS[:global] + OPTIONS[:global_config_setters], banner: PLAN_SHOW_HELP } when 'run' { flags: ACTION_OPTS + %w[params compile-concurrency tmpdir], banner: PLAN_RUN_HELP } else { flags: ACTION_OPTS + %w[params compile-concurrency tmpdir], banner: PLAN_HELP } end when 'project' case action when 'init' { flags: OPTIONS[:global], banner: PROJECT_INIT_HELP } else { flags: OPTIONS[:global], banner: PROJECT_HELP } end when 'puppetfile' case action when 'install' { flags: OPTIONS[:global] + OPTIONS[:global_config_setters], banner: PUPPETFILE_INSTALL_HELP } when 'show-modules' { flags: OPTIONS[:global] + OPTIONS[:global_config_setters], banner: PUPPETFILE_SHOWMODULES_HELP } when 'generate-types' { flags: OPTIONS[:global] + OPTIONS[:global_config_setters], banner: PUPPETFILE_GENERATETYPES_HELP } else { flags: OPTIONS[:global] + OPTIONS[:global_config_setters], banner: PUPPETFILE_HELP } end when 'script' { flags: ACTION_OPTS + %w[tmpdir], banner: SCRIPT_HELP } when 'secret' { flags: OPTIONS[:global] + OPTIONS[:global_config_setters] + %w[plugin], banner: SECRET_HELP } when 'task' case action when 'show' { flags: OPTIONS[:global] + OPTIONS[:global_config_setters], banner: TASK_SHOW_HELP } when 'run' { flags: ACTION_OPTS + %w[params tmpdir], banner: TASK_RUN_HELP } else { flags: ACTION_OPTS + %w[params tmpdir], banner: TASK_HELP } end else { flags: OPTIONS[:global], banner: BANNER } end end def self.examples(cmd, desc) <<~EXAMP #{desc} a Windows host via WinRM, providing for the password bolt #{cmd} -n winrm://winhost -u Administrator -p #{desc} the local machine, a Linux host via SSH, and hosts from a group specified in an inventory file bolt #{cmd} -n localhost,nixhost,node_group #{desc} Windows hosts queried from PuppetDB via WinRM as a domain user, prompting for the password bolt #{cmd} -q 'inventory[certname] { facts.os.family = "windows" }' --transport winrm -u 'domain\\Administrator' -p EXAMP end BANNER = <<~HELP Usage: bolt Available subcommands: bolt command run Run a command remotely bolt file upload Upload a local file or directory bolt script run