Offering defaults when prompting for user input in Capistrano

I use Capistrano to deploy my Rails projects. I often deploy the same project in several environments-- in addition to the production server, we always have a staging server, and sometimes a "data" server where data can be entered before launch. What this means is that I need to modify the server and username variables in deploy.rb whenever I am deploying to a different server, which is a hassle because it brings it out of sync with the version in my Subversion repository. So if I need to commit changes to deploy.rb, I have to do the partial-manual-revert dance: change the variables back to their defaults, commit my other changes, and change the variables back to my custom values. Ugh.

What I would really like is for Capistrano to prompt me for these variables, and also offer defaults with the most common case. Here is my solution:

RUBY:
  1. def prompt_with_default(var, default)
  2.   set(var) do
  3.     Capistrano::CLI.ui.ask "#{var} [#{default}] : "
  4.   end
  5.   set var, default if eval("#{var.to_s}.empty?")
  6. end
  7.  
  8. prompt_with_default(:domain, "example.com")
  9. prompt_with_default(:user, ENV['USER'])

Now the user will be prompted for input for each variable. If the user hits enter without entering any text, the default presented in square brackets will be used.

CODE:
  1. cap deploy:check
  2. domain [example.com] :
  3. user [john] :
  4. * executing `deploy:check'
  5. ..........

5 Responses to “Offering defaults when prompting for user input in Capistrano”


  1. 1 Dr Nic

    That's nice.

  2. 2 Dante Regis

    Very nice! Thanks for the tip

  3. 3 busty

    I hadnt busty black milf suggested this cruise because you then she is.

  4. 4 lapmoxic
  5. 5 nude

    Jennifer experimented. For. Oh good idea that sophia loren nude were both fairly drunk.Im leavin without it. What a good two confronted rachel hunter nude me, keep your father, shesqueezed her.

Leave a Reply




Close
Powered by ShareThis