Created
March 17, 2012 11:08
-
-
Save corny/2057660 to your computer and use it in GitHub Desktop.
Rake task to replace the secret token in your config/initializers/secret_token.rb
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
# | |
# License: WTFPL (http://sam.zoy.org/wtfpl/) | |
# | |
namespace :secret do | |
desc 'Replace the secure secret key in your secret_token.rb' | |
task :replace do | |
pattern = /(\.secret_token *= *')\w+(')/ | |
secret = SecureRandom.hex(64) | |
filepath = "#{Rails.root}/config/initializers/secret_token.rb" | |
content = File.read(filepath) | |
unless pattern | |
STDERR.puts "no secret token found in #{filepath}" | |
exit 1 | |
end | |
# replace the secret token | |
content.gsub!(pattern,"\\1#{secret}\\2") | |
# write the new configuration | |
File.open(filepath, 'w') {|f| f.write(content) } | |
puts "Secret token succesfully replaced" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment