From 0660895e7589fef56a174d5027eb587bb9b60342 Mon Sep 17 00:00:00 2001 From: Donal McBreen Date: Wed, 11 Sep 2024 16:10:52 +0100 Subject: [PATCH] Don't exit from failed secrets commands We can let the exception bubble up. We'll still get an error message and it ensures that any cleanup we need is done (i.e. releasing deploy locks). --- lib/kamal/cli/secrets.rb | 35 ++++++++++++----------------------- 1 file changed, 12 insertions(+), 23 deletions(-) diff --git a/lib/kamal/cli/secrets.rb b/lib/kamal/cli/secrets.rb index cab53ca87..8b919f9a3 100644 --- a/lib/kamal/cli/secrets.rb +++ b/lib/kamal/cli/secrets.rb @@ -5,24 +5,20 @@ class Kamal::Cli::Secrets < Kamal::Cli::Base option :from, type: :string, required: false, desc: "A vault or folder to fetch the secrets from" option :inline, type: :boolean, required: false, hidden: true def fetch(*secrets) - handle_output(inline: options[:inline]) do - results = adapter(options[:adapter]).fetch(secrets, **options.slice(:account, :from).symbolize_keys) - JSON.dump(results).shellescape - end + results = adapter(options[:adapter]).fetch(secrets, **options.slice(:account, :from).symbolize_keys) + + return_or_puts JSON.dump(results).shellescape, inline: options[:inline] end desc "extract", "Extract a single secret from the results of a fetch call" option :inline, type: :boolean, required: false, hidden: true def extract(name, secrets) - handle_output(inline: options[:inline]) do - parsed_secrets = JSON.parse(secrets) + parsed_secrets = JSON.parse(secrets) + value = parsed_secrets[name] || parsed_secrets.find { |k, v| k.end_with?("/#{name}") }&.last - value = parsed_secrets[name] || parsed_secrets.find { |k, v| k.end_with?("/#{name}") }&.last + raise "Could not find secret #{name}" if value.nil? - raise "Could not find secret #{name}" if value.nil? - - value - end + return_or_puts value, inline: options[:inline] end private @@ -30,18 +26,11 @@ def adapter(adapter) Kamal::Secrets::Adapters.lookup(adapter) end - def handle_output(inline: nil) - yield.tap do |output| - puts output unless inline + def return_or_puts(value, inline: nil) + if inline + value + else + puts value end - rescue => e - handle_error(e) - end - - def handle_error(e) - $stderr.puts " \e[31mERROR (#{e.class}): #{e.message}\e[0m" - $stderr.puts e.backtrace if ENV["VERBOSE"] - - exit 1 end end