Class: CemAcpt::Bolt::Cmd::Base
- Inherits:
-
Object
- Object
- CemAcpt::Bolt::Cmd::Base
- Includes:
- Logging
- Defined in:
- lib/cem_acpt/bolt/cmd/base.rb
Overview
Base class for all Bolt command classes
Direct Known Subclasses
Constant Summary
Constants included from Logging
Instance Attribute Summary collapse
-
#cmd_env ⇒ Object
readonly
Returns the value of attribute cmd_env.
-
#cmd_params ⇒ Object
readonly
Returns the value of attribute cmd_params.
Class Method Summary collapse
-
.option(name, flag, config_path: nil, default: nil, bool_flag: false) ⇒ Object
Class method that defines an option for the command.
-
.option_names ⇒ Object
Class method that holds the names of all options for the command.
-
.supports_params ⇒ Object
Denotes that the command supports parameters.
Instance Method Summary collapse
-
#add_cmd_env(name, value) ⇒ Object
Adds an environment variable to the command.
-
#add_cmd_param(name, value) ⇒ Object
Adds a parameter to the command.
- #bolt_bin ⇒ Object
-
#cmd ⇒ String
Returns the command to run as a string.
-
#command_family ⇒ String
Returns the family of the command.
-
#initialize ⇒ Base
constructor
A new instance of Base.
- #inspect ⇒ Object
-
#options ⇒ String
Returns the options for the command as a string.
-
#run(strict: true, **params) ⇒ CemAcpt::Bolt::Cmd::Output
Runs the command with the given options and environment variables.
- #to_s ⇒ Object
Methods included from Logging
current_log_config, #current_log_config, current_log_format, #current_log_format, #current_log_level, current_log_level, included, #logger, logger, new_log_config, #new_log_config, new_log_formatter, #new_log_formatter, #new_log_level, new_log_level, #new_logger, new_logger, verbose?, #verbose?
Constructor Details
#initialize ⇒ Base
Returns a new instance of Base.
52 53 54 55 |
# File 'lib/cem_acpt/bolt/cmd/base.rb', line 52 def initialize @cmd_env = { 'BOLT_GEM' => 'TRUE' } @cmd_params = {} end |
Instance Attribute Details
#cmd_env ⇒ Object (readonly)
Returns the value of attribute cmd_env.
50 51 52 |
# File 'lib/cem_acpt/bolt/cmd/base.rb', line 50 def cmd_env @cmd_env end |
#cmd_params ⇒ Object (readonly)
Returns the value of attribute cmd_params.
50 51 52 |
# File 'lib/cem_acpt/bolt/cmd/base.rb', line 50 def cmd_params @cmd_params end |
Class Method Details
.option(name, flag, config_path: nil, default: nil, bool_flag: false) ⇒ Object
Class method that defines an option for the command
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/cem_acpt/bolt/cmd/base.rb', line 30 def self.option(name, flag, config_path: nil, default: nil, bool_flag: false) name = name.to_sym option_names << name define_method(name) do if bool_flag find_val(name, flag: flag, config_path: config_path, default: default) ? flag : nil else val = find_val(name, flag: flag, config_path: config_path, default: default) val ? "#{flag} #{val}" : nil end end end |
.option_names ⇒ Object
Class method that holds the names of all options for the command
16 17 18 |
# File 'lib/cem_acpt/bolt/cmd/base.rb', line 16 def self.option_names @option_names ||= [] end |
.supports_params ⇒ Object
Denotes that the command supports parameters. Calling this method will define a supports_params? method on the command class that returns true.
46 47 48 |
# File 'lib/cem_acpt/bolt/cmd/base.rb', line 46 def self.supports_params define_method(:supports_params?) { true } end |
Instance Method Details
#add_cmd_env(name, value) ⇒ Object
Adds an environment variable to the command
74 75 76 |
# File 'lib/cem_acpt/bolt/cmd/base.rb', line 74 def add_cmd_env(name, value) @cmd_env[name] = value end |
#add_cmd_param(name, value) ⇒ Object
Adds a parameter to the command. If passing a complex value, such as a hash, use the JSON representation of the value.
82 83 84 |
# File 'lib/cem_acpt/bolt/cmd/base.rb', line 82 def add_cmd_param(name, value) @cmd_params[name] = value end |
#bolt_bin ⇒ Object
133 134 135 |
# File 'lib/cem_acpt/bolt/cmd/base.rb', line 133 def bolt_bin @bolt_bin ||= CemAcpt::Utils::Shell.which('bolt', raise_if_not_found: true) end |
#cmd ⇒ String
Returns the command to run as a string
67 68 69 |
# File 'lib/cem_acpt/bolt/cmd/base.rb', line 67 def cmd raise NotImplementedError, 'cmd method must be implemented in subclass' end |
#command_family ⇒ String
Returns the family of the command. This is used to determine which command to run when multiple commands are available for the same action. For example, the ‘task’ command has multiple subcommands, such as ‘task show’ and ‘task run’. The family of the ‘task’ command is ‘task’, and the family of the ‘task show’ and ‘task run’ subcommands is ‘task’.
92 93 94 |
# File 'lib/cem_acpt/bolt/cmd/base.rb', line 92 def command_family raise NotImplementedError, 'command_family method must be implemented in subclass' end |
#inspect ⇒ Object
57 58 59 |
# File 'lib/cem_acpt/bolt/cmd/base.rb', line 57 def inspect "#{self.class}:\"#{cmd}\"" end |
#options ⇒ String
Returns the options for the command as a string
121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/cem_acpt/bolt/cmd/base.rb', line 121 def opts = ['--format json'] recursive_option_names.each do |opt| val = send(opt) opts << val if val end if respond_to?(:supports_params?) && supports_params? && !cmd_params.empty? opts << "--params '#{JSON.generate(cmd_params)}'" end join_array(opts) end |
#run(strict: true, **params) ⇒ CemAcpt::Bolt::Cmd::Output
Runs the command with the given options and environment variables
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/cem_acpt/bolt/cmd/base.rb', line 102 def run(strict: true, **params) unless params.empty? logger.debug('CemAcpt::Bolt') { "Setting command parameters: #{params}" } params.each { |k, v| add_cmd_param(k, v) } end logger.debug('CemAcpt::Bolt') { "Running Bolt command: #{cmd}" } logger.debug('CemAcpt::Bolt') { "Bolt command environment: #{cmd_env}" } begin out = CemAcpt::Utils::Shell.run_cmd(cmd, cmd_env, output: nil, raise_on_fail: false) logger.debug('CemAcpt::Bolt') { "Bolt command raw output:\n#{out}" } CemAcpt::Bolt::Cmd::Output.new(out, strict: strict, **(@item_defaults || {})) rescue StandardError => e logger.debug('CemAcpt::Bolt') { "Bolt command error:\n#{e}" } CemAcpt::Bolt::Cmd::Output.new(e, **(@item_defaults || {})) end end |
#to_s ⇒ Object
61 62 63 |
# File 'lib/cem_acpt/bolt/cmd/base.rb', line 61 def to_s cmd end |