Skip to content

Instantly share code, notes, and snippets.

@danshultz
Created April 3, 2013 12:10
Show Gist options
  • Select an option

  • Save danshultz/5300645 to your computer and use it in GitHub Desktop.

Select an option

Save danshultz/5300645 to your computer and use it in GitHub Desktop.
Basic Rails task to initialize the secrets.rb
require 'ostruct'
namespace :bootstrap do
desc "Boostrap development machine"
task :setup do
Rake::Task['bootstrap:secrets'].invoke
end
task :secrets do
default_save_dir = "config/initializers"
save_path = File.join(ENV['SECRETS_DIR'] || default_save_dir, "secret_token.rb")
if File.exists? save_path
warning "#{save_path} already exists"
else
cipher = OpenSSL::Cipher.new('aes-256-cbc')
render(
"secret_token.rb.erb",
save_path,
secret_token: SecureRandom.hex(64)
)
info "#{save_path} file generated"
end
end
def render(template, save_path, vars)
template_file = File.open("lib/templates/#{template}", 'r').read
erb = ERB.new(template_file)
results = erb.result(OpenStruct.new(vars).instance_eval { binding })
File.open(save_path, 'w+') { |file| file.write(results) }
end
def colorize(text, color_code)
"\e[#{color_code}m#{text}\e[0m"
end
def error(text); puts "[#{red "ERROR"}] #{text}"; end
def warning(text); puts "[#{yellow "WARNING"}] #{text}"; end
def info(text); puts "[#{cyan "INFO"}] #{text}"; end
def debug(text); puts "[#{green "DEBUG"}] #{text}"; end
def red(text); colorize(text, 31); end
def green(text); colorize(text, 32); end
def yellow(text); colorize(text, 33); end
def cyan(text); colorize(text, 36); end
end
MyApp::Application.config.tap do |config|
config.secret_token = '<%= secret_token %>'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment