mirror of
https://github.com/capistrano/capistrano
synced 2023-03-27 23:21:18 -04:00
Refactor ask() to use Highline; support echo:false.
Note: a gem dependency for highline has been added by this commit.
This commit is contained in:
parent
53957e7d19
commit
4aebffe718
9 changed files with 63 additions and 23 deletions
|
@ -11,6 +11,8 @@ https://github.com/capistrano/capistrano/compare/v3.2.0...HEAD
|
|||
* Changed asking question to more standard format (like common unix commandline tools) (@sponomarev)
|
||||
* Fixed typos in the README. (@sponomarev)
|
||||
* Added `keys` method to Configuration to allow introspection of configuration options. (@juanibiapina)
|
||||
* The `ask` method now supports the `echo: false` option. (@mbrictson)
|
||||
* Capistrano now depends on the `highline` gem.
|
||||
|
||||
## `3.2.0`
|
||||
|
||||
|
|
|
@ -153,6 +153,11 @@ end
|
|||
|
||||
Perfect, who needs telephones.
|
||||
|
||||
When using `ask` to get user input, you can pass `echo: false` to prevent the input from being displayed:
|
||||
|
||||
```ruby
|
||||
ask(:database_passsword, "default", echo: false)
|
||||
```
|
||||
|
||||
## Using password authentication
|
||||
|
||||
|
|
|
@ -28,6 +28,7 @@ eos
|
|||
gem.add_dependency 'sshkit', '~> 1.3'
|
||||
gem.add_dependency 'rake', '>= 10.0.0'
|
||||
gem.add_dependency 'i18n'
|
||||
gem.add_dependency 'highline'
|
||||
|
||||
gem.add_development_dependency 'rspec'
|
||||
gem.add_development_dependency 'mocha'
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
require 'rake'
|
||||
require 'sshkit'
|
||||
require 'sshkit/dsl'
|
||||
require 'highline'
|
||||
|
||||
Rake.application.options.trace = true
|
||||
|
||||
|
|
|
@ -15,8 +15,8 @@ module Capistrano
|
|||
end
|
||||
end
|
||||
|
||||
def ask(key, default=nil)
|
||||
question = Question.new(self, key, default)
|
||||
def ask(key, default=nil, options={})
|
||||
question = Question.new(self, key, default, options)
|
||||
set(key, question)
|
||||
end
|
||||
|
||||
|
|
|
@ -2,27 +2,23 @@ module Capistrano
|
|||
class Configuration
|
||||
class Question
|
||||
|
||||
def initialize(env, key, default)
|
||||
@env, @key, @default = env, key, default
|
||||
def initialize(env, key, default, options)
|
||||
@env, @key, @default, @options = env, key, default, options
|
||||
end
|
||||
|
||||
def call
|
||||
ask_question
|
||||
save_response
|
||||
response = highline_ask(question) { |q| q.echo = echo? }
|
||||
save_response(value_or_default(response))
|
||||
end
|
||||
|
||||
private
|
||||
attr_reader :env, :key, :default
|
||||
attr_reader :env, :key, :default, :options
|
||||
|
||||
def ask_question
|
||||
$stdout.print question
|
||||
end
|
||||
|
||||
def save_response
|
||||
def save_response(value)
|
||||
env.set(key, value)
|
||||
end
|
||||
|
||||
def value
|
||||
def value_or_default(response)
|
||||
if response.empty?
|
||||
default
|
||||
else
|
||||
|
@ -30,13 +26,19 @@ module Capistrano
|
|||
end
|
||||
end
|
||||
|
||||
def response
|
||||
@response ||= $stdin.gets.chomp
|
||||
end
|
||||
|
||||
def question
|
||||
I18n.t(:question, key: key, default_value: default, scope: :capistrano)
|
||||
end
|
||||
|
||||
def echo?
|
||||
(options || {}).fetch(:echo, true)
|
||||
end
|
||||
|
||||
def highline_ask(question, &block)
|
||||
# For compatibility, we call #to_s to unwrap HighLine::String and
|
||||
# return a regular String.
|
||||
HighLine.new.ask(question, &block).to_s
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -27,8 +27,8 @@ module Capistrano
|
|||
env.delete(key)
|
||||
end
|
||||
|
||||
def ask(key, value)
|
||||
env.ask(key, value)
|
||||
def ask(key, value, options={})
|
||||
env.ask(key, value, options)
|
||||
end
|
||||
|
||||
def role(name, servers, options={})
|
||||
|
|
|
@ -5,13 +5,14 @@ module Capistrano
|
|||
|
||||
describe Question do
|
||||
|
||||
let(:question) { Question.new(env, key, default) }
|
||||
let(:question) { Question.new(env, key, default, options) }
|
||||
let(:default) { :default }
|
||||
let(:key) { :branch }
|
||||
let(:env) { stub }
|
||||
let(:options) { nil }
|
||||
|
||||
describe '.new' do
|
||||
it 'takes a key, default' do
|
||||
it 'takes a key, default, options' do
|
||||
question
|
||||
end
|
||||
end
|
||||
|
@ -47,6 +48,33 @@ module Capistrano
|
|||
end
|
||||
|
||||
end
|
||||
|
||||
describe 'highline behavior' do
|
||||
let(:highline) { stub }
|
||||
|
||||
before do
|
||||
question.expects(:highline_ask).yields(highline).returns("answer")
|
||||
env.expects(:set).with(key, "answer")
|
||||
end
|
||||
|
||||
context 'with no options' do
|
||||
let(:options) { nil }
|
||||
|
||||
it 'passes echo: true to HighLine' do
|
||||
highline.expects(:"echo=").with(true)
|
||||
question.call
|
||||
end
|
||||
end
|
||||
|
||||
context 'with echo: false' do
|
||||
let(:options) { { echo: false } }
|
||||
|
||||
it 'passes echo: false to HighLine' do
|
||||
highline.expects(:"echo=").with(false)
|
||||
question.call
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -138,14 +138,15 @@ module Capistrano
|
|||
|
||||
describe 'asking' do
|
||||
let(:question) { stub }
|
||||
let(:options) { Hash.new }
|
||||
|
||||
before do
|
||||
Configuration::Question.expects(:new).with(config, :branch, :default).
|
||||
Configuration::Question.expects(:new).with(config, :branch, :default, options).
|
||||
returns(question)
|
||||
end
|
||||
|
||||
it 'prompts for the value when fetching' do
|
||||
config.ask(:branch, :default)
|
||||
config.ask(:branch, :default, options)
|
||||
expect(config.fetch(:branch)).to eq question
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue