Created
December 17, 2020 20:01
-
-
Save mroach/b89ca994995eb2ff3942be7969a7357a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# | |
# Convert structured Ruby credentials to JSON that can be easily copy/pasted | |
# into an EJSON secrets file. | |
# | |
# Usage: | |
# | |
# rails credentails:show --environment staging | ./conv.rb | |
require 'yaml' | |
require 'json' | |
creds = YAML.load(ARGF.read) | |
def flatten_keys(memo, key, value) | |
if value.is_a?(Hash) | |
value.reduce(memo) { |memo, (k, v)| flatten_keys(memo, "#{key}_#{k}", v) } | |
else | |
value = "" if value.nil? | |
value = value.join(",") if value.is_a?(Array) | |
value = value.to_s if value.is_a?(Numeric) | |
memo + [ [key.upcase, value] ] | |
end | |
end | |
rendered = creds.reduce([]) do |memo, (k, v)| | |
flatten_keys(memo, k, v) | |
end.sort.to_h | |
puts JSON.pretty_generate(rendered) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment