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

Merge pull request #2055 from rolandasb/user-input-prompt-option

Add custom prompt support for user input
This commit is contained in:
Lee Hambley 2020-04-19 15:43:57 +02:00 committed by GitHub
commit 46e00dec15
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 1 deletions

View file

@ -49,6 +49,12 @@ server 'example.com', user: 'ssh_user_name', port: 22, password: fetch(:password
```
You can also show your own message by using `prompt` option:
```ruby
ask(:breakfast, "pancakes", prompt: "What's for breakfast?")
```
**Important!** `ask` will not prompt the user immediately. The question is
deferred until the first time `fetch` is used to obtain the setting. That means
you can `ask` for many variables, but only the variables used by your task(s)

View file

@ -49,7 +49,11 @@ module Capistrano
end
def question
if default.nil?
if prompt && default.nil?
I18n.t(:question_prompt, key: prompt, scope: :capistrano)
elsif prompt
I18n.t(:question_prompt_default, key: prompt, default_value: default, scope: :capistrano)
elsif default.nil?
I18n.t(:question, key: key, scope: :capistrano)
else
I18n.t(:question_default, key: key, default_value: default, scope: :capistrano)
@ -63,6 +67,10 @@ module Capistrano
def stdin
(options || {}).fetch(:stdin, $stdin)
end
def prompt
(options || {}).fetch(:prompt, nil)
end
end
end
end

View file

@ -12,6 +12,8 @@ en = {
written_file: "create %{file}",
question: "Please enter %{key}: ",
question_default: "Please enter %{key} (%{default_value}): ",
question_prompt: "%{key}: ",
question_prompt_default: "%{key} (%{default_value}): ",
keeping_releases: "Keeping %{keep_releases} of %{releases} deployed releases on %{host}",
skip_cleanup: "Skipping cleanup of invalid releases on %{host}; unexpected foldername found (should be timestamp)",
wont_delete_current_release: "Current release was marked for being removed but it's going to be skipped on %{host}",

View file

@ -6,6 +6,8 @@ module Capistrano
let(:question) { Question.new(key, default, stdin: stdin) }
let(:question_without_echo) { Question.new(key, default, echo: false, stdin: stdin) }
let(:question_without_default) { Question.new(key, nil, stdin: stdin) }
let(:question_prompt) { Question.new(key, default, stdin: stdin, prompt: "Your favorite branch") }
let(:question_prompt_without_default) { Question.new(key, nil, stdin: stdin, prompt: "Your favorite branch") }
let(:default) { :default }
let(:key) { :branch }
let(:stdin) { stub(tty?: true) }
@ -43,6 +45,22 @@ module Capistrano
expect(question_without_default.call).to eq(branch)
end
it "uses prompt and returns the value" do
$stdout.expects(:print).with("Your favorite branch (default): ")
stdin.expects(:gets).returns(branch)
stdin.expects(:noecho).never
expect(question_prompt.call).to eq(branch)
end
it "uses prompt and returns the value but has no default between parenthesis" do
$stdout.expects(:print).with("Your favorite branch: ")
stdin.expects(:gets).returns(branch)
stdin.expects(:noecho).never
expect(question_prompt_without_default.call).to eq(branch)
end
end
context "value is not entered" do