Skip to content

Instantly share code, notes, and snippets.

@mroach
Created December 17, 2020 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mroach/b89ca994995eb2ff3942be7969a7357a to your computer and use it in GitHub Desktop.
Save mroach/b89ca994995eb2ff3942be7969a7357a to your computer and use it in GitHub Desktop.
#!/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