Skip to content

Commit

Permalink
Don't exit from failed secrets commands
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
djmb committed Sep 11, 2024
1 parent debdf00 commit 0660895
Showing 1 changed file with 12 additions and 23 deletions.
35 changes: 12 additions & 23 deletions lib/kamal/cli/secrets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,32 @@ 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
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

0 comments on commit 0660895

Please sign in to comment.