published by Edmund Haselwanter on Sunday, September 21, 2008
Sometimes invoking a task in Capistrano requires to input data. A very comfortable approach is to use highline since it is build for this purpose.
task :ask_for_environment do
set :environments, [:production,:testing,:development]
set(:environment, Capistrano::CLI.ui.choose do |menu|
environments.each do |choice|
menu.choice choice do set(:environment,choice)
end
end
end
) unless exists?(:environment)
unless environments.include?(environment.to_sym)
puts "".rjust(70,'-') << "\nERROR: environment \"#{environment}\" is NOT a valid option\n" << "".rjust(70,'-')
exit 1
end
puts "#{environment}"
end
I think this code is self explanatory (more or less ;-). Capistrano::CLI.ui. returns a highline object we can work with. For more examples on how to use highline visit the highline svn repo This can then be combined with any task you like through the before hook
before "deploy", "ask_for_environment"
The
unless exists?(:environment) lets one bypass the menu through setting the parameter at the invocation line ( -s environment=production)