# 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[targets query rerun description], authentication: %w[user password password-prompt private-key host-key-check ssl ssl-verify], escalation: %w[run-as sudo-password sudo-password-prompt sudo-executable], run_context: %w[concurrency inventoryfile save-rerun cleanup], global_config_setters: %w[modulepath project configfile], transports: %w[transport connect-timeout tty native-ssh ssh-command copy-command], display: %w[format color verbose trace], global: %w[help version debug log-level] }.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 hiera-config], banner: APPLY_HELP } when 'command' case action when 'run' { flags: ACTION_OPTS + %w[env-var], banner: COMMAND_RUN_HELP } else { flags: OPTIONS[:global], banner: COMMAND_HELP } end when 'file' case action when 'upload' { flags: ACTION_OPTS + %w[tmpdir], banner: FILE_UPLOAD_HELP } when 'download' { flags: ACTION_OPTS, banner: FILE_DOWNLOAD_HELP } else { flags: OPTIONS[:global], banner: FILE_HELP } end when 'inventory' case action when 'show' { flags: OPTIONS[:inventory] + OPTIONS[:global] + %w[format inventoryfile boltdir configfile detail], banner: INVENTORY_SHOW_HELP } else { flags: OPTIONS[:global], banner: INVENTORY_HELP } end when 'group' case action when 'show' { flags: OPTIONS[:global] + %w[format inventoryfile boltdir configfile], banner: GROUP_SHOW_HELP } else { flags: OPTIONS[:global], banner: GROUP_HELP } end when 'guide' { flags: OPTIONS[:global] + %w[format], banner: GUIDE_HELP } when 'module' case action when 'install' { flags: OPTIONS[:global] + %w[configfile force project], banner: MODULE_INSTALL_HELP } when 'show' { flags: OPTIONS[:global] + OPTIONS[:global_config_setters], banner: MODULE_SHOW_HELP } when 'generate-types' { flags: OPTIONS[:global] + OPTIONS[:global_config_setters], banner: MODULE_GENERATETYPES_HELP } else { flags: OPTIONS[:global], banner: MODULE_HELP } end when 'plan' case action when 'convert' { flags: OPTIONS[:global] + OPTIONS[:global_config_setters], banner: PLAN_CONVERT_HELP } when 'new' { flags: OPTIONS[:global] + %w[configfile project], banner: PLAN_NEW_HELP } when 'run' { flags: ACTION_OPTS + %w[params compile-concurrency tmpdir hiera-config], banner: PLAN_RUN_HELP } when 'show' { flags: OPTIONS[:global] + OPTIONS[:global_config_setters] + %w[filter format], banner: PLAN_SHOW_HELP } else { flags: OPTIONS[:global], banner: PLAN_HELP } end when 'project' case action when 'init' { flags: OPTIONS[:global] + %w[modules], banner: PROJECT_INIT_HELP } when 'migrate' { flags: OPTIONS[:global] + %w[inventoryfile project configfile], banner: PROJECT_MIGRATE_HELP } else { flags: OPTIONS[:global], banner: PROJECT_HELP } end when 'puppetfile' case action when 'install' { flags: OPTIONS[:global] + OPTIONS[:global_config_setters] + %w[puppetfile], 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], banner: PUPPETFILE_HELP } end when 'script' case action when 'run' { flags: ACTION_OPTS + %w[tmpdir env-var], banner: SCRIPT_RUN_HELP } else { flags: OPTIONS[:global], banner: SCRIPT_HELP } end when 'secret' case action when 'createkeys' { flags: OPTIONS[:global] + OPTIONS[:global_config_setters] + %w[plugin force], banner: SECRET_CREATEKEYS_HELP } when 'decrypt' { flags: OPTIONS[:global] + OPTIONS[:global_config_setters] + %w[plugin], banner: SECRET_DECRYPT_HELP } when 'encrypt' { flags: OPTIONS[:global] + OPTIONS[:global_config_setters] + %w[plugin], banner: SECRET_ENCRYPT_HELP } else { flags: OPTIONS[:global], banner: SECRET_HELP } end when 'task' case action when 'run' { flags: ACTION_OPTS + %w[params tmpdir noop], banner: TASK_RUN_HELP } when 'show' { flags: OPTIONS[:global] + OPTIONS[:global_config_setters] + %w[filter format], banner: TASK_SHOW_HELP } else { flags: OPTIONS[:global], banner: TASK_HELP } end else { flags: OPTIONS[:global], banner: BANNER } end end BANNER = <<~HELP NAME bolt USAGE bolt [action] [options] DESCRIPTION Bolt is an orchestration tool that automates the manual work it takes to maintain your infrastructure. SUBCOMMANDS apply Apply Puppet manifest code command Run a command remotely file Copy files between the controller and targets group Show the list of groups in the inventory guide View guides for Bolt concepts and features inventory Show the list of targets an action would run on plan Convert, create, show, and run Bolt plans project Create and migrate Bolt projects puppetfile Install and list modules and generate type references script Upload a local script and run it remotely secret Create encryption keys and encrypt and decrypt values task Show and run Bolt tasks GUIDES For a list of guides on Bolt's concepts and features, run 'bolt guide'. HELP APPLY_HELP = <<~HELP NAME apply USAGE bolt apply [manifest.pp] [options] DESCRIPTION Apply Puppet manifest code on the specified targets. EXAMPLES bolt apply manifest.pp -t target bolt apply -e "file { '/etc/puppetlabs': ensure => present }" -t target HELP COMMAND_HELP = <<~HELP NAME command USAGE bolt command [options] DESCRIPTION Run a command on the specified targets. ACTIONS run Run a command on the specified targets. HELP COMMAND_RUN_HELP = <<~HELP NAME run USAGE bolt command run [options] DESCRIPTION Run a command on the specified targets. EXAMPLES bolt command run 'uptime' -t target1,target2 HELP FILE_HELP = <<~HELP NAME file USAGE bolt file [options] DESCRIPTION Copy files and directories between the controller and targets ACTIONS download Download a file or directory to the controller upload Upload a local file or directory from the controller HELP FILE_DOWNLOAD_HELP = <<~HELP NAME download USAGE bolt file download [options] DESCRIPTION Download a file or directory from one or more targets. Downloaded files and directories are saved to the a subdirectory matching the target's name under the destination directory. The destination directory is expanded relative to the downloads subdirectory of the project directory. EXAMPLES bolt file download /etc/ssh_config ssh_config -t all HELP FILE_UPLOAD_HELP = <<~HELP NAME upload USAGE bolt file upload [options] DESCRIPTION Upload a local file or directory. EXAMPLES bolt file upload /tmp/source /etc/profile.d/login.sh -t target1 HELP GROUP_HELP = <<~HELP NAME group USAGE bolt group [options] DESCRIPTION Show the list of groups in the inventory. ACTIONS show Show the list of groups in the inventory HELP GROUP_SHOW_HELP = <<~HELP NAME show USAGE bolt group show [options] DESCRIPTION Show the list of groups in the inventory. HELP GUIDE_HELP = <<~HELP NAME guide USAGE bolt guide [topic] [options] DESCRIPTION View guides for Bolt's concepts and features. Omitting a topic will display a list of available guides, while providing a topic will display the relevant guide. EXAMPLES View a list of available guides bolt guide View the 'project' guide page bolt guide project HELP INVENTORY_HELP = <<~HELP NAME inventory USAGE bolt inventory [options] DESCRIPTION Show the list of targets an action would run on. ACTIONS show Show the list of targets an action would run on HELP INVENTORY_SHOW_HELP = <<~HELP NAME show USAGE bolt inventory show [options] DESCRIPTION Show the list of targets an action would run on. HELP MODULE_HELP = <<~HELP NAME module USAGE bolt module [options] DESCRIPTION Install and list modules and generate type references ACTIONS generate-types Generate type references to register in plans install Install the project's modules show List modules available to the Bolt project HELP MODULE_INSTALL_HELP = <<~HELP NAME install USAGE bolt module install [options] DESCRIPTION Install the project's modules. Module declarations are loaded from the project's configuration file. Bolt will automatically resolve all module dependencies, generate a Puppetfile, and install the modules. HELP MODULE_GENERATETYPES_HELP = <<~HELP NAME generate-types USAGE bolt module generate-types [options] DESCRIPTION Generate type references to register in plans. HELP MODULE_SHOW_HELP = <<~HELP NAME show USAGE bolt module show [options] DESCRIPTION List modules available to the Bolt project. HELP PLAN_HELP = <<~HELP NAME plan USAGE bolt plan [parameters] [options] DESCRIPTION Convert, create, show, and run Bolt plans. ACTIONS convert Convert a YAML plan to a Bolt plan new Create a new plan in the current project run Run a plan on the specified targets show Show available plans and plan documentation HELP PLAN_CONVERT_HELP = <<~HELP NAME convert USAGE bolt plan convert [options] DESCRIPTION Convert a YAML plan to a Puppet language plan and print the converted plan to stdout. Converting a YAML plan may result in a plan that is syntactically correct but has different behavior. Always verify a converted plan's functionality. Note that the converted plan is not written to a file. EXAMPLES bolt plan convert path/to/plan/myplan.yaml HELP PLAN_NEW_HELP = <<~HELP NAME new USAGE bolt plan new [options] DESCRIPTION Create a new plan in the current project. EXAMPLES bolt plan new myproject::myplan HELP PLAN_RUN_HELP = <<~HELP NAME run USAGE bolt plan run [parameters] [options] DESCRIPTION Run a plan on the specified targets. EXAMPLES bolt plan run canary --targets target1,target2 command=hostname HELP PLAN_SHOW_HELP = <<~HELP NAME show USAGE bolt plan show [plan] [options] DESCRIPTION Show available plans and plan documentation. Omitting the name of a plan will display a list of plans available in the Bolt project. Providing the name of a plan will display detailed documentation for the plan, including a list of available parameters. EXAMPLES Display a list of available plans bolt plan show Display documentation for the aggregate::count plan bolt plan show aggregate::count HELP PROJECT_HELP = <<~HELP NAME project USAGE bolt project [options] DESCRIPTION Create and migrate Bolt projects ACTIONS init Create a new Bolt project migrate Migrate a Bolt project to the latest version HELP PROJECT_INIT_HELP = <<~HELP NAME init USAGE bolt project init [name] [options] DESCRIPTION Create a new Bolt project in the current working directory. Specify a name for the Bolt project. Defaults to the basename of the current working directory. EXAMPLES Create a new Bolt project using the directory as the project name. bolt project init Create a new Bolt project with a specified name. bolt project init myproject Create a new Bolt project with existing modules. bolt project init --modules puppetlabs-apt,puppetlabs-ntp HELP PROJECT_MIGRATE_HELP = <<~HELP NAME migrate USAGE bolt project migrate [options] DESCRIPTION Migrate a Bolt project to use current best practices and the latest version of configuration files. HELP PUPPETFILE_HELP = <<~HELP NAME puppetfile USAGE bolt puppetfile [options] DESCRIPTION Install and list modules and generate type references ACTIONS generate-types Generate type references to register in plans install Install modules from a Puppetfile into a project show-modules List modules available to the Bolt project HELP PUPPETFILE_GENERATETYPES_HELP = <<~HELP NAME generate-types USAGE bolt puppetfile generate-types [options] DESCRIPTION Generate type references to register in plans. HELP PUPPETFILE_INSTALL_HELP = <<~HELP NAME install USAGE bolt puppetfile install [options] DESCRIPTION Install modules from a Puppetfile into a project HELP PUPPETFILE_SHOWMODULES_HELP = <<~HELP NAME show-modules USAGE bolt puppetfile show-modules [options] DESCRIPTION List modules available to the Bolt project. HELP SCRIPT_HELP = <<~HELP NAME script USAGE bolt script [options] DESCRIPTION Run a script on the specified targets. ACTIONS run Run a script on the specified targets. HELP SCRIPT_RUN_HELP = <<~HELP NAME run USAGE bolt script run