Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't exit from failed secrets commands #934

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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