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)
|
* Changed asking question to more standard format (like common unix commandline tools) (@sponomarev)
|
||||||
* Fixed typos in the README. (@sponomarev)
|
* Fixed typos in the README. (@sponomarev)
|
||||||
* Added `keys` method to Configuration to allow introspection of configuration options. (@juanibiapina)
|
* 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`
|
## `3.2.0`
|
||||||
|
|
||||||
|
|
|
@ -153,6 +153,11 @@ end
|
||||||
|
|
||||||
Perfect, who needs telephones.
|
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
|
## Using password authentication
|
||||||
|
|
||||||
|
|
|
@ -28,6 +28,7 @@ eos
|
||||||
gem.add_dependency 'sshkit', '~> 1.3'
|
gem.add_dependency 'sshkit', '~> 1.3'
|
||||||
gem.add_dependency 'rake', '>= 10.0.0'
|
gem.add_dependency 'rake', '>= 10.0.0'
|
||||||
gem.add_dependency 'i18n'
|
gem.add_dependency 'i18n'
|
||||||
|
gem.add_dependency 'highline'
|
||||||
|
|
||||||
gem.add_development_dependency 'rspec'
|
gem.add_development_dependency 'rspec'
|
||||||
gem.add_development_dependency 'mocha'
|
gem.add_development_dependency 'mocha'
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
require 'rake'
|
require 'rake'
|
||||||
require 'sshkit'
|
require 'sshkit'
|
||||||
require 'sshkit/dsl'
|
require 'sshkit/dsl'
|
||||||
|
require 'highline'
|
||||||
|
|
||||||
Rake.application.options.trace = true
|
Rake.application.options.trace = true
|
||||||
|
|
||||||
|
|
|
@ -15,8 +15,8 @@ module Capistrano
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def ask(key, default=nil)
|
def ask(key, default=nil, options={})
|
||||||
question = Question.new(self, key, default)
|
question = Question.new(self, key, default, options)
|
||||||
set(key, question)
|
set(key, question)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -2,27 +2,23 @@ module Capistrano
|
||||||
class Configuration
|
class Configuration
|
||||||
class Question
|
class Question
|
||||||
|
|
||||||
def initialize(env, key, default)
|
def initialize(env, key, default, options)
|
||||||
@env, @key, @default = env, key, default
|
@env, @key, @default, @options = env, key, default, options
|
||||||
end
|
end
|
||||||
|
|
||||||
def call
|
def call
|
||||||
ask_question
|
response = highline_ask(question) { |q| q.echo = echo? }
|
||||||
save_response
|
save_response(value_or_default(response))
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
attr_reader :env, :key, :default
|
attr_reader :env, :key, :default, :options
|
||||||
|
|
||||||
def ask_question
|
def save_response(value)
|
||||||
$stdout.print question
|
|
||||||
end
|
|
||||||
|
|
||||||
def save_response
|
|
||||||
env.set(key, value)
|
env.set(key, value)
|
||||||
end
|
end
|
||||||
|
|
||||||
def value
|
def value_or_default(response)
|
||||||
if response.empty?
|
if response.empty?
|
||||||
default
|
default
|
||||||
else
|
else
|
||||||
|
@ -30,13 +26,19 @@ module Capistrano
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def response
|
|
||||||
@response ||= $stdin.gets.chomp
|
|
||||||
end
|
|
||||||
|
|
||||||
def question
|
def question
|
||||||
I18n.t(:question, key: key, default_value: default, scope: :capistrano)
|
I18n.t(:question, key: key, default_value: default, scope: :capistrano)
|
||||||
end
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -27,8 +27,8 @@ module Capistrano
|
||||||
env.delete(key)
|
env.delete(key)
|
||||||
end
|
end
|
||||||
|
|
||||||
def ask(key, value)
|
def ask(key, value, options={})
|
||||||
env.ask(key, value)
|
env.ask(key, value, options)
|
||||||
end
|
end
|
||||||
|
|
||||||
def role(name, servers, options={})
|
def role(name, servers, options={})
|
||||||
|
|
|
@ -5,13 +5,14 @@ module Capistrano
|
||||||
|
|
||||||
describe Question do
|
describe Question do
|
||||||
|
|
||||||
let(:question) { Question.new(env, key, default) }
|
let(:question) { Question.new(env, key, default, options) }
|
||||||
let(:default) { :default }
|
let(:default) { :default }
|
||||||
let(:key) { :branch }
|
let(:key) { :branch }
|
||||||
let(:env) { stub }
|
let(:env) { stub }
|
||||||
|
let(:options) { nil }
|
||||||
|
|
||||||
describe '.new' do
|
describe '.new' do
|
||||||
it 'takes a key, default' do
|
it 'takes a key, default, options' do
|
||||||
question
|
question
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -47,6 +48,33 @@ module Capistrano
|
||||||
end
|
end
|
||||||
|
|
||||||
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -138,14 +138,15 @@ module Capistrano
|
||||||
|
|
||||||
describe 'asking' do
|
describe 'asking' do
|
||||||
let(:question) { stub }
|
let(:question) { stub }
|
||||||
|
let(:options) { Hash.new }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
Configuration::Question.expects(:new).with(config, :branch, :default).
|
Configuration::Question.expects(:new).with(config, :branch, :default, options).
|
||||||
returns(question)
|
returns(question)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'prompts for the value when fetching' do
|
it 'prompts for the value when fetching' do
|
||||||
config.ask(:branch, :default)
|
config.ask(:branch, :default, options)
|
||||||
expect(config.fetch(:branch)).to eq question
|
expect(config.fetch(:branch)).to eq question
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue