_ftisa
Last Updated: February 25, 2016
·
3.1K
· mbillard
Michel funny square

Add a confirmation step to your rake tasks

Have you ever wanted to protect yourself from accidentally running a critical rake task? If so, start by creating a confirmation task:

task :confirm do
  confirm_token = rand(36**6).to_s(36)
  STDOUT.puts "Confirm [ACTION]? Enter '#{confirm_token}' to confirm:"
  input = STDIN.gets.chomp
  raise "Aborting [ACTION]. You entered #{input}" unless input == confirm_token
end

Of course replace [ACTION] by something relevant (or something generic). This implementation generates a random 6-character string that you will need to enter before continuing.

You can then use it by calling it before your own tasks:

task :deploy_to_production => :confirm do
  ...
end

Your critical rake tasks are now safe from you!

Say Thanks
Respond

3 Responses
Add your response

14568
Justin2011 brownshirt reduced

Awesome! from @railsonmaui.

over 1 year ago ·
15343
F81076ca19a5273854a17f0f337ed89a

Thank you!

over 1 year ago ·
18042
D0c48936ccbd3f2b0144e5e472005921

Really useful =)

over 1 year ago ·