Class: Cpl::Cli
Overview
rubocop:disable Metrics/ClassLength
Class Method Summary collapse
- .all_base_commands ⇒ Object
-
.check_cpl_version ⇒ Object
rubocop:disable Metrics/MethodLength.
-
.check_cpln_version ⇒ Object
rubocop:disable Metrics/MethodLength.
- .deprecated_commands ⇒ Object
-
.exit_on_failure? ⇒ Boolean
Needed to silence deprecation warning.
-
.fix_help_option ⇒ Object
This is so that we’re able to run ‘cpl COMMAND –help` to print the help (it basically changes it to `cpl –help COMMAND`, which Thor recognizes) Based on stackoverflow.com/questions/49042591/how-to-add-help-h-flag-to-thor-command.
-
.is_thor_reserved_word?(word, type) ⇒ Boolean
Needed to be able to use “run” as a command.
- .process_option_params(params) ⇒ Object
-
.show_info_header(config) ⇒ Object
rubocop:disable Metrics/MethodLength.
- .start(*args) ⇒ Object
-
.validate_options!(options) ⇒ Object
rubocop:disable Metrics/MethodLength.
Class Method Details
.all_base_commands ⇒ Object
147 148 149 |
# File 'lib/cpl.rb', line 147 def self.all_base_commands ::Command::Base.all_commands.merge(deprecated_commands) end |
.check_cpl_version ⇒ Object
rubocop:disable Metrics/MethodLength
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/cpl.rb', line 90 def self.check_cpl_version # rubocop:disable Metrics/MethodLength return if @checked_cpl_version @checked_cpl_version = true result = ::Shell.cmd("gem", "search", "^cpl$", "--remote", capture_stderr: true) return unless result[:success] matches = result[:output].match(/cpl \((.+)\)/) return unless matches version = Cpl::VERSION latest_version = matches[1] return unless Gem::Version.new(version) < Gem::Version.new(latest_version) ::Shell.warn("You are not using the latest 'cpl' version. Please update it with 'gem update cpl'.") $stderr.puts end |
.check_cpln_version ⇒ Object
rubocop:disable Metrics/MethodLength
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/cpl.rb', line 70 def self.check_cpln_version # rubocop:disable Metrics/MethodLength return if @checked_cpln_version @checked_cpln_version = true result = ::Shell.cmd("cpln", "--version", capture_stderr: true) if result[:success] data = JSON.parse(result[:output]) version = data["npm"] min_version = Cpl::MIN_CPLN_VERSION if Gem::Version.new(version) < Gem::Version.new(min_version) ::Shell.abort("Current 'cpln' version: #{version}. Minimum supported version: #{min_version}. " \ "Please update it with 'npm update -g @controlplane/cli'.") end else ::Shell.abort("Can't find 'cpln' executable. Please install it with 'npm install -g @controlplane/cli'.") end end |
.deprecated_commands ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/cpl.rb', line 133 def self.deprecated_commands @deprecated_commands ||= begin deprecated_commands_file_path = "#{__dir__}/deprecated_commands.json" deprecated_commands_data = File.binread(deprecated_commands_file_path) deprecated_commands = JSON.parse(deprecated_commands_data) deprecated_commands.to_h do |old_command_name, new_command_name| file_name = new_command_name.gsub(/[^A-Za-z]/, "_") class_name = file_name.split("_").map(&:capitalize).join [old_command_name, Object.const_get("::Command::#{class_name}")] end end end |
.exit_on_failure? ⇒ Boolean
Needed to silence deprecation warning
122 123 124 |
# File 'lib/cpl.rb', line 122 def self.exit_on_failure? true end |
.fix_help_option ⇒ Object
This is so that we’re able to run ‘cpl COMMAND –help` to print the help (it basically changes it to `cpl –help COMMAND`, which Thor recognizes) Based on stackoverflow.com/questions/49042591/how-to-add-help-h-flag-to-thor-command
112 113 114 115 116 117 118 119 |
# File 'lib/cpl.rb', line 112 def self.fix_help_option help_mappings = Thor::HELP_MAPPINGS + ["help"] matches = help_mappings & ARGV matches.each do |match| ARGV.delete(match) ARGV.unshift(match) end end |
.is_thor_reserved_word?(word, type) ⇒ Boolean
Needed to be able to use “run” as a command
127 128 129 130 131 |
# File 'lib/cpl.rb', line 127 def self.is_thor_reserved_word?(word, type) # rubocop:disable Naming/PredicateName return false if word == "run" super(word, type) end |
.process_option_params(params) ⇒ Object
151 152 153 154 155 156 157 |
# File 'lib/cpl.rb', line 151 def self.process_option_params(params) # Ensures that if no value is provided for a non-boolean option (e.g., `cpl command --option`), # it defaults to an empty string instead of the option name (which is the default Thor behavior) params[:lazy_default] ||= "" if params[:type] != :boolean params end |
.show_info_header(config) ⇒ Object
rubocop:disable Metrics/MethodLength
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/cpl.rb', line 267 def self.show_info_header(config) # rubocop:disable Metrics/MethodLength return if @showed_info_header rows = {} rows["ORG"] = config.org || "NOT PROVIDED!" rows["ORG"] += " (comes from CPLN_ORG env var)" if config.org_comes_from_env rows["APP"] = config.app || "NOT PROVIDED!" rows["APP"] += " (comes from CPLN_APP env var)" if config.app_comes_from_env rows.each do |key, value| puts "#{key}: #{value}" end @showed_info_header = true # Add a newline after the info header puts end |
.start(*args) ⇒ Object
60 61 62 63 64 65 66 67 68 |
# File 'lib/cpl.rb', line 60 def self.start(*args) ENV["CPLN_SKIP_UPDATE_CHECK"] = "true" check_cpln_version check_cpl_version fix_help_option super(*args) end |
.validate_options!(options) ⇒ Object
rubocop:disable Metrics/MethodLength
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/cpl.rb', line 247 def self.() # rubocop:disable Metrics/MethodLength .each do |name, value| normalized_name = ::Helpers.normalize_option_name(name) raise "No value provided for option #{normalized_name}." if value.to_s.strip.empty? option = ::Command::Base..find { |current_option| current_option[:name].to_s == name } if option[:new_name] normalized_new_name = ::Helpers.normalize_option_name(option[:new_name]) ::Shell.warn_deprecated("Option #{normalized_name} is deprecated, " \ "please use #{normalized_new_name} instead.") $stderr.puts end params = option[:params] next unless params[:valid_regex] raise "Invalid value provided for option #{normalized_name}." unless value.match?(params[:valid_regex]) end end |