lib/command/base.rb in cpl-0.4.1 vs lib/command/base.rb in cpl-0.5.0

- old
+ new

@@ -36,10 +36,23 @@ classname = File.read(file).match(/^\s+class (\w+) < Base($| .*$)/)&.captures&.first result[filename.to_sym] = Object.const_get("::Command::#{classname}") if classname end end + def self.org_option(required: false) + { + name: :org, + params: { + aliases: ["-o"], + banner: "ORG_NAME", + desc: "Organization name", + type: :string, + required: required + } + } + end + def self.app_option(required: false) { name: :app, params: { aliases: ["-a"], @@ -88,10 +101,23 @@ required: required } } end + def self.upstream_token_option(required: false) + { + name: :upstream_token, + params: { + aliases: ["-t"], + banner: "UPSTREAM_TOKEN", + desc: "Upstream token", + type: :string, + required: required + } + } + end + def self.skip_confirm_option(required: false) { name: :yes, params: { aliases: ["-y"], @@ -161,21 +187,23 @@ 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 ||= + def latest_image(app = config.app, org = config.org) + @latest_image ||= {} + @latest_image[app] ||= begin - items = cp.image_query["items"] - latest_image_from(items) + items = cp.image_query(app, org)["items"] + latest_image_from(items, app_name: app) end end - def latest_image_next - @latest_image_next ||= begin - image = latest_image.split(":").first + def latest_image_next(app = config.app, org = config.org) + @latest_image_next ||= {} + @latest_image_next[app] ||= begin + image = latest_image(app, org).split(":").first image += ":#{extract_image_number(latest_image) + 1}" image += "_#{config.options[:commit]}" if config.options[:commit] image end end @@ -188,11 +216,41 @@ def progress $stderr end + def step(message, abort_on_error: true) # rubocop:disable Metrics/MethodLength + progress.print("#{message}...") + + Shell.use_tmp_stderr do + success = false + + begin + success = yield + rescue RuntimeError => e + message = e.message + if abort_on_error + progress.puts(" #{Shell.color('failed!', :red)}\n\n") + Shell.abort(message) + else + Shell.write_to_tmp_stderr(message) + end + end + + if success + progress.puts(" #{Shell.color('done!', :green)}") + else + progress.puts(" #{Shell.color('failed!', :red)}\n\n#{Shell.read_from_tmp_stderr}\n\n") + end + end + end + def cp @cp ||= Controlplane.new(config) + end + + def perform(cmd) + system(cmd) || exit(false) end private # returns 0 if no prior image