Skip to content

Instantly share code, notes, and snippets.

@loftwah
Created May 22, 2025 01:12
Show Gist options
  • Save loftwah/cd85faaac1c489e12c75343ed971c2a3 to your computer and use it in GitHub Desktop.
Save loftwah/cd85faaac1c489e12c75343ed971c2a3 to your computer and use it in GitHub Desktop.
generate_credentials_template
#!/usr/bin/env ruby
# frozen_string_literal: true
require "yaml"
CREDENTIALS_PATH = File.expand_path("config/credentials.yml.enc")
MASTER_KEY_PATH = File.expand_path("config/master.key")
def decrypt_credentials(path)
key = ENV["RAILS_MASTER_KEY"] || File.read(MASTER_KEY_PATH).strip rescue nil
abort("❌ Missing master key.") unless key
require "active_support/encrypted_file"
encrypted = ActiveSupport::EncryptedFile.new(
content_path: path,
key_path: MASTER_KEY_PATH,
env_key: "RAILS_MASTER_KEY",
raise_if_missing_key: true
)
encrypted.read
end
def scrub_yaml(data)
case data
when Hash
data.transform_values { |v| scrub_yaml(v) }
when Array
data.map { |v| scrub_yaml(v) }
else
"VALUE"
end
end
begin
raw = decrypt_credentials(CREDENTIALS_PATH)
parsed = YAML.safe_load(raw, aliases: true)
template = scrub_yaml(parsed)
puts "# config/credentials.example.yml"
puts template.to_yaml
rescue => e
warn "❌ Error: #{e.message}"
exit 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment