lib/command/base.rb in cpl-0.1.0 vs lib/command/base.rb in cpl-0.2.0

- old
+ new

@@ -1,10 +1,10 @@ # frozen_string_literal: true module Command class Base # rubocop:disable Metrics/ClassLength - attr_reader :config + attr_reader :thor_shell, :config # Used to call the command (`cpl NAME`) # NAME = "" # Displayed when running `cpl help` or `cpl help NAME` (defaults to `NAME`) USAGE = "" @@ -25,10 +25,11 @@ HIDE = false NO_IMAGE_AVAILABLE = "NO_IMAGE_AVAILABLE" def initialize(config) + @thor_shell = Thor::Shell::Color.new @config = config end def self.all_commands Dir["#{__dir__}/*.rb"].each_with_object({}) do |file, result| @@ -88,18 +89,31 @@ required: required } } end + def self.skip_confirm_option(required: false) + { + name: :yes, + params: { + aliases: ["-y"], + banner: "SKIP_CONFIRM", + desc: "Skip confirmation", + type: :boolean, + required: required + } + } + end + def self.all_options methods.grep(/_option$/).map { |method| send(method.to_s) } end - def self.all_options_key_name + def self.all_options_by_key_name all_options.each_with_object({}) do |option, result| - option[:params][:aliases].each { |current_alias| result[current_alias.to_s] = option[:name] } - result["--#{option[:name]}"] = option[:name] + option[:params][:aliases].each { |current_alias| result[current_alias.to_s] = option } + result["--#{option[:name]}"] = option end end def wait_for(title) progress.print "- Waiting for #{title}" @@ -123,23 +137,26 @@ def ensure_workload_deleted(workload) progress.puts "- Ensure workload is deleted" cp.workload_delete(workload, no_raise: true) end - def latest_image # rubocop:disable Metrics/MethodLength + def latest_image_from(items, app_name: config.app, name_only: true) + matching_items = items.filter { |item| item["name"].start_with?("#{app_name}:") } + + # Or special string to indicate no image available + if matching_items.empty? + "#{app_name}:#{NO_IMAGE_AVAILABLE}" + else + latest_item = matching_items.max_by { |item| extract_image_number(item["name"]) } + name_only ? latest_item["name"] : latest_item + end + end + + def latest_image @latest_image ||= begin items = cp.image_query["items"] - matching_items = items.filter_map do |item| - item["name"] if item["name"].start_with?("#{config.app}:") - end - - # Or special string to indicate no image available - if matching_items.empty? - "#{config.app}:#{NO_IMAGE_AVAILABLE}" - else - matching_items.max_by { |item| extract_image_number(item) } - end + latest_image_from(items) end end def latest_image_next @latest_image_next ||= begin