1
0
Fork 0
mirror of https://github.com/capistrano/capistrano synced 2023-03-27 23:21:18 -04:00

Support ask

As an alternative to `set`, `ask` takes a key and a default value. When
the key is called with `fetch` the user will be prompted to set the
value.
This commit is contained in:
seenmyfate 2013-03-15 11:35:49 +00:00
parent cfdca4ef08
commit 8c1ede50cd
6 changed files with 130 additions and 10 deletions

View file

@ -21,8 +21,9 @@ TODO:
- [x] support set/fetch/role configuration
- [x] basic deploy
- [x] ask
- [ ] support existing significant configuration variables
- [ ] ask
- [ ] prefer `roles(:all)` over `all_roles`
- [ ] support primary servers `on primary(:db)`
- [ ] basic rollback
- [ ] add examples to README
@ -66,12 +67,13 @@ To create different stages:
## Configuration
# config/deploy.rb
set :application, 'example app'
# config/deploy/production.rb
set :stage, :production
ask :branch, :master
role :app, %w{example.com}
role :web, %w{example.com}
role :db, %w{example.com}

View file

@ -7,15 +7,21 @@ module Capistrano
end
end
def ask(key, default=nil)
question = Question.new(self, key, default)
set(key, question)
end
def set(key, value)
config[key] = value
end
def fetch(key, default=nil, &block)
if block_given?
config.fetch(key, &block)
value = fetch_for(key, default, &block)
if value.respond_to?(:call)
set(key, value.call)
else
config.fetch(key, default)
value
end
end
@ -55,6 +61,55 @@ module Capistrano
@config ||= Hash.new
end
def fetch_for(key, default, &block)
if block_given?
config.fetch(key, &block)
else
config.fetch(key, default)
end
end
class Question
def initialize(env, key, default)
@env, @key, @default = env, key, default
end
def call
ask_question
save_response
end
private
attr_reader :env, :key, :default
def ask_question
$stdout.puts question
end
def save_response
env.set(key, value)
end
def value
if response.empty?
default
else
response
end
end
def response
@response ||= $stdin.gets.chomp
end
def set(key, value)
end
def question
I18n.t(:question, key: key, default_value: default, scope: :capistrano)
end
end
class Roles
include Enumerable

View file

@ -14,6 +14,10 @@ module Capistrano
env.set(key, value)
end
def ask(key, value)
env.ask(key, value)
end
def role(name, servers)
env.role(name, servers)
end

View file

@ -11,6 +11,7 @@ en = {
finished: 'Finished',
stage_not_set: 'Stage not set',
written_file: 'create %{file}',
question: 'Please enter %{key}: |%{default_value}|',
console: {
welcome: 'capistrano console - enter command to execute on %{stage}',
bye: 'bye'

View file

@ -4,8 +4,7 @@ set :deploy_to, '/var/www/my_app'
set :scm, :git
set :repo, 'git@example.com:me/my_repo.git'
set :migrations, :migrate
set :branch, :master
ask :branch, :master
set :linked_files, %w{config/database.yml}
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}

View file

@ -98,10 +98,69 @@ module Capistrano
end
context 'block is passed' do
subject { config.fetch(:key, :default) { :block } }
subject { config.fetch(:key, :default) { fail 'we need this!' } }
it 'returns the block value' do
expect(subject).to eq :block
expect { subject }.to raise_error
end
end
end
describe 'asking' do
let(:question) { stub }
before do
Configuration::Question.expects(:new).with(config, :branch, :default).
returns(question)
end
it 'prompts for the value when fetching' do
config.ask(:branch, :default)
expect(config.fetch(:branch)).to eq question
end
end
end
describe Configuration::Question do
let(:question) { Configuration::Question.new(env, key, default) }
let(:default) { :default }
let(:key) { :branch }
let(:env) { stub }
describe '.new' do
it 'takes a key, default' do
question
end
end
describe '#call' do
subject { question.call }
context 'value is entered' do
let(:branch) { 'branch' }
before do
$stdout.expects(:puts).with('Please enter branch: |default|')
$stdin.expects(:gets).returns(branch)
end
it 'sets the value' do
env.expects(:set).with(key, branch)
question.call
end
end
context 'value is not entered' do
let(:branch) { default }
before do
$stdout.expects(:puts).with('Please enter branch: |default|')
$stdin.expects(:gets).returns('')
end
it 'sets the default as the value' do
env.expects(:set).with(key, branch)
question.call
end
end
end